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 ...
随机推荐
- 20170814xlVBA PowerPoint分类插图加说明
Public Sub AddPictures() Dim ppApp As PowerPoint.Application Set ppApp = New PowerPoint.Application ...
- HBase基于region数目和data locality来balance regions
1. 在Hbase的运维过程中,我们经常需要做如下操作: 移动 regionserver 到其他的 regionserver group中 下线一台机器 增加一台机器 移动 table 到其他 re ...
- zzuli 1875多线DP
1875: 蛤玮的财宝 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 530 Solved: 116 SubmitStatusWeb Board De ...
- Docker安装及基础知识
一.安装 & 启动 1.安装Docker [root@tokyo ~]# yum install docker 2.启动Docker服务 (1)旧式的 sysv 语法 [root@tokyo ...
- CachedThreadPool里的线程是如何被回收的?
线程池创建线程的逻辑图: 我们分析CachedThreadPool线程池里的线程是如何被回收的. //Executors public static ExecutorService newCached ...
- zookeeper server处理客户端命令的流程
zk server处理命令涉及到3个类,2个线程:一个命令请求先后经过PrepRequestProcessor,SyncRequestProcessor,FinalRequestProcessor. ...
- 李阳音标速成MP3文本
第一节:前元音 No. 1 [i:]穿针引线长衣音,简称"长衣音" 字母:e字母:ee 字母:ea字母:ie字母:ei 其发音要领是发音时舌尖抵下齿,前舌尽量抬高.舌位高于/i/: ...
- ASE加解密算法详细介绍
AEC扫盲主要增对CBC模式做详细讲解: https://blog.csdn.net/qq_28205153/article/details/55798628 AEC其他几种模式详细介绍 https: ...
- httpclient 使用代理
httpclient_使用代理 当爬取网页的时候,有的目标站点有反爬虫机制,对于频繁访问站点以及规则性访问站点的行为,会采用屏蔽IP的措施. 这时候代理IP就派上用场了. 代理的分类 透明代理 匿名代 ...
- 快速切题 poj3414 Pots
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10042 Accepted: 4221 Special J ...