nyoj 284

坦克大战

时间限制:1000 ms  |  内存限制:65535 KB

难度:3

描述

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 <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
#define  max 301
char map[max][max];//地图大小
int n,m;
bool vist[max][max];
int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//四个方位 

struct point
{
    int x,y,step;
    friend bool operator <(const point &s1,const point &s2)
    {
        return s1.step>s2.step;
    }
};
point s,e;

int bfs()
{
    priority_queue < point > q;//优先队列
    memset(vist,0,sizeof(vist));
    point now,t;
    s.step=0;
    q.push(s);
    vist[s.x][s.y]=1;
    while (!q.empty())
    {
        now=q.top();
        q.pop();
        if (now.x==e.x&&now.y==e.y)//坦克就在目的地
        {
            return now.step;
        }
        for (int i=0;i<4;i++)
        {
            t.x=now.x+d[i][0];
            t.y=now.y+d[i][1];
            if (t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&!vist[t.x][t.y])
            {
                vist[t.x][t.y]=1;
                if (map[t.x][t.y]==‘B‘)
                {
                    t.step=now.step+2;
                    q.push(t);
                }
                else
                    if (map[t.x][t.y]==‘E‘||map[t.x][t.y]==‘T‘)
                    {
                        t.step=now.step+1;
                        q.push(t);
                    }
            }
        }
    }
    return -1;
}

int main()
{
    int i,j;
    while (cin>>n>>m,m+n)
    {
        for (i=0;i<n;i++)
        {
            for (j=0;j<m;j++)
            {
                cin>>map[i][j];
                if (map[i][j]==‘Y‘)
                    s.x=i,s.y=j; //坦克位置
                if (map[i][j]==‘T‘)
                    e.x=i,e.y=j;//目的地
            }
        }
        int sum=bfs();
     cout<<sum<<endl;
    }

    return 0;
}

  

时间: 2024-10-13 12:16:38

nyoj 284的相关文章

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 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 co

nyoj 284 坦克大战 (优先队列)

题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284 特殊数据: 5 5 BBEEY EEERB SSERB SSERB SSETB 7 非优先队列: 1 2 #include<iostream> 3 #include<cstdio> 4 #include<algorithm> 5 #include<cstring> 6 #include<string> 7 #include<cm

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 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 space

NYOJ 284 坦克大战 bfs + 优先队列

这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 1 #include <stdio.h> 2 #include <iostream> 3 #include <queue> 4 #include <string.h> 5 using namespace std; 6 typedef struct Node{ 7 int x, y; 8 int step; 9 friend bool operator

nyoj 284 坦克大战【bfs】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 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 co

NYOJ 搜索题目汇总 NYOJ 20、21、27、42、58、82、202、284、325、353、488、491、523、592、722

NYOJ 搜索题目汇总 NYOJ 20 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<vector> #include<algorithm> using namespace std; int pre[100005]; vector<int>v[100005];//存储每个结点相邻的边 void DFS(int

NYOJ 237 游戏高手的烦恼 &amp;&amp; POJ3041-Asteroids ( 二分图的最大匹配 )

链接: NYOJ 237  游戏高手的烦恼:click here~~ POJ  3041 Asteroids           :click here~~ 题意: 两题一样,翻译不同而已. 有一位传说级游戏高手,在闲暇时间里玩起了一个小游戏,游戏中,一个n*n的方块形区域里有许多敌人,玩家可以使用炸弹炸掉某一行或者某一列的所有敌人.他是种玩什么游戏都想玩得很优秀的人,所以,他决定,使用尽可能少的炸弹炸掉所有的敌人. 现在给你一个游戏的状态,请你帮助他判断最少需要多少个炸弹才能炸掉所有的敌人吧.

NYOJ 49 开心的小明

开心的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N 元钱就行".今天一早小明就开始做预算,但是他想买的东西太多了,肯定会超过妈妈限定的N 元.于是,他把每件物品规定了一个重要度,分为5 等:用整数1~5 表示,第5 等最重要.他还从因特网上查到了每件物品的价格(都是整数元).

NYOJ 106 背包问题

背包问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w<=10):如果给你一个背包它能容纳的重量为m(10<=m<=20),你所要做的就是把物品装到背包里,使背包里的物品的价值总和最大. 输入 第一行输入一个正整数n(1<=n<=5),表示有n组测试数据: 随后有n测试数据,每组测试数据的第一行有两个正整数s,m(1<=s<=10