</pre>简单的BFS<p></p><p></p><pre code_snippet_id="1633553" snippet_file_name="blog_20160403_1_1107701" name="code" class="cpp">#include <iostream>
#include<cstring>
#include<queue>
using namespace std;
const int dx[4] = {0,0,-1,1};
const int dy[4] = {-1,1,0,0};
struct node
{
int x,y;
int step;
};
int N,M;
node now;
node New;
node S,T;
char map[50][50];
bool vis[50][50];
int bfs()
{
memset(vis,false,sizeof(vis));
queue<node>q;
q.push(S);
vis[S.x][S.y] = true;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i=0; i<4; i++)
{
New.x = now.x + dx[i];
New.y = now.y + dy[i];
New.step = now.step + 1;
if(New.x>=N || New.y>=M || vis[New.x][New.y] || map[New.x][New.y] == '#')
continue;
vis[New.x][New.y] = true;
q.push(New);
if(New.x == T.x && New.y == T.y)
return New.step;
}
}
return -1;
}
int main()
{
cin >> N >> M;
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
cin >> map[i][j];
if(map[i][j] == 'S')
{
S.x = i;
S.y = j;
S.step = 0;
}
if(map[i][j] == 'G')
{
T.x = i;
T.y = j;
}
}
}
int ans = bfs();
if(ans<0)
cout << "trapped" << endl;
else
cout << ans << endl;
return 0;
}
/*
5 5
#..S#
..#..
.#...
..G##
.####
*/
<pre name="code" class="cpp"><p>这样写强行停止</p><p>#include<iostream></p>#include<cstring>
#include<queue>
using namespace std;
struct node
{
int x,y;
int t;
};
bool vis[50][50];
char map[50][50];///****************
node now;
node New;
node S,T;
int n,m;
const int dx[4] = {0,0,-1,1};
const int dy[4] = {1,-1,0,0};///*************
int bfs()
{
memset(vis,false,sizeof(vis));
queue<node>q;
q.push(S);
vis[S.x][S.y] = true;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i=0; i<4; i++)
{
New.x = now.x + dx[i];
New.y = now.y + dy[i];
New.t = now.t + 1;
if(New.x>=n || New.y>=m || vis[New.x][New.y] || map[New.x][New.y] == '#')
continue;
vis[New.x][New.y] = true;
q.push(New);
if(New.x == T.x && New.y == T.y)
return New.t;
}
}
return -1;
}
int main()
{
cin >> n >> m;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
cin >> map[i][j];
if(map[i][j] == 'S')
{
S.x = i;
S.y = j;
S.t = 0;
}
if(map[i][j] == 'G')
{
T.x = i;
T.y = j;
}
}
}
int ans = bfs();
if(ans < 0)
cout << "trapped" << endl;
else
cout << ans << endl;
return 0;
}
书上的解法 pair<int , int>
#include<iostream>
#include<queue>
using namespace std;
const int INF = 100000000;
const int maxn = 100;
typedef pair<int, int>P;
char map[maxn][maxn];
int N, M;
int sx, sy;
int gx, gy;
int d[maxn][maxn];
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {-1, 1, 0, 0};
int bfs()
{
queue<P>q;
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
d[i][j] = INF;
}
q.push(P(sx, sy));
d[sx][sy] = 0;
while(q.size())
{
P p = q.front();
q.pop();
if(p.first == gx && p.second == gy)
break;
for(int i=0; i<4; i++)
{
int nx = p.first + dx[i];
int ny = p.second + dy[i];
if(nx>=0 && nx<N && ny>=0 && ny<M && map[nx][ny]!='#' && d[nx][ny] == INF)
{
q.push(P(nx, ny));
d[nx][ny] = d[p.first][p.second] + 1;
}
}
}
return d[gx][gy];
}
int main()
{
cin >> N >>M;
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
cin >> map[i][j];
if(map[i][j] == 'S')
{
sx = i;
sy = j;
}
if(map[i][j] == 'G')
{
gx = i;
gy = j;
}
}
}
int ans = bfs();
cout << ans << endl;
return 0;
}