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
  • 相关阅读:
    最近比较忙
    堆栈的实现
    推荐一款开源优秀Javascript编辑器Aptana Studio
    OOJ面向对象编程的三大特点封装,继承,多态分析与实例
    ASP.NET设计模式之单体模式Singleton
    OOJ面向对象的JAVASCRIPT(一)
    【转】性能优化关于Asp.net性能的技巧
    性能优化关于JQuery的性能优化
    学习javascript的动态this指针与动态绑定 call与apply函数的应用
    OOJ面向对象的JAVASCRIPT(二)
  • 原文地址:https://www.cnblogs.com/zsj-93/p/3185591.html
Copyright © 2011-2022 走看看