坦克大战

时间限制: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 }

转载于:https://www.cnblogs.com/123tang/p/5869231.html

nyoj 284 坦克大战【bfs】相关推荐

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

    题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284 特殊数据: 5 5 BBEEY EEERB SSERB SSERB SSETB 7 非 ...

  2. nyoj 284 坦克大战

    典型的广度优先搜索, 题意:图中Y代表起点,T代表终点,S代表铁墙,不可被击毁也不可被通过,B代表木墙,可以被击毁,击毁后可以通过,R代表河流,不可击毁也不可通过,E代表可通过. 击毁木墙会消耗一步, ...

  3. HYOJ 284 坦克大战

    描述 Many of us had played the game "Battle city" in our childhood, and some people (like me ...

  4. 坦克大战 bfs 优先队列

    题目描述 Many of us had played the game "Battle city" in our childhood, and some people (like ...

  5. nyoj284 坦克大战(dijkstra(bfs+优先队列))

    题目284 题目信息 运行结果 本题排行 讨论区 坦克大战 时间限制: 1000 ms  |  内存限制: 65535 KB 难度: 3 描述 Many of us had played the ga ...

  6. 坦克大战(bfs简单变形)

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" i ...

  7. NYOJ-284 坦克大战

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" i ...

  8. 《HTML5经典坦克大战》游戏(代码)

    前几天粗略地学了HTML5,然后就用它写了一个<经典坦克大战>游戏. 现在想分享一下我写的代码,写得不好请大家多多指教. 给大家推荐一个网站,这个网站是为大学生而做,为方便学习编程的同学而 ...

  9. 优先级队列广搜——坦克大战

    坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in ...

最新文章

  1. 【机器学习】基于蚁群算法的多元非线性函数极值寻优
  2. C语言易错题集 第四部
  3. Hadoop中Context类的作用
  4. ECCV 2018 《Triplet Loss in Siamese Network for Object Tracking》论文笔记
  5. vim傻瓜式配置 + git clone的速度慢到难以忍受问题的解决方法~
  6. python中count()方法
  7. Node + ts + puppeteer e2e前端自动化测试
  8. 11岁过python1级_11岁表弟写的Python零基础入门笔记!
  9. Qt--基础图形绘制
  10. tcp/ip知识点的总结
  11. 子集生成-增量构造法||位向量法
  12. 20190908:(leetcode习题)最大子序和
  13. RDD Persistence持久化
  14. H.264抗误码策略及FMO
  15. 读取cpu温度的api_获取传感器温度-cpu 温度篇
  16. 7-8 愿天下有情人都是失散多年的兄妹 (25分)
  17. 数据结构——数组以及n维数组
  18. 小米手机(MIUI)介绍以及工程机评测 【持续更新】
  19. “程序包com.sun.image.codec.jpeg不存在“ 正解
  20. ​听六小桨讲AI | 第7期:3D卷积和分组卷积

热门文章

  1. 推荐一款简单的页面加密网页(免费的哦)
  2. html5 双屏互动原理,一种全新的双屏互动智慧教室设计方案解析
  3. 用Python分析深圳在售二手房数据,看看买房需要多少预算
  4. 解决 操作必须使用一个可更新的查询的错误
  5. 笑话类网站推广的几种很实用的方法
  6. 哈佛管理论丛-谁背上了猴子(转)
  7. php前台可自定义框架,实现Discuz!前台DIY自定义框架比例
  8. 商城网站建设怎么做?这五个技巧告诉你!
  9. 关于我求是不是质数的一个错误,输入9判断是质数的原因
  10. WOS(SCI)爬虫案例