BFS zoj 1649
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的更多相关文章
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- zoj 1649 Rescue (BFS)(转载)
又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...
- ZOJ 1649:Rescue(BFS)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- zoj 1649 bfs
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...
- ZOJ 1649 Rescue(有敌人迷宫BFS)
题意 求迷宫中从a的位置到r的位置须要的最少时间 经过'.'方格须要1s 经过'x'方格须要两秒 '#'表示墙 因为有1s和2s两种情况 须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...
- zoj 1649 Rescue
BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...
- zoj 1649
#include <iostream> #include <queue> using namespace std; int n,m,s2,e2; int b[205][205] ...
- HZNU Training 1 for Zhejiang Provincial Collegiate Programming Contest
赛后总结: TJ:今天我先到实验室,开始看题,一眼就看了一道防AK的题目,还居然觉得自己能做wwww.然后金姐和彭彭来了以后,我和他们讲了点题目.然后金姐开始搞dfs,我和彭彭看榜研究F题.想了很久脑 ...
- BFS+模拟 ZOJ 3865 Superbot
题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...
随机推荐
- apk文件解析,学习笔记
Android 应用程序包文件 (APK) 是一种Android操作系统上的应用程序安装文件格式,其英文全称为 “application package file” . 如果懂得使用反编译工具,可以下 ...
- 如何在安卓/data(而不是/data/data)目录下进行文件的读写操作
分析:Android默认是无法直接操作/data目录的,只能读写程序自己的私有目录,也就是/data/data/package name/下,默认只能操作这个目录下的文件,也就是我们想直接读写/dat ...
- [中级] 有效删除URL中的index.php
如果你刚接触CI不久又或者刚刚研读CI的使用手册的话,关于如何有效删除URL中index.php以使URL看起来更友好美观的问题,可能是你面对的第一个较为复杂的问题!本贴不是原创,而是一个各种意见的综 ...
- 移动端touchstar、touchmove、touchend 事件如果页面有滚动时不让触发 touchend 事件。
/*仅适用于内容中点击元素.对于拖动等元素,需要自行在页面处理. * 主要是绑定touchstart和touchmove事件,并判断用户按下之后手指移动了多少像素. * 如果手指移动距离小于10像素, ...
- webpack学习笔记一(入门)
webpack集成了模块加载和打包等功能 ,这两年在前端领域越来越受欢迎.平时一般是用requirejs.seajs作为模块加载用,用grunt/gulp作为前端构建.webpack作为模块化加载兼容 ...
- java_设计模式_工厂模式_Factory Pattern(2016-08-04)
工厂模式主要是为创建对象提供了接口.工厂模式按照<Java与模式>中的提法分为三类: (1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory ...
- SGU 172.eXam(二分图染色)
时间限制:0.25s 空间限制:4M 题意: 将n(n<200)个点分成两个集合,给出m(m<=30000)对不能在一个集合的点对,判断能否分成满足要求的集合,输出其中一个集合和集合的总数 ...
- mysql更新密码为空
1.进入命令行 mysql -u root -p 'oldpassword'; 2 修改root用户的密码:mysql> update mysql.user set password=PASSW ...
- How far away ?
#include<iostream> #include <algorithm> using namespace std; const int M=40010; int dis[ ...
- php——composer 1、安装使用
Composer 是PHP中用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer会帮你安装这些依赖的库文件. 系统要求 运 ...