BFS台州1335营救天使
2016-03-30 21:01
281 查看
点击打开链接 题目链接
本题需要用到优先队列
优先队列:不同于普通的队列,优先队列是按照优先级高的先输出
若是不用优先队列:则会有数据出现错误
比如:
3 3
#.r
#.x
#a#
次组数据是没有按照优先队列 则搜索的时间为4而不是3了
4 3
..r
.#x
.#x
.ax
这一组同样会得到7而不是6
优先队列不懂得可以去下面的链接看看详细介绍
点击打开链接
广搜:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int go[4][2] = {{0,-1},{-1,0},{1,0},{0,1}};
char map[201][201];
int vis[201][201];
int n, m, ex, ey;
struct point
{
int x, y;
int step;
friend bool operator < (point a,point b)
{
return a.step > b.step; ///优先步数小的输出
}
}p;
int bfs(int x1,int y1)
{
p.x = x1,p.y = y1,p.step = 0;
priority_queue<point>q;
point current, next;
q.push(p);
vis[x1][y1] = 1;
while(!q.empty())
{
current = q.top();
q.pop();
for(int i = 0; i < 4; i++)
{
next.x = current.x + go[i][0];
next.y = current.y + go[i][1];
if(map[next.x][next.y]!='#'&&next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&vis[next.x][next.y]!=1)
{
if(map[next.x][next.y]=='a')
{
next.step = current.step + 1;
return next.step;
}
if(map[next.x][next.y]=='x')
{
vis[next.x][next.y] = 1;
next.step = current.step + 2;
q.push(next);
}
else
{
vis[next.x][next.y] = 1;
next.step = current.step + 1;
q.push(next);
}
}
}
}
return -1;
}
int main()
{
while(cin >> n >> m)
{
int ans;
memset(vis,0,sizeof(vis));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> map[i][j];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
if(map[i][j]=='r')
{
ex = i;
ey = j;
map[i][j] = '#';
}
}
ans = bfs(ex,ey);
if(ans!=-1)
cout << ans << endl;
else
cout << "Poor ANGEL has to stay in the prison all his life." << endl;
}
return 0;
}
本题需要用到优先队列
优先队列:不同于普通的队列,优先队列是按照优先级高的先输出
若是不用优先队列:则会有数据出现错误
比如:
3 3
#.r
#.x
#a#
次组数据是没有按照优先队列 则搜索的时间为4而不是3了
4 3
..r
.#x
.#x
.ax
这一组同样会得到7而不是6
优先队列不懂得可以去下面的链接看看详细介绍
点击打开链接
广搜:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int go[4][2] = {{0,-1},{-1,0},{1,0},{0,1}};
char map[201][201];
int vis[201][201];
int n, m, ex, ey;
struct point
{
int x, y;
int step;
friend bool operator < (point a,point b)
{
return a.step > b.step; ///优先步数小的输出
}
}p;
int bfs(int x1,int y1)
{
p.x = x1,p.y = y1,p.step = 0;
priority_queue<point>q;
point current, next;
q.push(p);
vis[x1][y1] = 1;
while(!q.empty())
{
current = q.top();
q.pop();
for(int i = 0; i < 4; i++)
{
next.x = current.x + go[i][0];
next.y = current.y + go[i][1];
if(map[next.x][next.y]!='#'&&next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&vis[next.x][next.y]!=1)
{
if(map[next.x][next.y]=='a')
{
next.step = current.step + 1;
return next.step;
}
if(map[next.x][next.y]=='x')
{
vis[next.x][next.y] = 1;
next.step = current.step + 2;
q.push(next);
}
else
{
vis[next.x][next.y] = 1;
next.step = current.step + 1;
q.push(next);
}
}
}
}
return -1;
}
int main()
{
while(cin >> n >> m)
{
int ans;
memset(vis,0,sizeof(vis));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> map[i][j];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
if(map[i][j]=='r')
{
ex = i;
ey = j;
map[i][j] = '#';
}
}
ans = bfs(ex,ey);
if(ans!=-1)
cout << ans << endl;
else
cout << "Poor ANGEL has to stay in the prison all his life." << endl;
}
return 0;
}
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- WordPress中用于获取搜索表单的PHP函数使用解析
- JavaScript中数组的排序、乱序和搜索实现代码
- jquery ztree实现树的搜索功能
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- iOS应用中UISearchDisplayController搜索效果的用法
- mysql 模糊搜索的方法介绍
- C#搜索文字在文件及文件夹中出现位置的方法