nyoj 284 坦克大战【bfs】

坦克大战

时间限制:1000 ms  |  内存限制:65535 KB

难度:3

描述

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

【思路】1 遇到 河‘R’、钢墙‘S’过不去, 遇到‘E’用时1, 遇到砖墙‘B’用时2;        2 此题是在迷宫问题的基础上做了些改动,就是地图上能走的点可能耗费时间1,也可能耗费时间2。那么,元素在出队列时,不能简单的按照以前的入队顺序出队了,而应该让时间最短的先出队,这样就能够保证先入队的点一定是时间最短的,那么搜到终点时也时间也一定最小。现在回头想下,迷宫问题之所以没有考虑这个问题,是因为先入队的点的时间一定不大于后入队的。言归正传,如何让时间最短的先出队呢?----------STL已经帮我们弄好了,priority_queue;
优先队列+bfs重载方法一:
 1 struct node
 2 {
 3     int x,y;
 4     int step;
 5 };
 6 priority_queue<node>q;       //优先队列中元素的比较规则默认是按元素的值从大到小排序;
 7
 8 bool operator<(const node &a,const node &b) //括号里面是const 而且还必须是引用
 9 {
10     return a.step > b.step;          //从小到大排序。重载小于号。因为默认是从大到小
11 }

重载方法二:

 1 struct node
 2 {
 3     int x,y;
 4     int step;  //定义一个优先队列
 5     friend bool operator<(node a, node b)
 6     {     //从小到大排序采用“>”号;如果要从大到小排序,则采用“<”号
 7         return a.step > b.step;       //从小到大排序
 8     }
 9 };
10 priority_queue<node>q;       //优先队列中元素的比较规则默认是按元素的值从大到小排序;
切记:从小到大排序采用“>”号;如果要从大到小排序,则采用“<”号;

AC代码:
 1 #include<cstdio>
 2 #include<queue>
 3 #include<string.h>
 4 using namespace std;
 5 #define max 310
 6 int n, m, sx, sy, ex, ey;
 7 char map[305][305];
 8 int dx[4]={0, 0, 1, -1};
 9 int dy[4]={1, -1, 0, 0};
10 struct node{
11     int x, y, step;
12     friend bool operator < (node a, node b)
13     {
14         return a.step > b.step;//步数少的优先
15     }
16 }a, b;
17 int judge(int x, int y)
18 {
19     if(x < 0 || x >= m || y < 0 || y >= n)
20         return false;
21     if(map[x][y] == ‘R‘ || map[x][y] == ‘S‘)
22         return false;
23     return true;
24 }
25 bool bfs()
26 {
27     int flag = 0;
28     priority_queue<node>q;
29     a.x = sx; a.y = sy; a.step = 0;
30     q.push(a);
31     while(!q.empty())
32     {
33         b = q.top();
34         q.pop();
35         if(b.x == ex && b.y == ey)
36         {    flag = 1;    break;   }
37         for(int k = 0; k < 4; k++)
38         {
39             a.x = b.x + dx[k];
40             a.y = b.y + dy[k];
41             if(judge(a.x, a.y))
42             {
43                 if(map[a.x][a.y] == ‘B‘)
44                     a.step = b.step + 2;
45                 else
46                     a.step = b.step + 1;
47                 map[a.x][a.y] = ‘S‘;
48                 q.push(a);
49             }
50         }
51     }
52     if(flag) printf("%d\n", b.step);
53     else printf("-1\n");
54 }
55 int main()
56 {
57     while(scanf("%d %d", &m, &n) != EOF)
58     {
59         if(m + n == 0) break;
60         for(int i = 0; i < m; i++)
61         {
62             scanf("%s", &map[i]);
63             for(int j = 0; j < n; j++)
64             {
65                 if(map[i][j] == ‘Y‘)
66                 {   sx = i; sy = j;   }
67                 if(map[i][j] == ‘T‘)
68                 {   ex = i; ey = j;   }
69             }
70         }
71         bfs();
72     }
73     return 0;
74 }

时间: 2024-10-15 22:40:35

nyoj 284 坦克大战【bfs】的相关文章

NYOJ 284 坦克大战 bfs + 优先队列

这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 1 #include <stdio.h> 2 #include <iostream> 3 #include <queue> 4 #include <string.h> 5 using namespace std; 6 typedef struct Node{ 7 int x, y; 8 int step; 9 friend bool operator

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 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 co

nyoj 284 坦克大战 (优先队列)

题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284 特殊数据: 5 5 BBEEY EEERB SSERB SSERB SSETB 7 非优先队列: 1 2 #include<iostream> 3 #include<cstdio> 4 #include<algorithm> 5 #include<cstring> 6 #include<string> 7 #include<cm

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 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 space

坦克大战(版本2.5-版本2.9)

版本2.5 功能:添加“血块”步骤:        1)添加blood类        2)添加必要的方法:eat方法等        3)让blood对象固定轨迹运动, 并在一定时间后消失 具体代码实现: 新增的blood类: 1 import java.awt.Color; 2 import java.awt.Graphics; 3 import java.awt.Rectangle; 4 5 //模拟血块,坦克吃了可以补血 6 public class Blood { 7 int x, y

Java__线程---基础知识全面实战---坦克大战系列为例

今天想将自己去年自己编写的坦克大战的代码与大家分享一下,主要面向学习过java但对java运用并不是很熟悉的同学,该编程代码基本上涉及了java基础知识的各个方面,大家可以通过练习该程序对自己的java进行一下实战. 每个程序版本代码中,都附有相关注释,看完注释大家就可以对本程序设计有个很明显的思路.真的很有趣,想对java重新温习的同学完全不必再对厚厚的java基础书籍进行阅读了,在跟着本次代码练习并分析后,大家一定会对java各方面基础知识 尤其是线程的知识有更深一步的了解!!! 本次坦克大

nyoj-----284坦克大战(带权值的图搜索)

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 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 co

nyoj 284

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 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 co

DS课设【坦克大战最短路】(MummyDing)

DS课设[坦克大战最短路] 还是决定写点东西简单记录下这次编码. 一.想法 还没放假的时候只想着用C#实现,算法图论方面觉得图论方向会靠谱些,但一直没有什么好点子.C#以前也没学过,自信来源于MFC的学习经历(以前也是用它做了C语言课设).C#应该是没有MFC那么复杂的,心想看几天应该就可以上手一些小东西了,事实证明也如此. 寒假时间相对以前更长,也并不着急做课设.开始一段是刷题+学习Kinect+顺带了解Kinect,后来在刷题过程中遇到这题,还蛮有意思的,当即就写了个"坦克大战最短路简单设计