Battle City 优先队列+bfs
What we are discussing is a simple edition of this game.
Given a map that consists of empty spaces, rivers, steel walls and brick
walls only. Your task is to get a bonus as soon as possible suppose
that no enemies will disturb you (See the following picture).
Your tank can't move through rivers or walls, but it can
destroy brick walls by shooting. A brick wall will be turned into empty
spaces when you hit it, however, if your shot hit a steel wall, there
will be no damage to the wall. In each of your turns, you can choose to
move to a neighboring (4 directions, not 8) empty space, or shoot in one
of the four directions without a move. The shot will go ahead in that
direction, until it go out of the map or hit a wall. If the shot hits a
brick wall, the wall will disappear (i.e., in this turn). Well, given
the description of a map, the positions of your tank and the target, how
many turns will you take at least to arrive there?
Input
test case contains two integers M and N (2 <= M, N <= 300). Each
of the following M lines contains N uppercase letters, each of which is
one of 'Y' (you), 'T' (target), 'S' (steel wall), 'B' (brick wall), 'R'
(river) and 'E' (empty space). Both 'Y' and 'T' appear only once. A test
case of M = N = 0 indicates the end of input, and should not be
processed.
Output
separate line. If you can't arrive at the target, output "-1" instead.
Sample Input
3 4
YBEB
EERE
SSTE
0 0
Sample Output
8 这道题一开始就想到了优先队列,可就是不对,原来是优先队列建立的位置不对,建立在函数外,如果队列不空,会影响下组的结果,所以让他随着函数共存亡把 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
int n,m,sx,sy,tx,ty;
char map[][];
char vis[][];
int dir[][]={,,,,,-,-,};
struct que
{
int x,y,d;
friend bool operator <(que a,que b)
{
return a.d>b.d;
}
}temp,cn;
//priority_queue<que> q;///优先队列要建立在函数里 否则队列不空影响结果
int bfs(int x,int y)
{
priority_queue<que> q;
temp.x=x,temp.y=y,temp.d=,vis[sx][sy]=;
q.push(temp);
while(!q.empty())
{
cn=q.top();
q.pop();
for(int i=;i<;i++)
{
tx=cn.x+dir[i][];
ty=cn.y+dir[i][];
if(tx<||ty<||tx>=n||ty>=m||vis[tx][ty])continue;
vis[tx][ty]=;
int d=cn.d+(map[tx][ty]=='B'?:);
temp.x=tx,temp.y=ty,temp.d=d;
q.push(temp);
if(map[tx][ty]=='T')return d;
}
}
return -;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
if(!n&&!m)break;
memset(vis,,sizeof(vis));
sx=sy=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='Y')sx=i,sy=j;
else if(map[i][j]=='S'||map[i][j]=='R')vis[i][j]=;
}
}
cout<<bfs(sx,sy)<<endl;
}
}
Battle City 优先队列+bfs的更多相关文章
- poj 2312 Battle City(优先队列+bfs)
题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- POJ 2312:Battle City(BFS)
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9885 Accepted: 3285 Descr ...
- B - Battle City bfs+优先队列
来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- C - Battle City BFS+优先队列
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- poj 2312 Battle City
题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...
- 【POJ3635】Full Tank 优先队列BFS
普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...
- Battle City
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7208 Accepted: 2427 Descr ...
随机推荐
- angular5中使用echart的方法
注意两点安装的版本 安装好后可以参照echart的官网使用 1.实现package.json中安装这两个包 2.index.html中引入 3.在appModule中添加 然后再html中就可以这么使 ...
- SQLSERVER 对于非dbo的表增加注释
平时我们创建表的时候总是dbo.imsi_collect_state,但是有时候为了方便管理我们可能会创建架构wifi,那么表名就是wifi.imsi_collect_state 原来增加注释的方式是 ...
- Linux-Load Average解析
load Average 转自:http://www.blogjava.net/sliverfancy/archive/2013/04/17/397947.html 1.1:什么是Load?什么是Lo ...
- dd 命令常用功能收集(ing...)
xu言: 发现自己老是忘记一些不怎么常用,但是一定会用到的命令...so,做个备忘吧 Tips: sudo sh -c "head -c 15M /dev/urandom > test ...
- php--------对象(object) 与 数组(array) 的转换
php开发中常常用到数组,sql数据都是数组,数组和对象用的也是比较多的,常常相互转化,数组是PHP的灵魂,非常强大,面向对象编程也是挺方便的. /** * 数组 转 对象 * * @param ar ...
- thinkphp if标签
1.thinkphp框架中的if标签,用于html页面中.在html中编写php代码 1).从控制器中得到数据在循环中if else 判断:<volist name="system_r ...
- nyoj1248(阅读理解???)
海岛争霸 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己 ...
- UVA-12304 Race(递推)
题目大意:求n个人比赛的所有可能的名次种数.比如:n=2时,有A第一B第二.B第一A第二.AB并列第一三种名次. 题目解析:既然是比赛,总有第一名.第一名的人数可能是i (1≤i≤n),则剩下待定的人 ...
- Leetcode 92
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- XML文档的读、写
代码: XmlDocument doc = new XmlDocument(); doc.Load("Books.xml"); //1.加载要读取的XML文件 //要想看到数据得先 ...