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 ...
随机推荐
- linux命令:env
env | grep DB ~/>env | grep DB KTK_NONDB_LOG=4
- 大数据高并发系统架构实战方案(LVS负载均衡、Nginx、共享存储、海量数据、队列缓存)
课程简介: 随着互联网的发展,高并发.大数据量的网站要求越来越高.而这些高要求都是基础的技术和细节组合而成的.本课程就从实际案例出发给大家原景重现高并发架构常用技术点及详细演练. 通过该课程的学习,普 ...
- 第四天学习内容 if switch for 的练习
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 如何使用Reaver破解Wi-Fi网络的WPA密码
via: http://lifehacker.com/5873407/how-to-crack-a-wi+fi-networks-wpa-password-with-reaver 译者:Mr小眼儿 本 ...
- 实现StatusBar的Flat风格
效果见右图,OfficeXP里就是这样的风格,其实实现很简单,不必专门在网上找别人控件. 把StatusBar的SimplePanel设为False,点击Panels添加StatusPanel,把所有 ...
- catalan 数——卡特兰数(转)
Catalan数——卡特兰数 今天阿里淘宝笔试中碰到两道组合数学题,感觉非常亲切,但是笔试中失踪推导不出来后来查了下,原来是Catalan数.悲剧啊,现在整理一下 一.Catalan数的定义令h(1) ...
- 基于visual Studio2013解决面试题之0203栈实现
题目
- display:table 水平居中
<div style="width:auto; margin:auto;display:table"> <div style="width: 100px ...
- javascript (八) 语法格式
JavaScript 对大小写敏感. JavaScript 对大小写是敏感的. 当编写 JavaScript 语句时,请留意是否关闭大小写切换键. 函数 getElementById 与 getEle ...
- 6.0RMB MP3所看到的……
产品篇: 偶然看到这个商品信息,作为电子开发人员,首先想到的便是采用了哪家芯片方案,怎么做到这么低的价格! 于是立刻买了一台回来,拆机如下: 成本BOM: ...