NYOJ 284 坦克大战 && 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 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

广搜(BFS)+优先队列,从下午4:00多写到现在,用了三种方法,逐渐算是熟悉了这类题的写法,广搜写的有点感觉,继续保持!

【解题思路】广搜+优先队列,枚举所有方向,注意有个耗时的差异,因此很容易想到优先队列 (priority_queue ),也可以用深搜试一下

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;
const int MAX = 305;
char map[MAX][MAX];
int n,m,u,v,s,e;
struct Node
{
    int x,y,step;
    friend bool operator < (const Node a,const Node b)
    {
        return a.step > b.step;
    }
};
int dir[4][3]= {{1,0},{0,1},{-1,0},{0,-1}};
bool judg(Node temp)
{
    int x = temp.x;
    int y = temp.y;
    if(x<0||x>=n||y<0||y>=m||map[x][y]=='S'||map[x][y]=='R')
        return false;
    return true;
}
Node pre,last;
int BFS()
{
    priority_queue <Node>Q;
    Node temp;
    Q.push(pre);
    while(!Q.empty())
    {
        pre=Q.top();
        Q.pop();
        if((pre.x==last.x)&&(pre.y==last.y)) return pre.step;
        for(int i = 0; i < 4; i++)
        {
            temp.x = pre.x + dir[i][0];
            temp.y = pre.y + dir[i][1];
            temp. step = pre.step + 1;
            if(judg(temp))
            {
                if(map[temp.x][temp.y]=='B')
                    temp.step += 1;
                map[temp.x][temp.y] = 'S';
                Q.push(temp);
            }
        }
    }
    return -1;
}
void init()
{
    for(int i = 0; i < n; i++)
        scanf("%s",map[i]);
    for(int i = 0; i< n; i++)
        for(int j = 0; j < m; j++)
        {
            if(map[i][j] == 'Y')
            {
                pre.x = i;
                pre.y = j;
                pre.step=0;
            }
            if(map[i][j] == 'T')
            {
                last.x = i;
                last.y = j;
                last.step=0;
            }
        }
}
int main()
{
    while(scanf("%d%d",&n,&m),(n||m))
    {
        init();
        int dist = BFS();
        printf("%d\n",dist);
    }
    return 0;
}
时间: 2024-12-16 07:03:22

NYOJ 284 坦克大战 && POJ 2312 Battle City (广搜+优先队列)的相关文章

poj 2312 Battle City【bfs+优先队列】

Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7579   Accepted: 2544 Description 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 d

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

POJ 2312 Battle City(优先队列+BFS)

题目链接:http://poj.org/problem?id=2312 Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7085   Accepted: 2390 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often pl

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

poj 2312 Battle City

题目连接 http://poj.org/problem?id=1840 Battle City Description 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. Giv

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 坦克大战 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

POJ 题目2312 Battle City(BFS)

Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7208   Accepted: 2427 Description 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 d

POJ 3126 Prime Path( 广搜 )

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12974   Accepted: 7342 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-dig