zoukankan      html  css  js  c++  java
  • NYOJ 284 坦克大战 (广搜)

    题目链接

    描述

    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

    分析:

    因为涉及到当前点往下走的时候是优先走空地还是能够被击破的墙(两个花费的时间不一样),所以应该用优先队列来处理,刚开始做的时候没有考虑到这一点。

    代码:

    include<stdio.h>

    include

    include

    include<string.h>

    using namespace std;
    int m,n;
    int sx,sy,ex,ey;
    char Tu[301][301];
    int bj[301][301];
    int Next[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};
    int init()
    {
    memset(Tu,'R',sizeof(Tu));
    memset(bj,'0',sizeof(bj));
    }
    struct Node
    {
    int x;
    int y;
    int step;
    friend bool operator < (Node a,Node b)
    {
    return a.step>b.step;
    }
    };
    void bfs(int s,int e,int step)
    {
    priority_queueq;
    Node now,next;
    now.x=s;
    now.y=e;
    now.step=step;
    q.push(now);
    while(!q.empty())
    {
    now=q.top();
    // cout<<"now.step="<<now.step<<" now.x="<<now.x<<" now.y"<<now.y<<endl;
    if(now.xex&&now.yey)
    {
    printf("%d ",now.step);
    return ;
    }
    q.pop();
    for(int i=0; i<4; i++)
    {
    next.x=now.x+Next[i][0];
    next.y=now.y+Next[i][1];
    if(bj[next.x][next.y]1||next.x<0||next.x>=m||next.y<0||next.y>=n||Tu[next.x][next.y]'R'||Tu[next.x][next.y]'S')
    continue;
    if(Tu[next.x][next.y]
    'T'||Tu[next.x][next.y]'E')
    {
    next.step=now.step+1;
    bj[next.x][next.y]=1;
    q.push(next);
    }
    if(Tu[next.x][next.y]
    'B')
    {
    next.step=now.step+2;
    bj[next.x][next.y]=1;
    q.push(next);
    }

    }
    }
    printf("-1 ");
    }
    int main()
    {
    while(~scanf("%d%d",&m,&n),n,m)
    {
    init();
    for(int i=0; i<m; i++)
    {
    getchar();
    for(int j=0; j<n; j++)
    {
    scanf("%c",&Tu[i][j]);
    if(Tu[i][j]'Y')
    {
    sx=i;
    sy=j;
    }
    if(Tu[i][j]
    'T')
    {
    ex=i;
    ey=j;
    }
    }
    }
    // printf("%d %d %d %d ",sx,sy,ex,ey);
    bfs(sx,sy,0);
    }
    return 0;
    }

  • 相关阅读:
    零零碎碎
    MFC入门--显示静态图片及调用本地软件
    Python版本OpenCV安装配置及简单实例
    用星星画菱形--Java
    pycharm IDE在导入自定义模块时提示有错,但实际没错
    Cmd使用方式--命令行运行程序
    cv2 & PIL(pillow)显示图像
    C++命令行多文件编译(g++)
    MNIST多图显示--Python练习
    visual studio 2017--括号自动补全
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6780069.html
Copyright © 2011-2022 走看看