原题
题目大意
走迷宫,连n和m都确定了,注意起点和终点不用加进去
题目分析
略
代码
#include<cstdio>
#include<cstring>
int const n = 10;
int const dirx[4] = {1,-1,0,0};
int const diry[4] = {0,0,1,-1};
int map[15][15];
int bx,by,ex,ey;
int fx[501],fy[501],dis[501],fs,ft;
bool been[15][15];
int ans = 0;
int main()
{
char ch;
memset(map,0xff,sizeof(map));
for (int i = 1;i <= n;++i)
{
for (int j = 1;j <= n;++j)
switch (getchar())
{
case '.':
map[i][j] = 1;
break;
case 'R':
map[i][j] = -1;
break;
case 'B':
ex = i,ey = j;
map[i][j] = -1;
break;
case 'L':
bx = i,by = j;
map[i][j] = -1;
break;
default:
break;
}
getchar();
}
fs = 0,ft = 0;
fx[ft] = bx,fy[ft] = by,dis[ft++] = 0;
while (fs < ft)
{
for (int i = 0;i < 4;++i)
{
int tx = fx[fs] + dirx[i],ty = fy[fs] + diry[i];
if (tx == ex and ty == ey)
{
ans = dis[fs];
fs = ft + 10;
break;
}
if (map[tx][ty] == -1) continue;
if (been[tx][ty]) continue;
been[tx][ty] = true;
fx[ft] = tx,fy[ft] = ty,dis[ft++] = dis[fs] + 1;
}
++fs;
}
printf("%d",ans);
return 0;
}