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 }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值