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 ...
 
随机推荐
- java 基于JDK中的源码总结下String二
			
申明:转载请注明出处,如有商用目的请务必知会本人,感谢. 上一篇文章:http://blog.csdn.net/ts1122/article/details/8738336,介绍了String一些易错 ...
 - GDI+: Curved Shapes
			
原文 http://www.functionx.com/vcsharp2003/gdi/curves.htm Curves Introduction to Curves A curve is ...
 - jqueryui datepicker refresh
			
http://stackoverflow.com/questions/6056287/jquery-ui-datepicker-prevent-refresh-onselect 给选中的TD加背景色
 - yii Query Builder (yii 查询构造器) 官方指南翻译
			
/**** Query Builder translated by php攻城师 http://blog.csdn.net/phpgcs Preparing Query Builder 准备 Quer ...
 - cookie的path和domain參数实例解析
			
一句话概括两个參数含义各为: path表示cookie所在的文件夹 domain表示的是cookie所在的域,默觉得请求的地址 首先改动我们的 hosts 文件 我本机内网ip 192.168.1.1 ...
 - Android  switch 中 case expressions must be constant expressions 错误
			
刚才导入android zxing 条码 的demo测试,发现出现如下错误 case expressions must be constant expressions 经检查,项目被设置成librar ...
 - sqlserver存储过程学习笔记(一)基础知识篇(全)
			
说出来有点丢人,做sqlserver应用系统近一年,竟然没有使用过存储过程,现在就好好的梳理一下对应知识,慢慢让其加入到我的项目中去吧. 存储过程的优点:1.运行效率高,提供了在服务器端快速执行sql ...
 - Centos系统各种日志存详解
			
Centos系统各种日志存储路径和详细介绍 Linux常见的日志文件详述如下 1./var/log/boot.log(自检过程) 2./var/log/cron (crontab守护进程crond所派 ...
 - ntohs, ntohl, htons,htonl的比较和详解
			
在C/C++写网络程序的时候,往往会遇到字节的网络顺序和主机顺序的问题. 这时就可能用到htons(), ntohl(), ntohs(),htons()这4个网络字节顺序与本地字节顺序之间的转换函数 ...
 - Linux vmstat命令详解
			
vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...