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;
    }

  • 相关阅读:
    [转]Android 应用性能调试
    [书目20120110]项目管理:计划、进度和控制的系统方法 哈罗德·科兹纳博士所著
    [转]Android数据存储SharedPreferences的使用
    [转]八款开源 Android 游戏引擎
    图书 beginningandroidgames 源码
    [转]Android中在SurfaceView上高效绘图
    [转] Himi 著作《Android游戏编程之从零开始》★书籍源码+第4/6/7样章—>免费下载★
    [转]AndroidAlarmManager(全局定时器/闹钟)指定时长或以周期形式执行某项操作
    android open source game frozenbubble
    [转]eclipse/myeclipse注释模板的修改
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6780069.html
Copyright © 2011-2022 走看看