poj-2312

题意:

这个题是以坦克大战为原型出来的题目,就是走迷宫的变种,给定一个地图mxn的地图,地图上有普通的砖B,金砖S,河R,空地E,和一个宝物位置T,和你的位置Y,求吃到宝物的最小步数(坦克通过普通砖B需要两步,空地E一步,不能通过金砖和河)..

样例输入

3 4
YBEB
EERE
SSTE
0 0

样例输出

8
解题思路:
简单的BFS但是要用优先队列来做,普通的队列会出错(很重要),不过目前还没弄清楚出错的原因。

具体代码:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
char map[305][305];
int beginx,beginy;
int m,n;
int fangxiang[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int res=9999999;
//int min=1000000;
struct Node
{
int x;
int y;
int step;
Node (int x1,int y1,int step1):x(x1),y(y1),step(step1){}
};
bool operator < (const Node &a, const Node &b) //优先队列,队头为最优解。即花费最少
{
return a.step > b.step;
}
void bfs(int p,int q,int ste)
{
priority_queue<Node> qq;
Node node(p,q,ste);
while(!qq.empty()) qq.pop();
qq.push(node);
while(!qq.empty())
{
node=qq.top();
qq.pop();
if(map[node.x][node.y]==‘T‘)
{
if(node.step<res)
{
res=node.step;
}
}
for(int i=0;i<4;i++)
{
int xx=node.x+fangxiang[i][0];
int yy=node.y+fangxiang[i][1];
if(xx>=1&&xx<=m&&yy>=1&&yy<=n&&(map[xx][yy]==‘E‘||map[xx][yy]==‘B‘||map[xx][yy]==‘T‘))
{
if(map[xx][yy]!=‘T‘)
{
if(map[xx][yy]==‘B‘)
{
map[xx][yy]=‘S‘;
Node test(xx,yy,node.step+2);
qq.push(test);
}
else if(map[xx][yy]==‘E‘)
{
map[xx][yy]=‘S‘;
Node test(xx,yy,node.step+1);
qq.push(test);
}
}
else
{
Node test(xx,yy,node.step+1);
qq.push(test);
}
}
}
}
}
int main()
{
while(1)
{
memset(map,0,sizeof(map));
res=9999999;
cin>>m>>n;
if(m+n==0)
break;
else
{
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
cin>>map[i][j];
if(map[i][j]==‘Y‘)
{
beginx=i;
beginy=j;
}
}
}
bfs(beginx,beginy,0);
if(res!=9999999)
cout<<res<<endl;
else
cout<<-1<<endl;

}
}
return 0;
}

poj-2312

时间: 2024-10-30 10:07:02

poj-2312的相关文章

POJ 2312 Battle City(优先队列+BFS)

题目链接:http://poj.org/problem?id=2312 Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7085   Accepted: 2390 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often pl

Poj(2312),坦克大战,BFS的变形

题目链接:http://poj.org/problem?id=2312 挺有趣的一道题目,然而很容易WA,我就WA了一次,虽然我Debug的时候已经知道哪里出问题了,就是比如说我搜到B和E时,从B搜第三个点,B左边的E就被搜了,step为3,然而其实他是step为2, 这里的处理方法很是巧妙,可以从B出发时,把B换成E,step+1,形成一个新的结点加入到队列中去 #include <stdio.h> #include <string.h> #include <queue&g

poj 2312 Battle City

题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Giv

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space

poj 2312 Battle City【bfs+优先队列】

Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7579   Accepted: 2544 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are d

POJ 2312(优先队列+bfs)

Battle City Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spac

bfs 2016.5.2

1.HDU 2612 Find a way 题意: Y和M去KFC见面,有很多KFC,帮他们找一个KFC使得他们花费的时间总和最小 解题思路: 两次 bfs 分别找出他们到达他们都能到达的所有的KFC的最短时间 然后比较他们花费时间的和找出最小 #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int max

POJ 题目2312 Battle City(BFS)

Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7208   Accepted: 2427 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are d

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京