hdu1242
来源:互联网 发布:软件测试零基础 编辑:程序博客网 时间:2024/06/06 09:00
这道题的主要是意思是求从 r 到 a的最短路径
思路就是用优先队列+bfs:每次找出最短的路径,并向下扩展。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define mx 210
char map[mx][mx];
int m,n;
struct node
{
int x;
int y;
int sum;
friend bool operator<(node s1,node s2)
{
return s1.sum>s2.sum;
}
}cx;
int cont;
int sx,sy;
int flag;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
void bfs()
{
cont=0;
flag=-1;
int i,j;
priority_queue<node>ka;
node a,b;
a.x=sx;
a.y=sy;
a.sum=1;
ka.push(a);
int ty,tx;
while(!ka.empty())
{
cx=ka.top();
ka.pop();
for(i=0;i<4;i++)
{
tx=cx.x+dir[i][0];
ty=cx.y+dir[i][1];
if(tx>=0&&ty>=0&&tx<n&&ty<m&&map[tx][ty]!='#')
{
if(map[tx][ty]=='.')
b.sum=cx.sum+1;
else if(map[tx][ty]=='x')
b.sum=cx.sum+2;
else if(map[tx][ty]=='a')
{
cont+=cx.sum;
flag=0;
return;
}
b.x=tx;
b.y=ty;
map[tx][ty]='#';
ka.push(b);
}
}
}
}
int main()
{
int f,g;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(f=0;f<n;f++)
{
scanf("%s",map[f]);
for(g=0;g<m;g++)
{
if(map[f][g]=='r')
{
sx=f;
sy=g;
}
}
}
map[sx][sy]='#';
bfs();
if(flag==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",cont);
}
}
- hdu1242
- hdu1242
- hdu1242
- hdu1242
- hdu1242
- hdu1242
- hdu1242
- hdu1242
- hdu1242
- HDU1242
- HDU1242
- hdu1242
- hdu1242 rescue
- hdu1242 Rescue
- hdu1242 Rescue
- HDU1242 Rescue
- hdu1242 Rescue
- HDU1242 Rescue
- 从零开始学android<android基本绘图.四十六.>
- oozie 常用标签 -- coordinator.xml
- 每天学习一算法系列(7) (根据上排给出十个数,在其下排填出对应的十个数)
- 四个程序员的一天 【转载】
- android项目解刨之时间轴
- hdu1242
- [Django] 查看orm自动执行的原始查询sql
- CodeVs 数的计算;
- 黑马程序员_java语言基础
- Jsoup简介——使用Java抓取网页数据
- 如何用虚拟机安装linux系统
- 微软等数据结构+算法面试100题全部答案集锦
- 在编译 squashfs4.2时候fatal error: zlib.h & lzma.h : No such file or directory
- 以多个版本JDK的配置,说明update-alternatives的用法