public class Main{
public boolean check(char[][]graph,int[][]lable,int[]points){
int x1=points[0];
int y1=points[1];
int x2=points[2];
int y2=points[3];
if(x1==x2&&y1==y2)return true;//起点终点重合返回true
char value=graph[x1][y1];
boolean f1=false;
boolean f2=false;
boolean f3=false;
boolean f4=false;
if(x1-1>=0&&lable[x1-1][y1]==0&&graph[x1-1][y1]==value){//往左走
points[0]=x1-1;
lable[x1-1][y1]=1;
f1=check(graph,lable,points);//递归
//回溯
points[0]=x1;
lable[x1-1][y1]=0;
}
if(x1+1>=0&&lable[x1+1][y1]==0&&graph[x1+1][y1]==value){//往上走
points[0]=x1+1;
lable[x1+1][y1]=1;
f2=check(graph,lable,points);//递归
//回溯
points[0]=x1;
lable[x1+1][y1]=0;
}
if(y1-1>=0&&lable[x1][y1-1]==0&&graph[x1][y1-1]==value){//往上走
points[1]=y1-1;
lable[x11][y1-1]=1;
f3=check(graph,lable,points);//递归
//回溯
points[1]=y1;
lable[x1][y1-1]=0;
}
if(y1+1>=0&&lable[x1][y1+1]==0&&graph[x1][y1+1]==value){//往下走
points[1]=y1+1;
lable[x11][y1+1]=1;
f4=check(graph,lable,points);//递归
//回溯
points[1]=y1;
lable[x1][y1+1]=0;
}
return f1||f2||f3||f4;
}
}
图_四连通检测
最新推荐文章于 2024-08-24 22:01:16 发布