zoj 1649 BFS
2014-10-20 19:13
274 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649
我觉得这题写的很好
以下为我的代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=55555555;
const int sizen=500;
struct ele
{
int x;
int y;
}t,s;
char Map[sizen][sizen];
int mapp[sizen][sizen];
int d[4][2]={1,0,-1,0,0,1,0,-1};
void bfs(int x,int y,int n,int m)
{
int i;
queue<ele>p;
t.x=x;
t.y=y;
p.push(t);
while(p.size())
{
t=p.front();
p.pop();
for(i=0;i<4;i++)
{
x=t.x+d[i][0];
y=t.y+d[i][1];
if(x>=0&&x<n&&y>=0&&y<m&&Map[x][y]!='#')
{
if(Map[x][y]=='.'||Map[x][y]=='a')
if(mapp[x][y]>mapp[t.x][t.y]+1)
{
//printf("%d %d\n",x,y);
mapp[x][y]=mapp[t.x][t.y]+1;
s.x=x;
s.y=y;
p.push(s);
}
if(Map[x][y]=='x')
if(mapp[x][y]>mapp[t.x][t.y]+2)
{
mapp[x][y]=mapp[t.x][t.y]+2;
s.x=x;
s.y=y;
p.push(s);
}
}
}
}
}
int main()
{
int sx,sy;
int ex,ey;
int n,m;
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
scanf("%s",Map[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(Map[i][j]=='r')
sx=i,sy=j;
mapp[i][j]=INF;
if(Map[i][j]=='a')
ex=i,ey=j;
}
mapp[sx][sy]=0;
bfs(sx,sy,n,m);
if(mapp[ex][ey]!=INF)
printf("%d\n",mapp[ex][ey]);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
我觉得这题写的很好
以下为我的代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=55555555;
const int sizen=500;
struct ele
{
int x;
int y;
}t,s;
char Map[sizen][sizen];
int mapp[sizen][sizen];
int d[4][2]={1,0,-1,0,0,1,0,-1};
void bfs(int x,int y,int n,int m)
{
int i;
queue<ele>p;
t.x=x;
t.y=y;
p.push(t);
while(p.size())
{
t=p.front();
p.pop();
for(i=0;i<4;i++)
{
x=t.x+d[i][0];
y=t.y+d[i][1];
if(x>=0&&x<n&&y>=0&&y<m&&Map[x][y]!='#')
{
if(Map[x][y]=='.'||Map[x][y]=='a')
if(mapp[x][y]>mapp[t.x][t.y]+1)
{
//printf("%d %d\n",x,y);
mapp[x][y]=mapp[t.x][t.y]+1;
s.x=x;
s.y=y;
p.push(s);
}
if(Map[x][y]=='x')
if(mapp[x][y]>mapp[t.x][t.y]+2)
{
mapp[x][y]=mapp[t.x][t.y]+2;
s.x=x;
s.y=y;
p.push(s);
}
}
}
}
}
int main()
{
int sx,sy;
int ex,ey;
int n,m;
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
scanf("%s",Map[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(Map[i][j]=='r')
sx=i,sy=j;
mapp[i][j]=INF;
if(Map[i][j]=='a')
ex=i,ey=j;
}
mapp[sx][sy]=0;
bfs(sx,sy,n,m);
if(mapp[ex][ey]!=INF)
printf("%d\n",mapp[ex][ey]);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
相关文章推荐
- ZOJ1649 营救Rescue (BFS)
- ZOJ 1649 Rescue (BFS)
- HDU 1242 Rescue(BFS),ZOJ 1649
- ZOJ 1649 Rescue (BFS)
- zoj 1649 bfs
- zoj 1649 Rescue (BFS)(转载)
- ZOJ 1649 BFS
- zoj 1649 Rescue【bfs】
- Hdu 1242 & Zoj 1649 Rescue (优先队列+BFS)
- zoj 1649 Rescue_BFS
- ZOJ 1649 Rescue BFS (搜索)
- zoj 1649 bfs
- ZOJ 1649:Rescue(BFS)
- ACM-ZOJ 1649 BFS 广度优先搜索
- ZOJ 1649:Rescue(BFS)
- ZOJ-1649 Rescue BFS (HDU 1242)
- zoj1649 BFS
- zoj 1649 rescue (bfs)
- ZOJ 1649 Resuce (BFS)
- ZOJ第一题_1649Rescue(优先队列下的BFS)