问题描述
packagetest2;importjava.util.*;publicclassmigong{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannerin=newScanner(System.in);while(in.hasNext()){int[][]num1=newint[5][5];for(inti=0;i<5;i++){for(intj=0;j<5;j++){num1[i][j]=in.nextInt();}}test4(num1);}}publicstaticvoidtest4(int[][]num1){Vector<Num>nums=null;nums=newVector<Num>();System.out.println(nums.size());Numnum=null;num=newNum(0,0);nums.add(0,num);inti,j;while(true){NumtempNum=nums.get(0);i=tempNum.getA();j=tempNum.getB();Numtemp=null;if(tempNum.getFangxiang()==0&&i<4&&num1[i+1][j]==0){temp=newNum(i+1,j);}elseif(tempNum.getFangxiang()==1&&j<4&&num1[i][j+1]==0){temp=newNum(i,j+1);}elseif(tempNum.getFangxiang()==2&&i>0&&num1[i-1][j]==0){temp=newNum(i-1,j);}elseif(tempNum.getFangxiang()==3&&j>0&&num1[i][j-1]==0){temp=newNum(i,j-1);}intn=tempNum.getFangxiang();tempNum.setFangxiang(n++);if(temp!=null){if(nums.size()>1){Numnumtemp=nums.get(1);if(numtemp.getA()==temp.getA()&&numtemp.getB()==temp.getB()){nums.remove(tempNum);i=numtemp.getA();j=numtemp.getB();continue;}}nums.add(0,num);i=num.getA();j=num.getB();}if(i==4&&j==4)break;}nums.add(0,newNum(4,4));System.out.println(nums.get(0).getA());for(intk=nums.size()-1;k>=0;k--){Numtemp=nums.get(k);System.out.println("("+temp.getA()+","+temp.getB()+")");}}}classNum{privateinta=0;privateintb=0;privateintfangxiang=0;publicintgetA(){returna;}publicvoidsetA(inta){this.a=a;}publicintgetB(){returnb;}publicvoidsetB(intb){this.b=b;}publicintgetFangxiang(){returnfangxiang;}publicvoidsetFangxiang(intfangxiang){this.fangxiang=fangxiang;}publicNum(inta,intb){this.a=a;this.b=b;}}//见解请发我QQ邮箱1670396619@qq.com