nyo-284-坦克大战--(bfs+优先队列)

本文介绍了一款简化版“坦克大战”游戏的地图遍历算法。玩家需操控坦克穿越空地、河流、砖墙及钢铁墙等地形,在不受敌方干扰的情况下尽快获取奖励。文章详细解释了如何使用广度优先搜索算法计算从起点到目标点所需的最少回合数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

坦克大战

时间限制: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  
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<queue>
 5 using namespace std;
 6 int dx[4]={-1,1,0,0};
 7 int dy[4]={0,0,-1,1};
 8 char map[305][305];
 9 int vis[305][305];
10 int n,m;
11 struct node
12 {
13     int x,y,step;
14     bool operator < (node b) const{
15         return step>b.step;
16     }
17 };
18 int bfs(int x,int y)
19 {
20     node p,q;
21     priority_queue <node> Q;
22     while(!Q.empty()) Q.pop();
23     vis[x][y]=1;
24     p.x=x;p.y=y;
25     p.step=0;
26     Q.push(p);
27     while(!Q.empty())
28     {
29         q=Q.top();
30         Q.pop();
31         for(int i=0;i<4;i++)
32         {
33             p.x=q.x+dx[i];
34             p.y=q.y+dy[i];
35             p.step=q.step;
36             if(!vis[p.x][p.y]&&p.x>=0&&p.x<n&&p.y>=0&&p.y<m)
37             {
38                 if(map[p.x][p.y]=='T') return p.step+1;
39                 if(map[p.x][p.y]=='B')
40                 {
41                     p.step+=2;
42                     //printf("%d ",p.step);
43                     vis[p.x][p.y]=1;
44                     Q.push(p);
45                 }
46                 if(map[p.x][p.y]=='E')
47                 {
48                     p.step++;
49                    // printf("%d ",p.step);
50                     vis[p.x][p.y]=1;
51                     Q.push(p);
52                 }
53             }
54         }//printf("\n");
55     }
56     return -1;
57 }
58 int main()
59 {
60     while(scanf("%d%d",&n,&m),n,m)
61     {
62         getchar();
63         memset(vis,0,sizeof(vis));
64         int x,y;
65         for(int i=0;i<n;i++)
66         {
67             for(int j=0;j<m;j++)
68             {
69                 scanf("%c",&map[i][j]);
70                 if(map[i][j]=='Y')
71                 {
72                     x=i;y=j;
73                 }
74             }
75             getchar();
76         }
77         printf("%d\n",bfs(x,y));
78     }
79 }
80         
View Code

 

转载于:https://www.cnblogs.com/zsj-93/p/3185591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值