/**
<挑战>p34::迷宫的最短路径:
给定一个N*M的迷宫。迷宫由通道和墙壁组成, 每一步可以向邻接的上下左右四格的通道移动。
请求出从起点到终点所需的最小步数。
请注意, 本题假定从起点一定可以移动到终点。
限制条件:N, M <= 100;
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <algorithm>
#include <queue>
using namespace std;
int n, m;
int sx, sy, gx, gy;
char maze[110][110];
int vis[110][110];
struct node
{
int x;
int y;
int roadl;
};
queue<node> q;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void playMaze()
{
node p, cc;
p.x = sx;
p.y = sy;
p.roadl = 0;
q.push(p);
vis[sx][sy] = 1;
while (!q.empty ())
{
cc = q.front ();
q.pop();
for (int i=0; i<4; i++)
{
p.x = cc.x + dir[i][0];
p.y = cc.y + dir[i][1];
if (p.x>=0 && p.x<n && p.y>=0 && p.y<m)
{
if ('.' == maze[p.x][p.y] && !vis[p.x][p.y])
{
p.roadl = cc.roadl+1;
q.push(p);
vis[p.x][p.y] = 1;
}
if ('G' == maze[p.x][p.y])
{
printf("%d\n", cc.roadl+1);
return ;
}
}
}
}
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
memset(maze, 0, sizeof(maze));
memset(vis, 0, sizeof(vis));
getchar();
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
scanf("%c", &maze[i][j]);
}
getchar();
}
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
if ('S' == maze[i][j])
{
sx = i;
sy = j;
}
if ('G' == maze[i][j])
{
gx = i;
gy = j;
}
}
}
playMaze();
}
return 0;
}
/*
10 10
#S######.#
......#..#
.#.##.##.#
.#........
##.##.####
....#....#
.#######.#
....#.....
.####.###.
....#...G#
*/
总以为自己什么都不知道, 原来是对自己的一种不自信,,其实踏实一点也挺好的, 骚年, 继续加油塞~~