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

本文深入探讨了游戏开发中涉及的多种技术,包括Unity、Cocos2d-X等游戏引擎的应用,以及AI音视频处理技术,如语音识别、图像处理等。详细介绍了如何利用这些技术实现更丰富、互动性强的游戏体验。

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

题目链接http://acm.nyist.net/JudgeOnline/problem.php?pid=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 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 .

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
来源
POJ
题目大意:
在地图中有几种障碍物,“S” 金属块(你不能打破),“B”砖块,可以打破它(需要一分钟,打破之后就相当于没有障碍物了,即穿过一个砖块要两分钟),“R”河流(不能通过),“E”没有障碍物(走一步要一分钟)。现在你处于“Y”,从“Y”走到“T”,输出需要的最短时间。
解题思路:
刚开始,我直接是用的BFS,结果WA,后来在讨论区发现一个测试数据,结果发现自己的程序确实过不了这个数据。之后才知道这个题目要用优先队列+BFS,要好好理解这些数据结构才能快速切题,加油
关于优先队列的使用方法其实和队列差不多,可以在这里看看用法: http://blog.youkuaiyun.com/yin_zongming/article/details/21559981
下面是AC代码
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif
#define maxn 300+10
int vis[maxn][maxn];
char v[maxn][maxn];
int dir[4][2]={-1,0,1,0,0,1,0,-1};
int m,n;
int bx,by;
int cnt;
struct point
{
    int x,y,step;
    friend bool operator < (const point &s1,const point &s2)//改变优先级,由于优先队列默认是大的数字优先级高
    {                                                       //现在改为step小的优先级高,符合题意
        return s1.step>s2.step;
    }
};
void bfs(int x,int y)
{
    priority_queue<point>p;
    point t,q;
    t.x=x;t.y=y;t.step=0;
    p.push(t);
    while(!p.empty())
    {
        t=p.top();//优先队列是用top获取队列头部的元素,而队列是用front,注意
        p.pop();
        if(v[t.x][t.y]=='T')
        {
            cnt=t.step;
            return ;
        }
        for(int i=0;i<4;i++)
        {
            q.x=t.x+dir[i][0];
            q.y=t.y+dir[i][1];
            if(q.x>=0&&q.x<m&&q.y>=0&&q.y<n&&!vis[q.x][q.y]&&v[q.x][q.y]!='S'&&v[q.x][q.y]!='R')
            {
                vis[q.x][q.y]=1;
                if(v[q.x][q.y]=='B')
                {
                    q.step=t.step+2;
                    p.push(q);
                }
                else
                {
                    q.step=t.step+1;
                    p.push(q);
                }
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&m,&n)&&m&&n)
    {
        for(int i=0;i<m;i++)
        {
            scanf("%s",v[i]);
            for(int j=0;j<n;j++)
            {
                if(v[i][j]=='Y')
                {
                    bx=i;
                    by=j;
                }
            }
        }
        memset(vis,0,sizeof(vis));
        vis[bx][by]=1;
        cnt=0;
        bfs(bx,by);
        if(cnt)
            printf("%d\n",cnt);
        else
            printf("-1\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值