直接上模板吧,自己复习用:
#include<cstdio>
#include<cstring>
#include<iostream>
#incldue<algorithm>
using namespace std;
#define N 10000+5
char map[N][N];
bool visit[N][N];
int cnt=0;
int n, m;
int dx[4]={1, 0, -1, 0};
int dy[4]={0, 1, 0, -1};
int dfs(int x, int y){
visit[x][y]=1;
if(map[x][y]==G){
..... //达到目标状态
return ;
}
for(int i=0; i<4; i++){
int tx=x+dx[i];
int ty=y+dy[i];
if(tx>=0&&tx<n&&ty>=0&&ty<m&&!visit[tx][ty]){
dfs(tx, ty);
}
}
return ;
}
int main(){
for(int i=0; i<n; i++){
for(int j=0; j<n; i++){
scanf("%d", &map[i][j]);
}
}
...........
}
bfs模板链接:blog.csdn.net/icodeajk/article/details/50989473