62,614
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
F(10,10);
}
//n m 棋盘
public static void F(int n,int m) {
char[][] charset=new char[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
charset[i][j]= (char)(Math.random()*26+97);
System.out.print(charset[i][j]);
}
System.out.println();
}
select(new char[][] {new char[] {'l','c','t','t','x','d','y'}});
}
static LinkedList<String> string1=new LinkedList<String>();
//1 棋盘 x y a当前下标 cha 查找的字符
public static void Seach(char[][] qi,int x,int y,int a,char[] cha) {
if(a==cha.length-1) {
System.out.println("成功。。。。。。");
string1.forEach(e->System.out.println(e));
System.exit(0);
return ;
}
String ok=isok(qi,x,y,cha[a]);
if(ok!=null) {
String [] s=ok.split(",");
string1.add(ok);
Seach(qi,Integer.parseInt(s[0]),Integer.parseInt(s[1]),a+1,cha);
string1.removeLast();
}
}
public static void select(char[][] qi) {
for (int i = 0; i < qi.length; i++) {
for (int j = 0; j < qi[0].length; j++) {
Seach(qi,i,j,0,new char[] {'l','c','t','t','x','d','y'});
}
}
}
//查询 并返回 坐标
public static String isok(char[][] qi,int x,int y,char a) {
if(ishh(qi,x+1,y,a)) return (x+1)+","+y;
if(ishh(qi,x-1,y,a)) return (x-1)+","+y;
if(ishh(qi,x-1,y+1,a)) return (x-1)+","+(y+1);
if(ishh(qi,x-1,y-1,a)) return (x-1)+","+(y-1);
if(ishh(qi,x+1,y+1,a)) return (x+1)+","+(y+1);
if(ishh(qi,x+1,y-1,a)) return (x+1)+","+(y-1);
if(ishh(qi,x,y+1,a)) return x+","+(y+1);
if(ishh(qi,x,y-1,a)) return x+","+(y-1);
return null;
}
public static boolean ishh(char [][]qi,int x,int y,char a) {
try {
return qi[x][y]==a;
} catch (Exception e) {
return false;
}
}
select 改成上面随机生成的就行了 不过看脸 哎。。。。