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 ...
随机推荐
- MFC 在对话框显示图片的多种方法(四种方法)
我们先从简单的开始吧.先分一个类: (一) 非动态显示图片(即图片先通过资源管理器载入,有一个固定ID) (二) 动态载入图片(即只需要在程序中指定图片的路径即可载入) 为方便说明,我们已经建好一个基 ...
- 8天玩转并行开发——第三天 plinq的使用
原文 8天玩转并行开发——第三天 plinq的使用 相信在.net平台下,我们都玩过linq,是的,linq让我们的程序简洁优美,简直玩的是爱不释手,但是传统的linq只是串行代码,在并行的 年代如果 ...
- 基于visual Studio2013解决C语言竞赛题之1081shell排序
题目 解决代码及点评 /************************************************************************/ /* ...
- mfc修改应用程序外观
1.在窗口创建前修改窗体外观 在BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)函数中修改,其中CREATESTRUCT结构中有诸如窗口大小 ...
- nginx源代码分析--模块分类
ngx-modules Nginx 基本的模块大致能够分为四类: handler – 协同完毕client请求的处理.产生响应数据.比方模块, ngx_http_rewrite_module, ngx ...
- java.lang.NoSuchMethodError: org.springframework.beans.factory.annotation.InjectionMetadata.<init>(L
关于错误: java.lang.NoSuchMethodError: org.springframework.beans.factory.annotation.InjectionMetadata.&l ...
- 改变Edit的光标(使用CreateCaret,ShowCaret和LoadBitmap三个API函数)
看着Edit的光标,是不是觉得了无生趣,想不想换个形状来玩玩,其实很简单,且听我道来. Edit是Windows的标准控件,它是一个系统范围窗口类,所以任何应用程序都能创建它.其实Edit本质上也是一 ...
- RAC 备份到本地不同设备
- PhoneGap-----Contacts
Everything in the code!!! <!DOCTYPE html> <html> <head> <title>Contact Examp ...
- C语言盲点笔记1
寥寥数笔,记录我的C语言盲点笔记,仅仅为以前经历过,亦有误,可交流. 1.int* a和int *a有差别吗? 没有不论什么差别,都表示a是int指针 建议这么写int *a;这样明显一点 理由例如以 ...