hud 1242 Rescue
来源:互联网 发布:淘宝服装店铺模板 编辑:程序博客网 时间:2024/05/17 21:52
这道题是一道广度的裸题,套模版,最重要的是要比较距离的大小,因为这道题中不是移动每个点都是1,当遇到的是狱警'x'的时候距离要+2,因此可能出现移动次数多但是移动距离小的可能,所以要加上比较
used[xx][yy] = true;
if(map[xx][yy] == 'x')if(step[xx][yy] > step[x1][y1]+2)
step[xx][yy] = step[x1][y1] + 2;
else
continue;
else
if(step[xx][yy] > step[x1][y1]+1)
step[xx][yy] = step[x1][y1]+1;
else
continue;
然后就是代码了。
#include <iostream>
#include <string>
#include <queue>
using std::string; using std::queue;
using std::cin; using std::cout;
using std::endl;
char map[250][250];
int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
bool used[250][250];
int step[250][250];
int dis;
int n,m;
queue<int> q;
void bfs(int x1,int y1)
{
while(!q.empty())//清空
q.pop();
memset(used,false,sizeof(used));
memset(step,10000,sizeof(step));
int u = x1*m+y1;
q.push(u);
used[x1][y1] = true;
step[x1][y1] = 0;
while(!q.empty())
{
u = q.front();
q.pop();
x1 = u/m;
y1 = u%m;
for(int i = 0;i < 4;i++)
{
int xx = x1+move[i][0];
int yy = y1+move[i][1];
if(xx >= n||yy >= m||yy<0||xx<0||map[xx][yy] == '#')
continue;
used[xx][yy] = true;
if(map[xx][yy] == 'x')
if(step[xx][yy] > step[x1][y1]+2)
step[xx][yy] = step[x1][y1] + 2;
else
continue;
else
if(step[xx][yy] > step[x1][y1]+1)
step[xx][yy] = step[x1][y1]+1;
else
continue;
if(map[xx][yy] == 'a')
{
if(step[xx][yy] < dis)
dis = step[xx][yy];
return;
}
q.push(xx*m+yy);
}
}
}
int main()
{
while(cin>>n>>m)
{
for(int i = 0;i < n;i++)
for(int k = 0;k < m;k++)
cin>>map[i][k];
dis = 0xffffff;
//找到每个朋友的位置;
for(int i = 0;i < n;i++)
{
for(int k = 0;k < m;k++)
if(map[i][k] == 'r')
{
int x = i;
int y = k;
bfs(x,y);
}
}
if(dis == 0xffffff)
cout << "Poor ANGEL has to stay in the prison all his life."<<endl;
else
cout << dis <<endl;
}
return 0;
}
- hud 1242 Rescue
- 1242 Rescue
- 1242 Rescue
- HUD
- hdu 1242 Rescue
- 1242 rescue 广搜
- hdoj 1242 Rescue(bfs)
- HDU-1242-Rescue
- HDU 1242 Rescue
- HDU-1242 Rescue
- hdoj 1242 Rescue
- hdoj 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue<java>
- hdu 1242 rescue
- HDU-1242:Rescue
- HDU 1242 Rescue
- HDU 1242 Rescue
- 用Java语言实现的各种排序
- unity使用第三方sdk图标问题
- android ListView美化中几个特别的属性
- JS+CSS仿网易的选项卡TAB标签样式-导航菜单类 .
- Hexagon and Rhombic Dominoes zoj2673
- hud 1242 Rescue
- 推荐一个Android应用开发的服务器端框架codeigniter
- chrome打开新页面都是打开http://www.94994.com/?thunder
- Windows7与Ubuntu双系统,windows引导
- 解题报告2
- SSH常用命令
- js操作txt
- Android适配器Adapter学习
- install SSH on ubuntu 12.04