lift and throw

import java.util.*;
import java.math.*; public class Main {
public static void main(String[] args) { Scanner sc = new Scanner(System.in);
int aPos = sc.nextInt();
int aMov = sc.nextInt();
int aThr = sc.nextInt();
int bPos = sc.nextInt();
int bMov = sc.nextInt();
int bThr = sc.nextInt();
int cPos = sc.nextInt();
int cMov = sc.nextInt();
int cThr = sc.nextInt();
sc.close(); Solution s = new Solution();
s.start(aPos, aMov, aThr, bPos, bMov, bThr, cPos, cMov, cThr);
}
} class Solution {
private int maxLength = 0; private int aMov;
private int aThr;
private int bMov;
private int bThr;
private int cMov;
private int cThr; public void start(int aPos, int aMov, int aThr,
int bPos, int bMov, int bThr,
int cPos, int cMov, int cThr) {
this.aMov = aMov;
this.aThr = aThr;
this.bMov = bMov;
this.bThr = bThr;
this.cMov = cMov;
this.cThr = cThr;
recursion(aPos, bPos, cPos, 511, 0, 0, 0);
System.out.println(this.maxLength);
} public void recursion(int curLengthA, int curLengthB, int curLengthC, int movStates, int aPos, int bPos, int cPos) {
int curMax = max_3(curLengthA, curLengthB, curLengthC);
if(maxLength < curMax)
maxLength = curMax; if(movStates == 0 || curLengthA < 0 || curLengthB < 0 || curLengthC < 0)
return ; //Walk
if( ((movStates & 448) == 256 || (movStates & 448) == 448 ) && aPos == 0) {
for(int i = 1; i <= this.aMov; i++) {
if(curLengthA + i != curLengthB && curLengthA + i != curLengthC)
recursion(curLengthA + i, curLengthB, curLengthC, movStates & 255, aPos, bPos, cPos);
if(curLengthA - i != curLengthB && curLengthA - i != curLengthC)
recursion(curLengthA - i, curLengthB, curLengthC, movStates & 255, aPos, bPos, cPos);
}
}
if( ((movStates & 56) == 32 || (movStates & 56) == 56 ) && bPos == 0) {
for(int i = 1; i <= this.bMov; i++) {
if(curLengthB + i != curLengthA && curLengthB + i != curLengthC)
recursion(curLengthA, curLengthB + i, curLengthC, movStates & 479, aPos, bPos, cPos);
if(curLengthB - i != curLengthA && curLengthB - i != curLengthC)
recursion(curLengthA, curLengthB - i, curLengthC, movStates & 479, aPos, bPos, cPos);
}
}
if( ((movStates & 7) == 4 || (movStates & 7) == 7 ) && cPos == 0) {
for(int i = 1; i <= this.cMov; i++) {
if(curLengthC + i != curLengthB && curLengthC + i != curLengthA)
recursion(curLengthA, curLengthB, curLengthC + i, movStates & 507, aPos, bPos, cPos);
if(curLengthC - i != curLengthB && curLengthC - i != curLengthA)
recursion(curLengthA, curLengthB, curLengthC - i, movStates & 507, aPos, bPos, cPos);
}
} // lift
if(Math.abs(curLengthA - curLengthB) == 1 && (movStates & 128) == 128 && aPos == 0 && bPos == 0) // a lift b
recursion(curLengthA, curLengthA, curLengthC == curLengthB ? curLengthA : curLengthC, movStates & 383, 0, 1, cPos == 1 ? 2 : 0);
if(Math.abs(curLengthA - curLengthB) == 1 && (movStates & 16) == 16 && aPos == 0 && bPos == 0) // b lift a
recursion(curLengthB, curLengthB, curLengthC == curLengthA ? curLengthB : curLengthC, movStates & 495, 1, 0, cPos == 1 ? 2 : 0);
if(Math.abs(curLengthA - curLengthC) == 1 && (movStates & 128) == 128 && aPos == 0 && cPos == 0) // a lift c
recursion(curLengthA, curLengthB == curLengthC ? curLengthA : curLengthB, curLengthA, movStates & 383, 0, bPos == 1 ? 2 : 0, 1);
if(Math.abs(curLengthA - curLengthC) == 1 && (movStates & 2) == 2 && aPos == 0 && cPos == 0) // c lift a
recursion(curLengthC, curLengthB == curLengthA ? curLengthC : curLengthB, curLengthC, movStates & 509, 1, bPos == 1 ? 2 : 0, 0);
if(Math.abs(curLengthB - curLengthC) == 1 && (movStates & 16) == 16 && bPos == 0 && cPos == 0) // b lift c
recursion(curLengthA == curLengthC ? curLengthB : curLengthA, curLengthB, curLengthB, movStates & 495, aPos == 1 ? 2 : 0, 0, 1);
if(Math.abs(curLengthB - curLengthC) == 1 && (movStates & 2) == 2 && bPos == 0 && cPos == 0) // c lift b
recursion(curLengthA == curLengthB ? curLengthC : curLengthA, curLengthC, curLengthC, movStates & 509, aPos == 1 ? 2 : 0, 1, 0); //throw
if(aPos + bPos + cPos == 1) { int lifting = 0;
if((movStates & 24) == 8)
lifting = 1;
if((movStates & 3) == 1)
lifting = 2; int throwed = 0;
if(bPos == 1)
throwed = 1;
if(cPos == 1)
throwed = 2; if(lifting == 0 && throwed == 1) { //a throw b
for(int i = 1; i <= this.aThr; i++) {
if(i + curLengthB != curLengthC)
recursion(curLengthA, curLengthB + i, curLengthC, movStates & 447, 0, 0, 0);
if(i - curLengthB != curLengthC)
recursion(curLengthA, curLengthB - i, curLengthC, movStates & 447, 0, 0, 0);
}
}
if(lifting == 0 && throwed == 2) { //a throw c
for(int i = 1; i <= this.aThr; i++) {
if(i + curLengthC != curLengthB)
recursion(curLengthA, curLengthB, curLengthC + i, movStates & 447, 0, 0, 0);
if(i - curLengthC != curLengthB)
recursion(curLengthA, curLengthB, curLengthC - i, movStates & 447, 0, 0, 0);
}
}
if(lifting == 1 && throwed == 0) { //b throw a
for(int i = 1; i <= this.bThr; i++) {
if(i + curLengthA != curLengthC)
recursion(curLengthA + i, curLengthB, curLengthC, movStates & 503, 0, 0, 0);
if(i - curLengthA != curLengthC)
recursion(curLengthA - i, curLengthB, curLengthC, movStates & 503, 0, 0, 0);
}
}
if(lifting == 1 && throwed == 2) { //b throw c
for(int i = 1; i <= this.bThr; i++) {
if(i + curLengthC != curLengthA)
recursion(curLengthA, curLengthB, curLengthC + i, movStates & 503, 0, 0, 0);
if(i - curLengthC != curLengthA)
recursion(curLengthA, curLengthB, curLengthC - i, movStates & 503, 0, 0, 0);
}
}
if(lifting == 2 && throwed == 0) { //c throw a
for(int i = 1; i <= this.cThr; i++) {
if(i + curLengthA != curLengthB)
recursion(curLengthA + i, curLengthB, curLengthC, movStates & 510, 0, 0, 0);
if(i - curLengthA != curLengthB)
recursion(curLengthA - i, curLengthB, curLengthC, movStates & 510, 0, 0, 0);
}
}
if(lifting == 2 && throwed == 1) { //c throw b
for(int i = 1; i <= this.cThr; i++) {
if(i + curLengthB != curLengthA)
recursion(curLengthA, curLengthB + i, curLengthC, movStates & 510, 0, 0, 0);
if(i - curLengthB != curLengthA)
recursion(curLengthA, curLengthB - i, curLengthC, movStates & 510, 0, 0, 0);
}
}
}
else if(aPos + bPos + cPos == 3) { int throwing = 0;
if(bPos == 0)
throwing = 1;
if(cPos == 0)
throwing = 2; if(throwing == 0) {
for(int i = 1; i <= this.aThr; i++) {
recursion(curLengthA, curLengthB + i, curLengthC + i, movStates & 447, 0, bPos - 1, cPos - 1);
recursion(curLengthA, curLengthB - i, curLengthC - i, movStates & 447, 0, bPos - 1, cPos - 1);
}
}
if(throwing == 1) {
for(int i = 1; i <= this.bThr; i++) {
recursion(curLengthA + i, curLengthB, curLengthC + i, movStates & 503, aPos - 1, 0, cPos - 1);
recursion(curLengthA - i, curLengthB, curLengthC - i, movStates & 503, aPos - 1, 0, cPos - 1);
}
}
if(throwing == 2) {
for(int i = 1; i <= this.cThr; i++) {
recursion(curLengthA + i, curLengthB + i, curLengthC, movStates & 510, aPos - 1, bPos - 1, 0);
recursion(curLengthA - i, curLengthB - i, curLengthC, movStates & 510, aPos - 1, bPos - 1, 0);
}
}
}
}
private int max_3(int a, int b, int c) {
if(a >= b && a >= c)
return a;
else if(b >= a && b >= c)
return b;
else
return c;
} }

这道题目是蓝桥杯官网练习系统的一道题目,我目前没有找到什么巧解来解决这道题目,大致上只是用暴力列举每一种情况。

当然这样做的坏处就是要面对极多的逻辑控制。我这代码最终也没有拿全分数,有3组测试数据跑出了时间限制。而且由于逻辑的控制过于复杂(对我来说),在不知道测试数据的情况下慢慢自己debug到现在已经是最好的情况了。其实继续下去也许能解决这个问题,但实在太恶心,等一些高人拿出比较精辟的想法吧。

上一篇:angular指令(二)--内置指令


下一篇:页面初始化document.body.clientWidth大小变化