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 ng-bootstrap和ngx-bootstrap区别
https://angular.cn/resources ngx-bootstrap 安装: npm install ngx-bootstrap --save 再引入css <link href ...
- Android之MVP模式实现登录和网络数据加载
MVP简介 相信大家对 MVC 都是比较熟悉了:M-Model-模型.V-View-视图.C-Controller-控制器,MVP作为MVC的演化版本,也是作为用户界面(用户层)的实现模式,那么类似的 ...
- Confluence 6 LDAP 用户结构设置
用户对象类(User Object Class) 这个是在 LDAP 用户对象中对用户分类的名字.例如: user 用户对象过滤器(User Object Filter) 当对用户对象进行搜索的时候 ...
- hdu6405Make ZYB Happy 广义sam
题意:给出n(n<=10000)个字符串S[1~n],每个S[i]有权值val[i],随机等概率造一个由小写字母构成的字符串T,Sum = 所有含有子串T的S[i]的val[i]之积,求Sum的 ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- 在菜鸟教程学 HTML(一)
注意:对于中文网页需要使用 <meta charset="utf-8"> 声明编码,否则会出现乱码.有些浏览器会设置 GBK 为默认编码,则你需要设置为 <met ...
- List原理
ArrayList: ArrayList是一个可变数组实现,实现了List接口的所有方法,并允许存取null值.ArrayList基本上等同与Vector,但它只对writeObject()和read ...
- zk请求和响应对
zk的请求和响应是通过id对应上的: 请求头(RequestHeader)和响应头(ReplyHeader)共用一个xid,它的本质是ClientCnxn类中的一个计数器. 1. 首先看客户端: Pa ...
- MySQL5.7 主从复制配置
一.主从复制原理 MySQL 主从复制是一个异步的复制过程,主库发送更新事件到从库,从库读取更新记录,并执行更新记录,使得从库的内容与主库保持一致.每一个主从复制的连接,都有三个线程.拥有多个从库的主 ...
- 【DevExpress v17.2新功能预告】增强ASP.NET TreeList
本文主要为大家介绍在下一个主要版本v17.2中,DevExpress ASP.NET TreeList获得的一些重大改进.DevExpress ASP.NET TreeList和GridView控件在 ...