测试数据
0 1 0 0 0 0 0
1 0 0 0 0 0 0
0 0 0 1 1 0 0
0 0 1 0 0 1 0
0 0 1 0 0 0 1
0 0 0 1 0 0 0
0 0 0 0 1 0 0
类似于深度优先遍历 随即找一个未访问过的点,寻找与其相邻的第二个点;最后判断是否访问过所有点;代码如下:
#include<stdio.h>
#define M 7
#define N 7
void init(int map[][N]){
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
scanf("%d",&map[i][j]);
}
}
}
void show(int map[][N]){
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
printf("%d",map[i][j]);
}
}
}
void prcos(int map[][N],int &sum,int &temp){
// int flag=1;
// while(flag&&){
for(int j=0;j<N;j++){
// flag=0;
if(map[temp][j]==1){
// flag=1;
sum++;
for(int i=0;i<M;i++){//找到新的1,则将此列全部设为0;表示此点已经走到过
map[i][j]=0;
}
prcos(map,sum,j);
}
}
// }
}
void main(){
int map[M][N];
init(map);
show(map);
int temp=0;
int sum=0;
prcos(map,sum,temp);
printf("%d\n",sum);
show(map);
}