Java编程能力强化——狼羊过河问题

 题目:有3只狼和3只羊要过河,只有一条船,一次最多只能坐两只动物并且每次必须有动物开船,如果某一边的狼的个数大于羊的个数,羊将被吃掉,编程给出解。
关于编程思路,参考:Java编程能力强化(2)——搜索解决方案类问题的通用解法
参考答案:
package ch1;
public class LangAndYang {
 public static void main(String[] args) {
  int state[] = {3,3};
  // 第1、2个元素表示左岸的狼和羊的数量
  new LangAndYang().next(state,null);
 }
 
 public void next(int state[],StringBuffer str){
  int[] newState;
  if(str==null){ //表示第一步
   // 一只狼一只羊
   newState = move(state,"-1-1");
   next(newState,new StringBuffer("-1-1"));
   // 两只狼过河
   newState = move(state,"-2-0");
   next(newState,new StringBuffer("-2-0"));
   return;
  }
 
  if(state[0]==0 && state[1]==0){ // 全部转移到右岸了
   printResult(str);
   return;
  }
 
  if(str!=null && hasExist(str)){ // 看是否为死循环
   return;
  }
 
  // 考虑向右转移
  if(str.charAt(0)=='+'){
   // 两只狼
   if(state[0]>=2 && !str.substring(0,4).equals("+2+0")){
    newState = move(state,"-2-0");
    if(check(newState)){
     next(newState,new StringBuffer(str).insert(0,"-2-0"));
    }
   }
   // 一只狼
   if(state[0]>=1 && !str.substring(0,4).equals("+1+0")){
    newState = move(state,"-1-0");
    if(check(newState)){
     next(newState,new StringBuffer(str).insert(0,"-1-0"));
    }
   }
   // 一只羊
   if(state[1]>=1 && !str.substring(0,4).equals("+0+1")){
    newState = move(state,"-0-1");
    if(check(newState)){
     next(newState,new StringBuffer(str).insert(0,"-0-1"));
    }
   }
   // 一只狼一只羊
   if(state[0]>=1 && state[1]>=1 && !str.substring(0,4).equals("+1+1")){
    newState = move(state,"-1-1");
    if(check(newState)){
     next(newState,new StringBuffer(str).insert(0,"-1-1"));
    }
   }
   // 两只羊
   if(state[1]>=2 && !str.substring(0,4).equals("+0+2")){
    newState = move(state,"-0-2");
    if(check(newState)){
     next(newState,new StringBuffer(str).insert(0,"-0-2"));
    }
   }
  }else{  // 考虑向左转移
   // 两只狼
   if(state[0]<2 && !str.substring(0,4).equals("-2-0")){
    newState = move(state,"+2+0");
    if(check(newState)){
     next(newState,new StringBuffer(str).insert(0,"+2+0"));
    }
   }
   // 一只狼
   if(state[0]<3 && !str.substring(0,4).equals("-1-0")){
    newState = move(state,"+1+0");
    if(check(newState)){
     next(newState,new StringBuffer(str).insert(0,"+1+0"));
    }
   }
   // 一只羊
   if(state[1]<3 && !str.substring(0,4).equals("-0-1")){
    newState = move(state,"+0+1");
    if(check(newState)){
     next(newState,new StringBuffer(str).insert(0,"+0+1"));
    }
   }
   // 一只狼一只羊
   if(state[0]<3 && state[1]<3 && !str.substring(0,4).equals("-1-1")){
    newState = move(state,"+1+1");
    if(check(newState)){
     next(newState,new StringBuffer(str).insert(0,"+1+1"));
    }
   }
   // 两只羊
   if(state[1]<2 && !str.substring(0,4).equals("-0-2")){
    newState = move(state,"+0+2");
    if(check(newState)){
     next(newState,new StringBuffer(str).insert(0,"+0+2"));
    }
   }
  }
 }
 
 /*
  * 第一个参数表示状态,第二个参数表示走法,向右用-,向左用+
  * 返回值表示新的状态
  */
 public int[] move(int state[],String info){
  int lang = 0;
  try{
   lang = Integer.parseInt(info.substring(0,2));
  }catch(Exception e){
   lang = Integer.parseInt(info.substring(1,2));
  }
  int yang = 0;
  try{
   yang= Integer.parseInt(info.substring(2));
  }catch(Exception e){
   yang = Integer.parseInt(info.substring(3));
  }
  int[] result = new int[state.length];
  result[0] = state[0]+lang;
  result[1] = state[1]+yang;
  return result;
 }
 
 /*
  * 验证状态是否合适,狼的个数不能大于羊
  */
 public boolean check(int state[]){
  if(state[0]>state[1] && state[1]>0){ //左边有羊,并且狼比羊多
   return false;
  }else if(state[0]<state[1] && state[1]<3){ // 右边有羊,并且狼比羊多
   return false;
  }else
   return true;
 }
 
 /*
  * 防止死循环,例如 先过去一只狼一只羊,然后回来一只羊,然后再过去一只狼,然后回来两只狼,就回到初始状态了
  */
 public boolean hasExist(StringBuffer str){
  int langSum=0;
  int yangSum=0;
  for(int i=0;i<str.length()/4;i++){
   if(str.charAt(i*4)=='-'){
    langSum += str.charAt(i*4+1)-'0';
    yangSum += str.charAt(i*4+3)-'0';
   }else{
    langSum -= str.charAt(i*4+1)-'0';
    yangSum -= str.charAt(i*4+3)-'0';
   }
   if(langSum==0 && yangSum==0 && i%2==1)
    return true;
  }
  return false;
 }
 
 public void printResult(StringBuffer str){
  System.out.println("-----方案------");
  for(int i=str.length()/4-1;i>=0;i--){
   if(str.charAt(i*4)=='-'){
    System.out.println("运过去"+str.charAt(i*4+1)+"只狼,"+str.charAt(i*4+3)+"只羊");
   }else{
    System.out.println("---------------运回来"+str.charAt(i*4+1)+"只狼,"+str.charAt(i*4+3)+"只羊");
   }
  }
  System.out.println();
 }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值