Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 882|回复: 0

[算法学习]农夫过河问题(深度优先算法)java版

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-12-1 00:05:53 | 显示全部楼层 |阅读模式
    农夫过河问题dfs算法的java版本
    自己做的不足之处希望指出.232136813@qq.com
    1. public class manriver {
    2.        private int[][] maxtri=new int[16][16];//邻接矩阵
    3.        private int[][] order={{0,0},
    4.                               {1,0},
    5.                               {2,0},
    6.                               {3,0},
    7.                               {4,0},
    8.                               {5,0},
    9.                               {6,0},
    10.                               {7,0},
    11.                               {8,0},
    12.                               {9,0},
    13.                               {10,0},
    14.                               {11,0},
    15.                               {12,0},
    16.                               {13,0},
    17.                               {14,0},
    18.                               {15,0}};// 状态是否被访问过的数组
    19.         private stack stack=new stack();
    20.        
    21.         /**
    22.          * judge the adjacency matrix elements true or false(1 or 0)
    23.          *
    24.          */
    25.        
    26.   private boolean isConnected(int x,int y){
    27.         String X=getformatString(x);
    28.         String Y=getformatString(y);
    29.        
    30.        if(X.charAt(0)==Y.charAt(0))
    31.                 return false;
    32.         else{
    33.           if(X.charAt(1)!=Y.charAt(1)&&X.charAt(2)!=Y.charAt(2)&&X.charAt(3)!=Y.charAt(3)){
    34.                 return false;
    35.            }
    36.           else if(X.charAt(1)!=Y.charAt(1)&&X.charAt(2)!=Y.charAt(2)){
    37.                 return false;
    38.           }
    39.           else if(X.charAt(1)!=Y.charAt(1)&&X.charAt(3)!=Y.charAt(3)){
    40.                  return false;
    41.           }
    42.           else if(X.charAt(2)!=Y.charAt(2)&&X.charAt(3)!=Y.charAt(3)){
    43.                  return false;
    44.           }
    45.           else if(((X.charAt(0)!=X.charAt(1))&&(X.charAt(1)!=Y.charAt(1)))||((X.charAt(0)!=X.charAt(2))&&
    46.                 (X.charAt(2)!=Y.charAt(2)))||((X.charAt(0)!=X.charAt(3))&&(X.charAt(3)!=Y.charAt(3)))){
    47.                         return false;
    48.                 }
    49.                 return true;
    50.         }
    51.         }
    52.         /**
    53.          *  
    54.          *  produce the adjacency matrix
    55.          *
    56.          */
    57.         public void makeMaxtri(){
    58.                 for(int i=0;i< 16;i++){
    59.                         for(int j=0;j< 16;j++){
    60.                                 if(isConnected(i, j))
    61.                                 {        maxtri[i][j]=1;
    62.                                 }
    63.                                 else maxtri[i][j]=0;
    64.                         }
    65.                 }
    66.         }
    67.         /**
    68.          *
    69.          * 得到状态的字符串表示
    70.          * 0000  四位分别代表人 羊 草 狼
    71.          *
    72.          * */
    73.         public String getformatString(int x){
    74.                
    75.                 String X=Integer.toBinaryString(x);
    76.                 if(X.length()< 4&&X.length()>=3){
    77.                         X="0"+X;
    78.                 }
    79.                 else if(X.length()< 3&&X.length()>=2){
    80.                         X="00"+X;
    81.                 }
    82.                 else if(X.length()< 2&&X.length()>=1){
    83.                         X="000"+X;
    84.                 }
    85.                 return X;
    86.         }
    87.        
    88.         /**
    89.          *  
    90.          *  dfs arithmetic
    91.          *  dfs算法
    92.          *
    93.          */
    94.   public void dfs(){
    95.         stack.push(0);
    96.         order[0][1]=1;
    97.         while(!stack.isEmpty()){       
    98.                 if(stack.peek()==15){
    99.                         break;
    100.                 }
    101.                 int v=getUnvisitedVetex(stack.peek());
    102.                 if(v==-1){
    103.                         try{stack.pop();
    104.                         }catch(Exception e){
    105.                                 e.printStackTrace();
    106.                         }
    107.                 }
    108.                 else{
    109.                
    110.                         stack.push(v);
    111.                         order[v][1]=1;
    112.                 }
    113.         }                               
    114.    }
    115.         /**
    116.          *
    117.          *得到与输入节点相连的一个结点
    118.          *  
    119.          */
    120.    public int getUnvisitedVetex(int x){
    121.         for(int j=0;j< 16;j++){
    122.        
    123.          if(maxtri[x][j]==1&&#8500;[j][1]==0){
    124.                                
    125.            String X=getformatString(j);
    126.            //合法性判断
    127.            if((X.charAt(0)!=X.charAt(1)&&X.charAt(1)==X.charAt(3))||
    128.                  (X.charAt(0)!=X.charAt(1)&&X.charAt(1)==X.charAt(2))){
    129.                 continue;       
    130.             }
    131.             else {
    132.                 return j;}
    133.             }
    134.          }
    135.         return -1;
    136.     }
    137.         /**
    138.          *
    139.          *  make order
    140.          *
    141.          */
    142. public void printOrder() throws Exception{
    143.   for(int i=0;i< stack.length()-1;i++){
    144.         int x=stack.peekByIndex(i);
    145.         int y=stack.peekByIndex(i+1);
    146.         String X=getformatString(x);
    147.         String Y=getformatString(y);
    148.         String type="";
    149.         if(X.charAt(0)=="0"){
    150.                 type="过河";
    151.         }
    152.         if(X.charAt(0)=="1"){
    153.                 type="回来";
    154.         }
    155.         if(X.charAt(1)!=Y.charAt(1)){
    156.                 System.out.println("人带羊"+type);
    157.         }
    158.         else if(X.charAt(2)!=Y.charAt(2)){
    159.                 System.out.println("人带草"+type);
    160.         }
    161.         else if(X.charAt(3)!=Y.charAt(3)){
    162.                 System.out.println("人带狼"+type);
    163.         }
    164.         else{
    165.                 System.out.println("人自己"+type);
    166.         }
    167.   }
    168. }
    169.         public static void main(String[] args){
    170.                 manriver manriver=new manriver();
    171.                 manriver.makeMaxtri();
    172.                 manriver.dfs();
    173.                 try{
    174.                         manriver.printOrder();
    175.                 }catch(Exception e){
    176.                         e.printStackTrace();
    177.                 }
    178.         }
    179. }
    180. /**
    181. *
    182. * 堆栈简单实现类
    183. *
    184. */
    185. class stack{
    186.         private int[] num;
    187.         private int value;
    188.         public stack(){
    189.                 num=new int[20];
    190.                 value=-1;
    191.         }
    192.         public int peek(){
    193.        
    194.                 return num[value];
    195.         }
    196.         public int pop() throws Exception{
    197.                 if(value>=0){
    198.                         return num[value--];
    199.                 }
    200.                 else throw new Exception("无元素!");
    201.                
    202.         }
    203.         public void push(int xx){
    204.                 if(value==-1){
    205.                         value=0;
    206.                         num[value]=xx;
    207.                 }
    208.                 else{
    209.                         num[++value]=xx;
    210.                 }       
    211.         }
    212.         public int getSize(){
    213.                 return value;
    214.         }
    215.         public boolean isEmpty(){
    216.                 return(value< 0);
    217.         }
    218.         public int length(){
    219.                 return value+1;
    220.         }
    221.         public int peekByIndex(int i)throws Exception{
    222.                 if(i< value+1&&i>=0){
    223.                 return num[i];
    224.                 }
    225.         else throw new Exception("未找到合适元素!");
    226.         }
    227.        
    228. }
    复制代码
    C:        est>java manriver
    人带羊过河
    人自己回来
    人带狼过河
    人带羊回来
    人带草过河
    人自己回来
    人带羊过河

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/12/1/000552781.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 04:15 , Processed in 0.352820 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表