zoukankan      html  css  js  c++  java
  • nyo-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  
     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("
    ");
    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
    ",bfs(x,y));
    78     }
    79 }
    80         
    View Code
  • 相关阅读:
    verilog 基础知识
    excel函数提取身份证出生日期,分离日期时间的日期和时间
    excel函数提取内容中的汉字
    excel匹配函数vlookup和lookup
    excel计数函数COUNTIF、COUNTIFS
    excel函数sum、sumif和sumifs
    excel替换函数substitute
    excel判断数据是否存在另一列中
    python用random模块模拟抽奖逻辑(print修改end参数使打印结果不分行)
    excel用函数去掉单元格内容中的括号,并只保留单元格里面的内容
  • 原文地址:https://www.cnblogs.com/zsj-93/p/3185591.html
Copyright © 2011-2022 走看看