把左右两边的青蛙位置互换。青蛙只能向前,不能退后,用鼠标点青蛙,青蛙就会向前跳,它只会向前跳一步或隔着一只青蛙向前跳一步
原游戏在这里点击打开链接
面试过程中的一个上机题:废话少说,上代码
- import java.util.ListIterator;
- import java.util.Stack;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 12-1-7
- * To change this template use File | Settings | File Templates.
- */
- public class Qc {
- private int[] a = {1, 1, 1, 0, 2, 2, 2};
- private Stack<Pos> step = new Stack<Pos>();
- public static void main(String[] args) {
- Qc qc = new Qc();
- qc.testNextStep();
- qc.printStack();
- }
- public void testNextStep() {
- Pos lastFailStep = null;
- int lastFrog = -1;
- int lastFrogStyle = 1;
- int lastFrogPos = -1;
- int lastStonePos = -1;
- int lastFrog1 = 0;
- int lastFrog2 = 2;
- while (true) {
- if (lastFailStep != null) {
- lastFrog = lastFailStep.getFrog();
- lastFrogStyle = lastFailStep.getFrogStyle();
- lastFrogPos = lastFailStep.getFrogPos();
- lastStonePos = lastFailStep.getStonePos();
- if (lastFrogStyle == 1) {
- lastFrog1 = lastFrog;
- lastFrog2 = 2;
- } else {
- lastFrog2 = lastFrog;
- lastFrog1 = 0;
- }
- }
- boolean flag1 = false;
- boolean flag2 = false;
- if (lastFrogStyle == 1) {
- for (int i = 2; i > -1; i--) {
- if (lastFrogStyle == 1 && lastFrog == i) {
- break;
- }
- int frog = i;
- int frogStyle = 1;
- int frogPos = getFrog(frog, frogStyle);
- int stonePos = getStone();
- boolean isChSuccess = chPos(frogPos, stonePos);
- if (isChSuccess) {
- step.push(new Pos(frogPos, stonePos, frog, frogStyle));
- } else
- continue;
- }
- flag1 = true;
- int lastTwo = -1;
- for (int i = 0; i < 3; i++) {
- if (lastFrogStyle == 2 && lastFrog == i) {
- lastTwo = i;
- break;
- }
- int frog = i;
- int frogStyle = 2;
- int frogPos = getFrog(frog, frogStyle);
- int stonePos = getStone();
- boolean isChSuccess = chPos(frogPos, stonePos);
- if (isChSuccess) {
- lastTwo = i;
- step.push(new Pos(frogPos, stonePos, frog, frogStyle));
- } else
- continue;
- }
- if (lastTwo == lastFrog2) flag2 = true;
- } else if (lastFrogStyle == 2) {
- for (int i = 0; i < 3; i++) {
- if (lastFrogStyle == 2 && lastFrog == i) {
- break;
- }
- int frog = i;
- int frogStyle = 2;
- int frogPos = getFrog(frog, frogStyle);
- int stonePos = getStone();
- boolean isChSuccess = chPos(frogPos, stonePos);
- if (isChSuccess) {
- step.push(new Pos(frogPos, stonePos, frog, frogStyle));
- } else
- continue;
- }
- flag2 = true;
- int lastOne = -1;
- for (int i = 2; i > -1; i--) {
- if (lastFrogStyle == 1 && lastFrog == i) {
- lastOne = i;
- break;
- }
- int frog = i;
- int frogStyle = 1;
- int frogPos = getFrog(frog, frogStyle);
- int stonePos = getStone();
- boolean isChSuccess = chPos(frogPos, stonePos);
- if (isChSuccess) {
- lastOne = i;
- step.push(new Pos(frogPos, stonePos, frog, frogStyle));
- } else
- continue;
- }
- if (lastOne == lastFrog1) flag1 = true;
- }
- if (checkSuccess())
- return;
- else {
- if (!(flag1 && flag2)) {
- lastFailStep = step.pop();
- restart();
- } else {
- lastFailStep = null;
- lastFrog = -1;
- lastFrogStyle = 1;
- lastFrogPos = -1;
- lastStonePos = -1;
- lastFrog1 = 0;
- lastFrog2 = 2;
- }
- //printA();
- }
- }
- }
- public void printStack(){
- a = new int[]{1, 1, 1, 0, 2, 2, 2};
- Stack tmpStack = new Stack();
- copyStack(step, tmpStack);
- ListIterator iterator = tmpStack.listIterator();
- step.clear();
- while (iterator.hasNext()) {
- Pos pos = (Pos) iterator.next();
- chPos(pos.getFrogPos(), pos.getStonePos());
- printA();
- step.push(pos);
- }
- }
- public int getFrog(int whichFrog, int whichStyle) {
- int count = 0;
- int pos = 0;
- for (int i = 0; i < a.length; i++) {
- if (a[i] == whichStyle) {
- if (count++ == whichFrog) {
- pos = i;
- }
- }
- }
- return pos;
- }
- public int getStone() {
- int pos = 0;
- for (int i = 0; i < a.length; i++) {
- if (a[i] == 0) {
- pos = i;
- }
- }
- return pos;
- }
- public boolean chPos(int frog, int stone) {
- if (Math.abs(frog - stone) > 2)
- return false;
- else if ((a[frog] == 1 && frog > stone) || (a[frog] == 2 && frog < stone))
- return false;
- else {
- int tmp = 0;
- tmp = a[frog];
- a[frog] = a[stone];
- a[stone] = tmp;
- return true;
- }
- }
- public void printA() {
- for (int i = 0; i < a.length; i++) {
- System.out.print(a[i]);
- }
- System.out.println();
- }
- public boolean checkSuccess() {
- if (a[0] == a[1] && a[1] == a[2] && a[1] == 2 && a[4] == a[5] && a[5] == a[6] && a[6] == 1) {
- return true;
- }
- return false;
- }
- public void restart() {
- a = new int[]{1, 1, 1, 0, 2, 2, 2};
- Stack tmpStack = new Stack();
- copyStack(step, tmpStack);
- ListIterator iterator = tmpStack.listIterator();
- step.clear();
- while (iterator.hasNext()) {
- Pos pos = (Pos) iterator.next();
- chPos(pos.getFrogPos(), pos.getStonePos());
- step.push(pos);
- }
- }
- public void copyStack(Stack<Pos> from, Stack<Pos> to) {
- ListIterator<Pos> iterator = from.listIterator();
- while (iterator.hasNext()) {
- to.push(iterator.next());
- }
- }
- }
- class Pos {
- private int frogPos;
- private int stonePos;
- private int frog;
- private int frogStyle;
- public Pos(int frogPos, int stonePos, int frog, int frogStyle) {
- this.frogPos = frogPos;
- this.stonePos = stonePos;
- this.frog = frog;
- this.frogStyle = frogStyle;
- }
- public int getFrogPos() {
- return frogPos;
- }
- public int getStonePos() {
- return stonePos;
- }
- public int getFrog() {
- return frog;
- }
- public int getFrogStyle() {
- return frogStyle;
- }
- }
初始状态:1,1,1,0,2,2,2
输出结果:
1101222
1121022
1121202
1120212
1021212
0121212
2101212
2121012
2121210
2121201
2120211
2021211
2201211
2221011
2220111
最终结果:2,2,2,0,1,1,1
转自:http://blog.csdn.net/wugui414/article/details/7183695