• C


    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

    思路:

    遇见B就加2遇见E加1,,其他的不可以走。。

    #include<iostream#include<queue>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    const int N=310;
    char arr[N][N];
    int mark[N][N];
    int sa,ea,st,et;
    int n,m;
    struct stu{
        int x,y;
        int s;
        friend bool operator<(stu a,stu b){
            return a.s>b.s;//步数小的优先
        }
    }e1,e2;
    int base[4][2]={1,0,-1,0,0,1,0,-1};
    //4个方向
    void bfs(int x1,int y1,int x2,int y2){ memset(mark,0,sizeof(mark)); priority_queue<stu> que; e1.x=x1,e1.y=y1,e1.s=0;
    mark[x1][y1]
    =1;
    que.push(e1);
    int ans=-1;
    while(que.size()){ e1=que.top(); que.pop();
    if(e1.x==x2&&e1.y==y2) { ans=e1.s; break; } for(int i=0;i<4;i++){ e2.x=e1.x+base[i][0]; e2.y=e1.y+base[i][1
    if(e2.x<0||e2.x>=n||e2.y<0||e2.y>=m) continue;//判断越界 if(mark[e2.x][e2.y] == 1) continue;//判断是否走过 if(arr[e2.x][e2.y]=='S'||arr[e2.x][e2.y]=='R') continue;//判断是否可以走 if(arr[e2.x][e2.y]=='B') {//如果为B就加2 e2.s=e1.s+2; } else e2.s=e1.s+1; que.push(e2); mark[e2.x][e2.y] = 1; } } if(ans == -1) puts("-1"); else printf("%d ", ans); } int main(){ int x1,y1,x2,y2; while(cin>>n>>m){ if(n==0||m==0) break; for(int i=0;i<n;i++){ scanf("%s",&arr[i]); } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(arr[i][j]=='Y'){ x1=i; y1=j; } else if(arr[i][j]=='T'){ x2=i; y2=j; } } } bfs(x1,y1,x2,y2); } return 0; }


  • 相关阅读:
    SpringBoot-07:SpringBoot整合PageHelper做多条件分页查询
    SpringBoot-06:SpringBoot增删改查一套完整的考试案例
    SpringBoot-05:SpringBoot初运行以及tomcat端口号的修改
    SpringBoot-04:SpringBoot在idea中的俩种创建方式
    SpringBoot-03:SpringBoot+Idea热部署
    SpringBoot-02:SpringBoot中的POM文件详细解释
    SpringBoot-01:什么是SpringBoot?
    SSM-Spring-23:概念《Spring中的事务是什么?》
    二叉查找树 & B(B-)树 & B+树 & B*树
    静态资源缓存与更新
  • 原文地址:https://www.cnblogs.com/Accepting/p/11234770.html
Copyright © 2020-2023  润新知