左右两边的青蛙位置互换--面试编程题

5 篇文章 0 订阅
2 篇文章 0 订阅

把左右两边的青蛙位置互换。青蛙只能向前,不能退后,用鼠标点青蛙,青蛙就会向前跳,它只会向前跳一步或隔着一只青蛙向前跳一步


原游戏在这里点击打开链接

面试过程中的一个上机题:废话少说,上代码

[java]  view plain copy
  1. import java.util.ListIterator;  
  2. import java.util.Stack;  
  3.   
  4. /** 
  5.  * Created by IntelliJ IDEA. 
  6.  * User: Administrator 
  7.  * Date: 12-1-7 
  8.  * To change this template use File | Settings | File Templates. 
  9.  */  
  10. public class Qc {  
  11.     private int[] a = {1110222};  
  12.     private Stack<Pos> step = new Stack<Pos>();  
  13.   
  14.     public static void main(String[] args) {  
  15.         Qc qc = new Qc();  
  16.         qc.testNextStep();  
  17.         qc.printStack();  
  18.     }  
  19.   
  20.     public void testNextStep() {  
  21.         Pos lastFailStep = null;  
  22.         int lastFrog = -1;  
  23.         int lastFrogStyle = 1;  
  24.         int lastFrogPos = -1;  
  25.         int lastStonePos = -1;  
  26.         int lastFrog1 = 0;  
  27.         int lastFrog2 = 2;  
  28.         while (true) {  
  29.             if (lastFailStep != null) {  
  30.                 lastFrog = lastFailStep.getFrog();  
  31.                 lastFrogStyle = lastFailStep.getFrogStyle();  
  32.                 lastFrogPos = lastFailStep.getFrogPos();  
  33.                 lastStonePos = lastFailStep.getStonePos();  
  34.                 if (lastFrogStyle == 1) {  
  35.                     lastFrog1 = lastFrog;  
  36.                     lastFrog2 = 2;  
  37.                 } else {  
  38.                     lastFrog2 = lastFrog;  
  39.                     lastFrog1 = 0;  
  40.                 }  
  41.             }  
  42.   
  43.             boolean flag1 = false;  
  44.             boolean flag2 = false;  
  45.   
  46.             if (lastFrogStyle == 1) {  
  47.                 for (int i = 2; i > -1; i--) {  
  48.                     if (lastFrogStyle == 1 && lastFrog == i) {  
  49.                         break;  
  50.                     }  
  51.                     int frog = i;  
  52.                     int frogStyle = 1;  
  53.                     int frogPos = getFrog(frog, frogStyle);  
  54.                     int stonePos = getStone();  
  55.                     boolean isChSuccess = chPos(frogPos, stonePos);  
  56.                     if (isChSuccess) {  
  57.                         step.push(new Pos(frogPos, stonePos, frog, frogStyle));  
  58.                     } else  
  59.                         continue;  
  60.                 }  
  61.                 flag1 = true;  
  62.   
  63.                 int lastTwo = -1;  
  64.                 for (int i = 0; i < 3; i++) {  
  65.                     if (lastFrogStyle == 2 && lastFrog == i) {  
  66.                         lastTwo = i;  
  67.                         break;  
  68.                     }  
  69.                     int frog = i;  
  70.                     int frogStyle = 2;  
  71.                     int frogPos = getFrog(frog, frogStyle);  
  72.                     int stonePos = getStone();  
  73.                     boolean isChSuccess = chPos(frogPos, stonePos);  
  74.                     if (isChSuccess) {  
  75.                         lastTwo = i;  
  76.                         step.push(new Pos(frogPos, stonePos, frog, frogStyle));  
  77.                     } else  
  78.                         continue;  
  79.                 }  
  80.                 if (lastTwo == lastFrog2) flag2 = true;  
  81.             } else if (lastFrogStyle == 2) {  
  82.                 for (int i = 0; i < 3; i++) {  
  83.                     if (lastFrogStyle == 2 && lastFrog == i) {  
  84.                         break;  
  85.                     }  
  86.                     int frog = i;  
  87.                     int frogStyle = 2;  
  88.                     int frogPos = getFrog(frog, frogStyle);  
  89.                     int stonePos = getStone();  
  90.                     boolean isChSuccess = chPos(frogPos, stonePos);  
  91.                     if (isChSuccess) {  
  92.                         step.push(new Pos(frogPos, stonePos, frog, frogStyle));  
  93.                     } else  
  94.                         continue;  
  95.                 }  
  96.                 flag2 = true;  
  97.   
  98.                 int lastOne = -1;  
  99.                 for (int i = 2; i > -1; i--) {  
  100.                     if (lastFrogStyle == 1 && lastFrog == i) {  
  101.                         lastOne = i;  
  102.                         break;  
  103.                     }  
  104.                     int frog = i;  
  105.                     int frogStyle = 1;  
  106.                     int frogPos = getFrog(frog, frogStyle);  
  107.                     int stonePos = getStone();  
  108.                     boolean isChSuccess = chPos(frogPos, stonePos);  
  109.                     if (isChSuccess) {  
  110.                         lastOne = i;  
  111.                         step.push(new Pos(frogPos, stonePos, frog, frogStyle));  
  112.                     } else  
  113.                         continue;  
  114.                 }  
  115.                 if (lastOne == lastFrog1) flag1 = true;  
  116.             }  
  117.   
  118.             if (checkSuccess())  
  119.                 return;  
  120.             else {  
  121.                 if (!(flag1 && flag2)) {  
  122.                     lastFailStep = step.pop();  
  123.                     restart();  
  124.                 } else {  
  125.                     lastFailStep = null;  
  126.                     lastFrog = -1;  
  127.                     lastFrogStyle = 1;  
  128.                     lastFrogPos = -1;  
  129.                     lastStonePos = -1;  
  130.                     lastFrog1 = 0;  
  131.                     lastFrog2 = 2;  
  132.                 }  
  133.                 //printA();  
  134.             }  
  135.         }  
  136.     }  
  137.   
  138.     public void printStack(){  
  139.         a = new int[]{1110222};  
  140.         Stack tmpStack = new Stack();  
  141.         copyStack(step, tmpStack);  
  142.         ListIterator iterator = tmpStack.listIterator();  
  143.         step.clear();  
  144.         while (iterator.hasNext()) {  
  145.             Pos pos = (Pos) iterator.next();  
  146.             chPos(pos.getFrogPos(), pos.getStonePos());  
  147.             printA();  
  148.             step.push(pos);  
  149.         }  
  150.     }  
  151.   
  152.     public int getFrog(int whichFrog, int whichStyle) {  
  153.         int count = 0;  
  154.         int pos = 0;  
  155.         for (int i = 0; i < a.length; i++) {  
  156.             if (a[i] == whichStyle) {  
  157.                 if (count++ == whichFrog) {  
  158.                     pos = i;  
  159.                 }  
  160.             }  
  161.         }  
  162.         return pos;  
  163.     }  
  164.   
  165.     public int getStone() {  
  166.         int pos = 0;  
  167.         for (int i = 0; i < a.length; i++) {  
  168.             if (a[i] == 0) {  
  169.                 pos = i;  
  170.             }  
  171.         }  
  172.         return pos;  
  173.     }  
  174.   
  175.     public boolean chPos(int frog, int stone) {  
  176.         if (Math.abs(frog - stone) > 2)  
  177.             return false;  
  178.         else if ((a[frog] == 1 && frog > stone) || (a[frog] == 2 && frog < stone))  
  179.             return false;  
  180.         else {  
  181.             int tmp = 0;  
  182.             tmp = a[frog];  
  183.             a[frog] = a[stone];  
  184.             a[stone] = tmp;  
  185.             return true;  
  186.         }  
  187.     }  
  188.   
  189.     public void printA() {  
  190.         for (int i = 0; i < a.length; i++) {  
  191.             System.out.print(a[i]);  
  192.         }  
  193.         System.out.println();  
  194.     }  
  195.   
  196.     public boolean checkSuccess() {  
  197.         if (a[0] == a[1] && a[1] == a[2] && a[1] == 2 && a[4] == a[5] && a[5] == a[6] && a[6] == 1) {  
  198.             return true;  
  199.         }  
  200.         return false;  
  201.     }  
  202.   
  203.     public void restart() {  
  204.         a = new int[]{1110222};  
  205.         Stack tmpStack = new Stack();  
  206.         copyStack(step, tmpStack);  
  207.         ListIterator iterator = tmpStack.listIterator();  
  208.         step.clear();  
  209.         while (iterator.hasNext()) {  
  210.             Pos pos = (Pos) iterator.next();  
  211.             chPos(pos.getFrogPos(), pos.getStonePos());  
  212.             step.push(pos);  
  213.         }  
  214.     }  
  215.   
  216.     public void copyStack(Stack<Pos> from, Stack<Pos> to) {  
  217.         ListIterator<Pos> iterator = from.listIterator();  
  218.         while (iterator.hasNext()) {  
  219.             to.push(iterator.next());  
  220.         }  
  221.     }  
  222. }  
  223.   
  224. class Pos {  
  225.     private int frogPos;  
  226.     private int stonePos;  
  227.     private int frog;  
  228.     private int frogStyle;  
  229.   
  230.     public Pos(int frogPos, int stonePos, int frog, int frogStyle) {  
  231.         this.frogPos = frogPos;  
  232.         this.stonePos = stonePos;  
  233.         this.frog = frog;  
  234.         this.frogStyle = frogStyle;  
  235.     }  
  236.   
  237.     public int getFrogPos() {  
  238.         return frogPos;  
  239.     }  
  240.   
  241.     public int getStonePos() {  
  242.         return stonePos;  
  243.     }  
  244.   
  245.     public int getFrog() {  
  246.         return frog;  
  247.     }  
  248.   
  249.     public int getFrogStyle() {  
  250.         return frogStyle;  
  251.     }  
  252. }  


初始状态: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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值