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 ...
随机推荐
- cygwin install git
Installation with Cygwin If you're comfortable with Cygwin, then use it to install git, ssh, wget an ...
- URAL 1658 Sum of Digits
URAL 1658 思路: dp+记录路径 状态:dp[i][j]表示s1为i,s2为j的最小位数 初始状态:dp[0][0]=0 状态转移:dp[i][j]=min(dp[i-k][j-k*k]+1 ...
- Codeforces 827C - DNA Evolution
827C - DNA Evolution 思路: 写4*10*10个树状数组,一个维度是4(ATCG),另一个维度是长度len,另一个维度是pos%len,因为两个pos,如果len和pos%len相 ...
- Python3 基本语法学习
1.查看Python版本及打印“Hellow World!”: 需要注意的是:在打印“Hello World”之前一定要先执行 python,否则会报无法 “无法初始化设备 PRN”,如图: 2.查看 ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...
- php--------对象(object) 与 数组(array) 的转换
php开发中常常用到数组,sql数据都是数组,数组和对象用的也是比较多的,常常相互转化,数组是PHP的灵魂,非常强大,面向对象编程也是挺方便的. /** * 数组 转 对象 * * @param ar ...
- Please, another Queries on Array? CodeForces - 1114F (线段树,欧拉函数)
这题刚开始看成求区间$\phi$和了........先说一下区间和的做法吧...... 就是说将题目的操作2改为求$(\sum\limits_{i=l}^{r}\phi(a[i]))\%P$ 首先要知 ...
- BUCTOJ1073
#include "iostream" #include "algorithm" using namespace std; ; struct Time { in ...
- mac连接Windows远程桌面
先打开微软官方的下载面面:http://www.microsoft.com/zh-CN/download/details.aspx?id=18140 下载远程连接客户端 http://jingyan. ...
- Elasticsearch在centos6中的安装
一安装, 在你可以从 elasticsearch.org\/download 下载最新版本的Elasticsearch.tar文件. 一.用户设置 如果已经是普通用户登录可跳过此步骤. Elastic ...