http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649

//hnldyhy(303882171)  11:12:46
// zoj 1649 //bfs +优先队列 #include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int x;
int y;
int step;
}; bool operator<(const node &a,const node &b)
{
if (a.step>b.step) return true;
else return false;
} char g[201][201];
int main()
{
int m,n,i,j;
node start,end,temp,t,tx;
priority_queue <node>q,x; //x队列用于暂存由x变成的r,在下一回合再push进q
bool c;
int s[4][2]={{0,1}, {0,-1}, {1,0}, {-1,0}};
while (scanf("%d%d",&m,&n)!=EOF)
{
c=false;
for (i=0;i<m;i++)
{
scanf("%s",g[i]);
for (j=0;j<n;j++)
{
if (g[i][j]=='r')
{
start.x=j; //起始点进q
start.y=i;
start.step=0;
q.push(start);
}
if (g[i][j]=='a')
{
end.x=j; //记录结束点
end.y=i;
}
}
}
while (!q.empty()||!x.empty())
{
while (!x.empty()) //将x里的元素push进q里
{
tx=x.top();
x.pop();
q.push(tx);
}
temp=q.top();
q.pop();
for (i=0;i<4;i++) //上下左右进行扩张
{
t=temp;
t.x=t.x+s[i][0];
t.y=t.y+s[i][1];
if (t.x<0||t.x>=n||t.y<0||t.y>=m)
continue;
if (g[t.y][t.x]=='.')
{
g[t.y][t.x]='r';
t.step=t.step+1;
q.push(t);
}
if (g[t.y][t.x]=='x')
{ g[t.y][t.x]='r'; //勿忘标记,
t.step=t.step+2;
x.push(t);
}
if (g[t.y][t.x]=='a')
{
c=true;
break;
}
}
if (c) break;
}
if (c) printf("%d\n",temp.step+1);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
while (!q.empty()) q.pop(); //一定要清,否则超内存
}
return 0;
} zoj 1649 // bfs搜索:x的出现是难点。每次过x的时候step是要+2 #include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <iomanip>
using namespace std;
string map[210];
int flag[210][210];
struct node
{ int x;
int y;
int step;
};
int way[4][2]={0,1,0,-1,1,0,-1,0}; bool operator<(const node &a,const node &b)
{
if(a.step>b.step) return true;
else return false;
} int main()
{
int i,j,n,m,cur;
node t,tt;
bool fail;
priority_queue <node>q;
while( cin>>n>>m )
{ memset( flag,0,sizeof(flag) );
for( i=0;i<n;i++ )
cin>>map[i];
for( i=0;i<n;i++ )
{
for( j=0;j<m;j++ )
{
if( map[i][j]=='r' )
{
t.x=i;
t.y=j;
t.step=1;
flag[t.x][t.y]=1;
q.push(t);
}
}
}
fail=true;
cur=1;
while( !q.empty() )
{
t=q.top();
q.pop();
if( map[t.x][t.y]=='a' )
{
fail=false;
cout<<t.step-1<<endl;
break;
}
for( i=0;i<4;i++ )
{
tt.x=t.x+way[i][0];
tt.y=t.y+way[i][1];
if( tt.x>=0 && tt.x<n && tt.y>=0 && tt.y<m && !flag[tt.x][tt.y] )
{
if( map[tt.x][tt.y]=='.' || map[tt.x][tt.y]=='a' )
{
// cout<<tt.x<<" "<<tt.y<<" "<<t.step+1<<endl;
tt.step=t.step+1;
flag[tt.x][tt.y]=tt.step;
q.push( tt );
}
else if( map[tt.x][tt.y]=='x' )
{
tt.step=t.step+2; //每次过x的时候step是要+2 flag[tt.x][tt.y]=tt.step;
q.push( tt );
}
}
}
}
if( fail ) cout<<"Poor ANGEL has to stay in the prison all his life.\n";
while( !q.empty() ) q.pop();
}
return 0;
}

  

// hnldyhy(303882171)  10:26:24
// zoj 1649
#include<iostream>
#include<stdio.h>
#include <string.h>
#include<queue>
#define MAXMN 200
#define INF 1000000 //走到每个位置所需时间的初始值无穷大
using namespace std;
struct point//表示到达某个方格时的状态
{
int x,y;//方格的位置
int time;//走到当前位置所花时间
};
queue<point> Q;//队列中的结点为当前angel的朋友所处的位置
int N,M;//监狱的大小
char map[MAXMN][MAXMN];//地图
int mintime[MAXMN][MAXMN];//走到每个位置所需最少时间
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};//4个相邻方向:上,右,下,左
int ax,ay;//angle所在的位置 int BFS(point s)
{ int i;
point hd,t;
Q.push(s);
while (!Q.empty())//当队列非空
{
hd=Q.front();
Q.pop();
for (i=0;i<4;i++)
{ int x=hd.x+dir[i][0],y=hd.y+dir[i][1];
if (x>=0&&x<=N-1&&y>=0&&y<=M-1&&map[x][y]!='#') //排除边界 和 墙壁
{ t.x=x; t.y=y; t.time=hd.time+1;
if (map[x][y]=='x') t.time++;//杀死警卫队多花一个单位时间 if (t.time<mintime[x][y])//如果这种走法比之前走到(x,y)位置所花的时间更少,则把t入队列
//否则t无需入队列
{ mintime[x][y]=t.time;
Q.push(t);//t入队
}
}
}
}
return mintime[ax][ay];
}
int main()
{
int i,j,sx,sy;
point start;
while (scanf("%d%d",&N,&M)!=EOF)
{
memset(map,0,sizeof(map));
for (i=0;i<N;i++) scanf("%s",map[i]);//读入地图 for (i=0;i<N;i++)
for (j=0;j<M;j++)
{
mintime[i][j]=INF;
if (map[i][j]=='a') { ax=i; ay=j; }
else if (map[i][j]=='r') { sx=i; sy=j; }
}
start.x=sx; start.y=sy; start.time=0;
mintime[sx][sy]=0;
int mint=BFS(start);//返回到达angle位置的最少时间,有可能为INF
if (mint<INF)
cout<<mint<<endl;
else
cout<<"Poor ANGEL has to stay in the prison all his life.\n";
}
return 0;
}

  

BFS zoj 1649的更多相关文章

  1. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  2. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

  3. ZOJ 1649:Rescue(BFS)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

  4. zoj 1649 bfs

    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...

  5. ZOJ 1649 Rescue(有敌人迷宫BFS)

    题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过'x'方格须要两秒  '#'表示墙 因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...

  6. zoj 1649 Rescue

    BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...

  7. zoj 1649

    #include <iostream> #include <queue> using namespace std; int n,m,s2,e2; int b[205][205] ...

  8. HZNU Training 1 for Zhejiang Provincial Collegiate Programming Contest

    赛后总结: TJ:今天我先到实验室,开始看题,一眼就看了一道防AK的题目,还居然觉得自己能做wwww.然后金姐和彭彭来了以后,我和他们讲了点题目.然后金姐开始搞dfs,我和彭彭看榜研究F题.想了很久脑 ...

  9. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

随机推荐

  1. IsPostBack and DropdownList.

    Encounted the issue accident when helping my classmate dealing with his homework assignment,it turns ...

  2. System.Data.DbType的字符串和数据库中字符串类型对应关系

    前两天项目中因为历史原因数据库中的一个字段是varchar类型,在做SQL参数化处理时候默认都是DbType.String, 免得查询出现数据转换,于是做类型一致,搜了下对应关系还没找到,只好自己打开 ...

  3. 用PHP实现一个高效安全的ftp服务器(一)

    摘要: 本文主要阐述使用PHP的swoole扩展实现ftp服务器,同时扩展ftp服务器个性化功能和安全性.真正实现一个自己完全掌控的ftp服务器,可以个性化定制的ftp服务器. 正文: FTP服务器想 ...

  4. 最简单的基于FFmpeg的移动端例子:IOS 推流器

    转至:http://blog.csdn.net/leixiaohua1020/article/details/47072519   ================================== ...

  5. ubuntu12.04安装QQ2013

    1.下载Longene QQ2013SP6 http://pan.baidu.com/s/1hq83fWo 2.安装 1)如果之前安装过旧版本需要先卸载(通过dpkg -l | grep qq查看). ...

  6. 服务器上搭建spark开发环境

    1.安装相应的软件 (1)安装jdk 下载地址:http://www.Oracle.com/technetwork/java/javase/downloads/index.html (2)安装scal ...

  7. C# 线程间互相通信

    C#线程间互相通信主要用到两个类:AutoResetEvent和ManualResetEvent. 一.AutoResetEvent AutoResetEvent 允许线程通过发信号互相通信,线程通过 ...

  8. cocod2d-x 之 HelloWorld

    cocos2d-x 2.2创建项目 进入cocos2d-x-2.2/tools/project-creator,运行命令 python create_project.py -project MyGam ...

  9. ajax xmlhttprequest status

    status 0**:未被始化 status 1**:请求收到,继续处理 status 2**:操作成功收到,分析.接受 status 3**:完成此请求必须进一步处理 status 4**:请求包含 ...

  10. CSS3的appearance属性--改变元素的外观

    CSS3 appearance 属性 CSS 参考手册 实例 使 div 元素看上去像一个按钮: div { appearance:button; -moz-appearance:button; /* ...