回复 2楼 rjsp
我写了下迷宫代码,但是我不会标记障碍物连着障碍物,而且迷宫走的也有问题,请指教
#include<string.h>
#include<stdio.h>
int n,m;
typedef struct{
int x; int y;
}pos;
int sx,sy,gx,gy;
const int base = -1;
char maze[105][105];
int d[105][105];
pos c[10005];
int dx[4] = {1,0,-1,0},dy[4]={0,1,0,-1};
int bfs(int n,int m){
for( int i = 1 ; i <= n ; i++)
{
for( int j = 1 ; j <= m ; j++)
{
d[i][j] = base;
scanf(" %c", &maze[i][j]);
if(maze[i][j] == 'S'){
sx = i;
sy = j;
}
if(maze[i][j] == 'T'){
gx = i;
gy = j;
}
}
}
d[sx][sy] = 0;
int l = 0,r = 1;
while(l < r){
sx = c[l].x;
sy = c[l].y;
l++;
if(sx == gx && sy == gy)break;
for(int i = 0 ; i < 4 ; i++){
int nx = sx + dx[i],ny = sy + dy[i];
if(0 <= nx && nx < m && 0 <= ny && ny < n && maze[nx][ny] != '#' && d[nx][ny] == base){
maze[nx][ny] = 0;
//d[nx][ny] = d[sx][sy] + 1;
c[r].x = nx;
c[r].y = ny;
r++;
}
if(0 <= nx && nx < m && 0 <= ny && ny < n && maze[nx][ny] == '#'){
maze[nx][ny] = 1;
c[r].x = nx;
c[r].y = ny;
r++;
}
}
}
return d[gx][gy];
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
int ans = bfs(n,m);
printf("%d\n",ans);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) {
printf("%d ", maze[i][j]);
}
printf("\n");
}
return 0;
}
}