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. JAva Collections类方法详解

    http://blog.csdn.net/lskyne/article/details/8961014 Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素 ...

  2. Java开发工程师必会做试题

    一.单选题     (共19道题,每题5分) 1.下面有关java的一些细节问题,描述错误的是? A.构造方法不需要同步化 B.一个子类不可以覆盖掉父类的同步方法 C.定义在接口中的方法默认是publ ...

  3. VS2015使用OSChina的git功能

    好长时间没有写博了,把今天的新的记录一下. 最近开始使用vs2015,vs2015支持git平台和TF功能,因为....,我选择了OSChina的git.一开始学习的此篇文章http://my.osc ...

  4. 利用linq快速判断给定数字是否包含在某个段范围内

    一.需求: 知道某段范围0x0020~0x007F0x00A0~0x017F0x01A0~0x01CF0x01F0~0x01FF0x0210~0x021F0x1EA0~0x1EFF给定一个值,快速判断 ...

  5. Chess---->简单命令框象棋(人VS人)

    简单粗暴,直接先上代码: ChessBoard.h:  1 #ifndef CHESBOARD_H  2 #include<iostream>  3 #include<string& ...

  6. 内置方法+lambda是pythonic的利器

    python可以写的非常简洁,通过使用内置的map,reduce,filter,lambda方法,非常具有文艺范. 举个例子,例如 def fromIpToNum(ipAddr):    return ...

  7. Mysql 目录恢复注意事项

    SET @mycnt=0; SELECT @mycnt := @mycnt +1 as mycnt, a.*, b.* FROM a, b; 表中第一列即为mycnt,从1开始计数. set @num ...

  8. C# Windows服务安装出现System.Security.SecurityException异常解决办法

    我把注册windows服务所用的安装及启用服务命令写到了bat可执行文件(名称为install.bat)中,如下所示: %SystemRoot%\Microsoft.NET\Framework\v4. ...

  9. 使用iOS8 WKWebView的浏览器模块,脉冲动画层-b

    KINWebBrowser是一个可嵌入app的浏览器模块. 它使用iOS 8的 WKWebView API编写,同时在iOS 7上使用UIWebView来兼容. 测试环境: Xcode 6.0 iOS ...

  10. SequoiaDB 架构指南

    1 简介 SequoiaDB(巨杉数据库)是一款分布式非关系型文档数据库,可以被用来存取海量非关系型的数据,其底层主要基于分布式,高可用,高性能与动态数据类型设计,与当前主流分布式计算框架 Hadoo ...