POJ 2312 - Battle City
来源:互联网 发布:1024程序员节 编辑:程序博客网 时间:2024/05/21 14:44
题目就是根据我们小时候经常玩的游戏坦克大战,
图上有你自己的位置Y,硬墙S,砖块B,河R,空地E,目标位置T,硬墙和河都是不能通过的,通过空地需要1步,而通过砖块则需要两步,给出一个图,要求共需要要多少步能从当前位置到达目标位置;
思路分析:BFS,由于砖块需要两步,所以每次需要比较到达某一位置的步数,记忆化搜索,其他的和一般简单的BFS没什么区别;
code:
#include<iostream>
#include<cstdio>
#include<queue>
#include<memory.h>
using namespace std;
struct node
{
int x,y,step;
} p,q;
int direct[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int num[301][301];
char map[301][301];
int m,n,x,y,xx,yy;
int judge(int x1,int y1)
{
return (x1>=0 && x1<m && y1>=0 && y1<n && map[x1][y1]!=\'S\' && map[x1][y1]!=\'R\');
}
void bfs()
{
queue<node> Q;
q.x=x;
q.y=y;
q.step=0;
Q.push(q);
while(!Q.empty())
{
q=Q.front();
Q.pop();
for(int i=0; i<4; i++)
{
p.x=q.x+direct[i][0];
p.y=q.y+direct[i][1];
if(judge(p.x,p.y))
{
if(map[p.x][p.y]==\'B\')
p.step=q.step+2;
else
p.step=q.step+1;
if(num[p.x][p.y]>p.step)
{
num[p.x][p.y]=p.step;
Q.push(p);
}
}
}
}
}
int main()
{
//freopen(\"in.txt\",\"r\",stdin);
while(cin>>m>>n && (m||n))
{
memset(map,\'S\',sizeof(map));
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
num[i][j]=10000000;
for(int i=0; i<m; i++)
{
getchar();
for(int j=0; j<n; j++)
{
char c=getchar();
if(c==\'Y\')
{
x=i;
y=j;
}
if(c==\'T\')
{
xx=i;
yy=j;
}
map[i][j]=c;
}
}
bfs();
if(num[xx][yy]!=10000000)
cout<<num[xx][yy]<<endl;
else
cout<<\"-1\"<<endl;
}
return 0;
}
- poj 2312 Battle City
- POJ 2312 Battle City
- POJ 2312 - Battle City
- poj 2312:Battle City
- POJ 2312 Battle City
- POJ 2312 Battle City
- poj 2312 Battle City
- poj 2312 Battle City
- POJ 2312 Battle City
- poj-2312-Battle City
- poj 2312 Battle City
- 【POJ 2312】Battle City
- POJ 2312 Battle City
- POJ 2312 Battle City
- Battle City(POJ 2312)
- poj 2312 Battle City-bfs
- poj 2312 Battle City bfs
- POJ 2312 Battle City 笔记
- UVA 10310 - Dog and Gopher
- UVA 439 - Knight Moves
- POJ 1979 - Red and Black
- POJ 3126 - Prime Path
- POJ 3278 - Catch That Cow
- POJ 2312 - Battle City
- POJ 2251 - Dungeon Master
- POJ 2225 - Asteroids!
- POJ 1101 - The Game
- BFS 宽度优先搜索——初学
- POJ题目分类(算法分类较详细)
- HDU 4004 - The Frog\'s Games 第36届ACM/ICPC亚洲大连赛区网络赛
- HDU 题目分类
- UVA 10115 - Automatic Editing