zoukankan      html  css  js  c++  java
  • 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; }


  • 相关阅读:
    Delphi开发动态连接库的方法和规范
    EntityFramework+DomainDataSource+Silverlight完成数据读取分页排序与修改
    啊啊啊啊
    HTML、CSS、JavaScript从零开始系列文章
    从JAVA学思想,在.Net用……关于各种模型,备案以便查
    为ExtJS 4 系列树添加可将节点拖动到叶子节点上的功能,可配置
    在使用WCF RIA Services时所要注意的,不断更新中……
    修复EXTJS 4.0.2a在ie9与FIREFOX下字体过小、表格头部字体在Chrome下模糊的CSS补丁
    修复EXTJS 4.0.2a下面gridFilter的type为list时,不能从服务器读取列表数据的bug
    大学生程序员,"我们将要何去何从?!"
  • 原文地址:https://www.cnblogs.com/Accepting/p/11234770.html
Copyright © 2011-2022 走看看