zoukankan      html  css  js  c++  java
  • POJ 2312(优先队列+bfs)

           
                             Battle City
                
    Description
    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
     1 /*由于碰到brick walls步数会增2,所以要用优先队列,优先选取步数最小的进行扩展*/
     2 #include<cstdio>
     3 #include<queue>
     4 #include<cstring>
     5 using namespace std;
     6 
     7 char map[305][305];
     8 bool visit[305][305];
     9 int sx,sy,ex,ey,m,n;
    10 int d[4][2]={1,0,0,1,-1,0,0,-1};
    11 
    12 struct point
    13 {
    14     int x;
    15     int y;
    16     int step;
    17     bool operator <(const point &temp)const
    18     {
    19         return step>temp.step;
    20     }
    21 };
    22 
    23 int BFS()
    24 {
    25     point init;
    26     init.x=sx;
    27     init.y=sy;
    28     init.step=0;
    29     priority_queue<point>q;
    30     q.push(init);
    31     visit[init.x][init.y]=true;
    32     point node,newnode;
    33     while(!q.empty())
    34     {
    35         node=q.top();
    36         q.pop();
    37         if(node.x==ex&&node.y==ey)
    38         return node.step;
    39         for(int i=0;i<4;i++)
    40         {
    41             newnode.x=node.x+d[i][0];
    42             newnode.y=node.y+d[i][1];
    43             if(newnode.x>=0&&newnode.x<m&&newnode.y>=0&&newnode.y<n&&!visit[newnode.x][newnode.y])
    44             if(map[newnode.x][newnode.y]!='S'&&map[newnode.x][newnode.y]!='R')
    45             {
    46                 if(map[newnode.x][newnode.y]=='B')
    47                 newnode.step=node.step+2;
    48                 else
    49                 newnode.step=node.step+1;
    50                 q.push(newnode);
    51                 visit[newnode.x][newnode.y]=true;//这个一开始写外面了一直TLE
    52             }
    53         }
    54     }
    55     return -1;
    56 }
    57 
    58 int main()
    59 {
    60     //freopen("in.txt","r",stdin);
    61     int i,j;
    62     while(scanf("%d%d",&m,&n),m||n)
    63     {
    64         memset(visit,false,sizeof(visit));
    65         getchar();
    66         for(i=0;i<m;i++)
    67         {
    68             for(j=0;j<n;j++)
    69             {
    70                 scanf("%c",&map[i][j]);
    71                 if(map[i][j]=='Y')
    72                 sx=i,sy=j;
    73                 else if(map[i][j]=='T')
    74                 ex=i,ey=j;
    75                 else;
    76             }
    77             getchar();
    78         }
    79         int ans=BFS();
    80         printf("%d
    ",ans);
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    保存时出错jup
    Spyder默认页面布局调整
    根据所处位置提取单元格内容的函数(left、right、mid)和查找字符串位于单元格内容第几位的函数(find)
    excel打印出现多余空白页
    Excel的布尔值运算
    excel VBA一个fuction同时执行多个正则表达式,实现方法
    excel VBA把一个单元格内容按逗号拆分并依次替换到另一个单元格的括号里面(本题例子,把文本中的括号换成{答案}的格式,并按顺序填空)
    excel自动记录项目完成进度,是否逾期,逾期/提前完成天数,计算天数可以把now()改为today()
    jquery循环动画
    jquery动画(控制动画隐藏、显示时间轴)
  • 原文地址:https://www.cnblogs.com/homura/p/4691655.html
Copyright © 2011-2022 走看看