BFS用栈弹出路径
来源:互联网 发布:矩阵的秩的物理意义 编辑:程序博客网 时间:2024/06/10 22:06
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
struct p
{
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 g[100][100];
queue <p> q;
stack <p> s;
int m, n, i, j;
int sx, sy, ex, ey;
int xx, yy;
cin >> m >> n;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
cin >> g[i][j];
vis[i][j] = 0;
}
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if(g[i][j] == 's')
{
sx = j;
sy = i;
}
if(g[i][j] == 't')
{
ex = j;
ey = i;
}
}
}
p temp, cur;
temp.x = sx;
temp.y = sy;
temp.d = 0;
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(g[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;
q.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;
s.push(temp);
while(g[yy][xx]!='s')
{
temp.y=fy[yy][xx];
temp.x=fx[yy][xx];
s.push(temp);
yy=temp.y;
xx=temp.x;
}
while(!s.empty())
{
temp=s.top();
cout<<"("<<temp.x<<","<<temp.y<<")"<<endl;
s.pop();
}
return 0;
}
#include <stack>
#include <queue>
using namespace std;
struct p
{
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 g[100][100];
queue <p> q;
stack <p> s;
int m, n, i, j;
int sx, sy, ex, ey;
int xx, yy;
cin >> m >> n;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
cin >> g[i][j];
vis[i][j] = 0;
}
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if(g[i][j] == 's')
{
sx = j;
sy = i;
}
if(g[i][j] == 't')
{
ex = j;
ey = i;
}
}
}
p temp, cur;
temp.x = sx;
temp.y = sy;
temp.d = 0;
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(g[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;
q.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;
s.push(temp);
while(g[yy][xx]!='s')
{
temp.y=fy[yy][xx];
temp.x=fx[yy][xx];
s.push(temp);
yy=temp.y;
xx=temp.x;
}
while(!s.empty())
{
temp=s.top();
cout<<"("<<temp.x<<","<<temp.y<<")"<<endl;
s.pop();
}
return 0;
}
- BFS用栈弹出路径
- hdu1026 BFS+记录路径
- BFS 求最短路径问题
- Pots BFS 记忆路径
- bfs+路径记录
- BFS(简单路径打印)
- bfs最短路径
- BFS如何记录路径
- BFS-记录路径
- POJ (BFS + 路径输出)
- POJ3984 BFS 输出路径
- 搜索 bfs 路径
- BFS输出路径
- BFS + 回溯路径 POJ
- BFS最短路路径
- hdu1026 bfs+记录路径
- bfs路径打印
- 【BFS + 保存路径】UVA
- 查询不同数据库表前几条数据的方式
- C++学习之六、有效的内存管理
- 站点导航
- 从字符串中分离找出每个单词,并且寻找某个特定的单词
- 浪潮优派培训笔记:oracle笔记
- BFS用栈弹出路径
- jsp,图片形式的登录校验码
- ActivityGroup使用问题
- android Dialog 各种Dialog 三个选项的、进度条的、单选的、多选的、自定义的对话框
- 小感
- SDL库的安装,spca5xx的安装,spcaview 的安装,摄像头的查看,及常见问题的解决方法。
- [Android]使用ActivityGroup来切换Activity和Layout
- Windows 下安装Redmine 1.1.2 转帖修改
- PIC序列二——第一个程序的建立