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 ...
随机推荐
- Appium 命令行启动配置
Appium 安装过后,会有图形界面,同样也支持命令行参数的启动和配置 Windws: 在windows 安装配置Appium有三种方式: Node install -g appium .exe文件安 ...
- qt下的跨目录多工程编译
原地址:http://blog.csdn.net/fjb2080/article/details/7386292 转自:http://blog.csdn.net/high_high/article/d ...
- 我的mysql数据库sql优化原则
原文 我的mysql数据库sql优化原则 一.前提 这里的原则 只是针对mysql数据库,其他的数据库 某些是殊途同归,某些还是存在差异.我总结的也是mysql普遍的规则,对于某些特殊情况得特殊对待. ...
- smartforms初始化
smartforms 第一次打开的页面是和prd环境下的一样,需要跑一个程序才能编辑
- 暴力拆解CPU
http://www.ruanyifeng.com/blog/2010/11/cpu_autopsy.htmlhttp://bbs.mydigit.cn/read.php?tid=110272http ...
- uva 147 Dollars(完全背包)
题目连接:147 - Dollars 题目大意:有11种硬币, 现在输入一个金额, 输出有多少种组成方案. 解题思路:uva 674 的升级版,思路完全一样, 只要处理一下数值就可以了. #inclu ...
- POJ训练计划3041_Asteroids(二分图/最小点覆盖=最大匹配)
解题报告 http://blog.csdn.net/juncoder/article/details/38135053 题目传送门 题意: 给出NxN的矩阵,有M个点是障碍 每次仅仅能删除一行或者一列 ...
- 华为OJ培训主题 比赛统计
题目例如以下: 比赛情况统计 有一个游戏平台,各个參赛队伍(以唯一的TeamID来标识)之间进行单循环的对抗赛,两个队伍之间仅仅举行一场比赛,比赛以得分的多少定胜负.须要完毕一个统计赛况的程序,可以随 ...
- ALV判断修改后是否有不合法数据,有则选中错误行,高亮度显示。
alv数据表维护表时错误行需要高亮度显示 gt_index_rows TYPE lvc_t_row,"用以存放要选择行的内表 gs_index_rows TYPE lvc_s_row.&qu ...
- nmap -- write a nmap script
漏洞扫描 --编写Nmap脚本 2006年12月份,Nmap4.21 ALPHA1版增加脚本引擎,并将其作为主线代码的一部分.NSE脚本库现在已经有400多个脚本.覆盖了各种不同的网络机制(从SMB漏 ...