zoukankan      html  css  js  c++  java
  • Battle City 优先队列+bfs

    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?
    Input
    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.
    Output
    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.
    Sample Input
    3 4
    YBEB
    EERE
    SSTE
    0 0
    
    Sample Output
    8


    这道题一开始就想到了优先队列,可就是不对,原来是优先队列建立的位置不对,建立在函数外,如果队列不空,会影响下组的结果,所以让他随着函数共存亡把


    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <string>
    #include <cmath>
    using namespace std;
    int n,m,sx,sy,tx,ty;
    char map[300][300];
    char vis[300][300];
    int dir[4][2]={0,1,1,0,0,-1,-1,0};
    struct que
    {
        int x,y,d;
        friend bool operator <(que a,que b)
        {
            return a.d>b.d;
        }
    }temp,cn;
    //priority_queue<que> q;///优先队列要建立在函数里 否则队列不空影响结果
    int bfs(int x,int y)
    {
        priority_queue<que> q;
        temp.x=x,temp.y=y,temp.d=0,vis[sx][sy]=1;
        q.push(temp);
        while(!q.empty())
        {
            cn=q.top();
            q.pop();
            for(int i=0;i<4;i++)
            {
                tx=cn.x+dir[i][0];
                ty=cn.y+dir[i][1];
                if(tx<0||ty<0||tx>=n||ty>=m||vis[tx][ty])continue;
                vis[tx][ty]=1;
                int d=cn.d+(map[tx][ty]=='B'?2:1);
                    temp.x=tx,temp.y=ty,temp.d=d;
                    q.push(temp);
                if(map[tx][ty]=='T')return d;
            }
        }
        return -1;
    }
    int main()
    {
       while(scanf("%d%d",&n,&m))
       {
           if(!n&&!m)break;
           memset(vis,0,sizeof(vis));
           sx=sy=0;
           for(int i=0;i<n;i++)
           {
               for(int j=0;j<m;j++)
               {
                   cin>>map[i][j];
                   if(map[i][j]=='Y')sx=i,sy=j;
                   else if(map[i][j]=='S'||map[i][j]=='R')vis[i][j]=1;
               }
           }
           cout<<bfs(sx,sy)<<endl;
       }
    }
  • 相关阅读:
    C语言截取从某位置开始指定长度子字符串方法
    vim:放弃hjkl
    vim资源
    PHP和.NET通用的加密解密函数类,均使用3DES加解密 .
    Java与.NET DES加密解密互转
    案例:使用正则表达式的爬虫
    爬虫的正则表达式re模块
    爬虫中Requests模块
    Oracle系列十一 数据处理
    爬虫urllib2 的异常错误处理URLError和HTTPError
  • 原文地址:https://www.cnblogs.com/8023spz/p/7218456.html
Copyright © 2011-2022 走看看