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. U3D 内置对象

    在U3D里面提供了一个Time对象: void OnGUI(){ Debug.Log("########################"); GUILayout.Label (& ...

  2. ArrayList 类和List<T>泛型类

    ArrayList集合类在System.Colletions命名空间下,它其实是一个特殊的数组,它可以动态的添加和删除元素,根据元素的改变自动决定它自身的大小,也可以灵活的插入元素等操作,使用起来要比 ...

  3. 如何设置MySQL Workbench EER Diagram 尺寸?

    ER Diagram -> Model -> Diagram Properties and Size...

  4. Debug your C# project more efficiently

    I am a very beginner working with C# and Visual Studio 2013. When I debug my project, I always reope ...

  5. JDK常用类_util

    集合 Collection:集合顶层接口 AbstractCollection:集合抽象类 关联数组 Map:顶层接口 AbstractMap:抽象类实现,提供了子类的通用操作 HashMap:哈希表 ...

  6. hdoj 1089(费马小定理)

    题目大意:方程f(x)=5*x^13+13*x^5+k*a*x:输入任意一个数k,是否存在一个数a,对任意x都能使得f(x)能被65整出. 现假设存在这个数a ,因为对于任意x方程都成立 所以,当x= ...

  7. nginx location

    1. “= ”,字面精确匹配, 如果匹配,则跳出匹配过程.(不再进行正则匹配) 2. “^~ ”,最大前缀匹配,如果匹配,则跳出匹配过程.(不再进行正则匹配) 3. 不带任何前缀:最大前缀匹配,举例如 ...

  8. 1.dubbo的安装 quickstart

    按照官网给定的指导,执行下面的步骤即可 1.Import the dubbo source code to eclipse project 在eclipse中安装git插件 egit 直接可以从git ...

  9. Ubuntu14.04+CUDA6.5环境下神经网络工具包Deepnet配置

    deepnet是多伦多大学计算机系机器学习组开发的一个神经网络工具包,可以进行以下计算: 1.  Feed-forward Neural Nets 2.  Restricted Boltzmann M ...

  10. Bootstrap_表单_图像

    在Bootstrap框架中对于图像的样式风格提供以下几种风格: 1.img-responsive:响应式图片,主要针对于响应式设计2.img-rounded:圆角图片3.img-circle:圆形图片 ...