题目:.表示可以走的路,S表示出口,T是开始的位置,#是强,不能走。现在让你实现3个功能:
1:求是否可以从开始的位置到达出口,能得话输出Yes,不能的话输出No。
2:输出最少要走的步数。
3:输出要走的路线。
. S . . #
# . # . #
# . . . #
# . # . T
# . . . #
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
struct Point
{
int x, y, d;
};
int vis[100][100], dis[100][100],fy[100][100],fx[100][100];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int main()
{
char map[100][100];
queue <Point> myqueue;
stack <Point> mystack;
int m, n, i, j;
int sx, sy, ex, ey;
int xx, yy;
while(cin >> m >> n)
{
while(!myqueue.empty())
myqueue.pop();
while(!mystack.empty())
mystack.pop();
memset(vis,0,sizeof(vis));
memset(fx,0,sizeof(fx));
memset(fy,0,sizeof(fy));
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
cin >> map[i][j];
if(map[i][j] == 'S')
{
sx = j;
sy = i;
}
if(map[i][j] == 'T')
{
ex = j;
ey = i;
}
}
}
Point temp, cur;
temp.x = sx;
temp.y = sy;
temp.d = 0;
myqueue.push(temp);
while(!myqueue.empty())
{
cur = myqueue.front();
myqueue.pop();
for(i = 0; i < 4; i++)
{
yy = cur.y + dir[i][0];
xx = cur.x + dir[i][1];
if(map[yy][xx] != '#' && vis[yy][xx] == 0 && m > xx && xx >= 0 && n > yy && yy >= 0)
{
temp.x = xx;
temp.y = yy;
temp.d = cur.d + 1;
fy[yy][xx]=cur.y;
fx[yy][xx]=cur.x;
dis[yy][xx] = cur.d + 1;
myqueue.push(temp);
}
vis[cur.y][cur.x] = 1;
}
}
if(vis[ey][ex] == 0)
cout << "NO" << endl;
else
cout << "Yes" << endl;
cout << dis[ey][ex] << endl;
temp.x = ex;
temp.y = ey;
yy=ey;
xx=ex;
mystack.push(temp);
while(map[yy][xx]!='S')
{
temp.y=fy[yy][xx];
temp.x=fx[yy][xx];
mystack.push(temp);
yy=temp.y;
xx=temp.x;
}
while(!mystack.empty())
{
temp=mystack.top();
cout<<"("<<temp.x<<","<<temp.y<<")"<<endl;
mystack.pop();
}
}
return 0;
}
师哥的代码,功能比较强大。
#include <iostream>
#include <queue>
#include <cstring>
#include <stack>
#define clr(x) memset(x,0,sizeof(x))
const int MAXN = 20;
using namespace std;
typedef struct p
{
int x, y, fx,fy,d;
};
char g[MAXN][MAXN];
int d[MAXN][MAXN],fy[MAXN][MAXN],fx[MAXN][MAXN],vist[MAXN][MAXN];
int dir[][2]={{1,0},{0,1},{-1,0},{0,-1}};
int main()
{
int w, h, i, j, sx,sy,tx,ty, xx, yy;
queue <p> q;
stack <p> st;
p temp,cur;
while (cin>>h>>w)
{
clr(g);clr(d);clr(fx);clr(fy);clr(vist);
for (i=0; i<h; i++)
{
for (j=0; j<w; j++)
{
cin>>g[i][j];
if (g[i][j]=='s')
{
sy=i;
sx=j;
}
if (g[i][j]=='t')
{
ty=i;
tx=j;
}
}
}
if (!q.empty())
q.pop();
temp.x=sx;
temp.y=sy;
temp.d=0;
temp.fx=-1;
temp.fy=-1;
q.push(temp);
while (!q.empty())
{
cur = q.front();
q.pop();
for (i=0; i<4; i++)
{
yy = cur.y+ dir[i][0];
xx = cur.x+ dir[i][1];
if (vist[yy][xx] == 0 && g[yy][xx]!='#' && yy<h && yy>=0 && xx>=0 && xx<w)
{
d[yy][xx]=d[cur.y][cur.x] + 1;
temp.x=xx;
temp.y=yy;
temp.d=d[yy][xx];
fx[yy][xx] = cur.x;
fy[yy][xx] = cur.y;
q.push(temp);
}
}
vist[cur.y][cur.x] = 1;
}
if(vist[cur.y][cur.x]==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
cout<<"从S到达T的最少要"<<d[ty][tx]<<"步"<<endl;
cout<<"行走路线为:"<<endl;
temp.y=ty;
temp.x=tx;
yy=ty;
xx=tx;
st.push(temp);
while (g[yy][xx]!='s')
{
temp.y=fy[yy][xx];
temp.x=fx[yy][xx];
st.push(temp);
yy=temp.y;
xx=temp.x;
}
while (!st.empty())
{
temp = st.top();
printf("(%d,%d)\n",temp.y,temp.x);
st.pop();
}
for(i=0;i<h;i++)
{
for (j=0;j<w;j++)
{
if (g[i][j]!='#')
{
if (d[i][j]<10)
cout<<d[i][j];
else if(d[i][j]==10)
cout<<'A';
else if (d[i][j]==11)
cout<<"B";
else if (d[i][j]==12)
cout<<"C";
}
else cout<<g[i][j];
}
cout<<endl;
}
}
return 0;
}