NYOJ 284 坦克大战 【BFS】+【优先队列】
坦克大战
- 描写叙述
-
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 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?- 输入
- The input consists of several test cases. The first line of each 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.
- 输出
- For each test case, please output the turns you take at least in a separate line. If you can't arrive at the target, output "-1" instead.
- 例子输入
-
3 4
YBEB
EERE
SSTE
0 0 - 例子输出
-
8
開始用DFS,结果超时,然后百度的结果是要用优先队列,之前从没见过这玩意儿,然后折腾了一夜晚,第二天才写出来。
#include <cstdio>
#include <cstring>
#include <queue>
using std::priority_queue;
int m, n;
char map[302][302];
bool vis[302][302];
struct Node{
int x, y, steps;
friend bool operator<(Node a, Node b){
return a.steps > b.steps;
}
} you, tar;
int mov[][2] = {0, 1, 0, -1, 1, 0, -1, 0};
priority_queue<Node> PQ; int check(Node a){
if(a.x < 0 || a.y < 0 || a.x >= m || a.y >= n)
return 0;
if(vis[a.x][a.y]) return 0;
if(map[a.x][a.y] == 'B') return 2;
if(map[a.x][a.y] == 'E') return 1;
if(map[a.x][a.y] == 'T') return 1;
return 0;
} int BFS(){
Node temp, sta;
int count;
vis[you.x][you.y] = 1;
PQ.push(you);
while(!PQ.empty()){
sta = temp = PQ.top(); PQ.pop();
for(int i = 0; i < 4; ++i){
temp.x += mov[i][0];
temp.y += mov[i][1];
if(count = check(temp)){
temp.steps += count;
if(map[temp.x][temp.y] == 'T')
return temp.steps;
vis[temp.x][temp.y] = 1;
PQ.push(temp);
}
temp = sta;
}
}
return -1;
} int main(){
while(scanf("%d%d", &m, &n), m || n){
for(int i = 0; i < m; ++i){
scanf("%s", map[i]);
for(int j = 0; j < n; ++j)
if(map[i][j] == 'Y') you.x = i, you.y = j;
else if(map[i][j] == 'T') tar.x = i, tar.y = j;
}
memset(vis, 0, sizeof(vis));
while(!PQ.empty()) PQ.pop();
printf("%d\n", BFS());
}
return 0;
}
NYOJ 284 坦克大战 【BFS】+【优先队列】的更多相关文章
- NYOJ 284 坦克大战 bfs + 优先队列
这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 #include <stdio.h> #include <iostream ...
- nyoj 284 坦克大战 (优先队列)
题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284 特殊数据: 5 5 BBEEY EEERB SSERB SSERB SSETB 7 非 ...
- nyoj 284 坦克大战 简单搜索
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...
- NYOJ 284 坦克大战 (广搜)
题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...
- nyoj 483 Nightmare【bfs+优先队列】
Nightmare 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He found him ...
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- nyoj-----284坦克大战(带权值的图搜索)
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" ...
- java制作简单的坦克大战
坦克大战是我们小时候玩红白机时代的经典游戏,看到有不少小伙伴都使用各种语言实现了一下,手痒痒,也使用java做的一个比较简单的坦克大战,主要面向于学过Java的人群,与学了一段时间的人,有利于面向对象 ...
- 3D坦克大战游戏源码
3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...
随机推荐
- 重操JS旧业第十一弹:BOM对象
BOM对象即浏览器内置对象,现今流行的浏览器内核有Safri,Firefox,Chrome,Opera,IE其中IE的兼容性是最蛋疼的在10及其过后还好点,但是现在IE基本上淘汰,而国内像360这种垃 ...
- QLockFile,QRunInfo
http://doc.qt.io/qt-5/qlockfile.html http://www.dushibaiyu.com/2014/10/qruninfo-api-smple.html
- MapReduce/Hbase进阶提升(原理剖析、实战演练)
什么是MapReduce? MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",和他们 ...
- 基于visual Studio2013解决C语言竞赛题之1061最大值和次最大值
题目 解决代码及点评 /* 功能: 编写子函数, 求一维整型数组M[10]的最大值及次最大值(次最大值可能不存在). 主函数中输入10个整数, 然后调用上述子函数, 若次最大值存在, ...
- boost::asio async_write也不能保证一次发完所有数据 二
只有看boost源码才能弄明白发生了什么.首先我是将vector里面写入了数据,然后用boost::asio::buffer将vector构造成了mutable_buffer_1对象. 参考该文档的重 ...
- ACM POJ 2192 Zipper
题目大意:输入字符串a,b,c 要求推断c是否有a,b中的个字符保持原有顺序组合而成. 算法思想: DP 用dp[i][j]表示a的前0~i-1共i个字符和b的前0~j-1共j个字符是否构成c[i+j ...
- ASP.NET - TreeView 增删
效果: 前端代码: <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Si ...
- EF具体用在什么类型的项目上
一般来说,使用EF框架,肯定会比直接使用ADO.NET,消耗的时间多一些. 因为使用ADO.NET直接把SQL语句传回数据库执行. 而使用EF框架的话,会把所用到的尸体,转换成相对应得SQL,然后再传 ...
- 进阶:案例三: Upload File using WebDynpro
1.节点创建,其中DATASOURCE存放uploadfile名称 2.layout布局 3.upload事件代码: method ONACTIONUPLOAD . DATA: lo_Node typ ...
- innerXml,outerXml,innerText的不同
原文:innerXml,outerXml,innerText的不同 昨天看到咱们园子里有一个仁兄写的关于xml的有关操作,在读的过程中,由于是初学者有不明白的地方就查资料,发现自己多innerXml, ...