美文网首页
【优先队列+BFS】POJ_2312_Battle City

【优先队列+BFS】POJ_2312_Battle City

作者: 今天也继续开心涅普涅普 | 来源:发表于2016-10-22 18:00 被阅读0次

Battle City
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8431 Accepted: 2821

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.

Paste_Image.png

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).

Paste_Image.png

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
8

Source
POJ Monthly,鲁小石

题意:
坦克大战,给一个m*n的地图,其中Y为自己,T为目标,S、R为不可跨过的墙、河流,B为可以击破的墙,E为空。每次操作可以选择4个方向移动一个或开炮打破一面B墙,找出最少的操作数。

思路:
BFS遍历求解,对于B墙,其相当于走两步处理,使用优先队列保证找到的第一个解为最优。

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;

const int maxn = 300;

struct Node {
    int x, y;
    int count;
    Node(int xx, int yy, int c = 0) :x(xx), y(yy), count(c) {};
    bool operator<(const Node& right) const {
        return this->count > right.count;
    }
};

int m, n;
char buf[maxn + 5][maxn + 5];
int pass[maxn + 5][maxn + 5];
int nowx, nowy;
int tarx, tary;

int ud[] = { 0, 0, 1, -1 };
int lr[] = { 1, -1, 0, 0 };

bool check(int x, int y) {
    if (x >= 0 && x < m && y >= 0 && y < n && pass[x][y] == 0) {
        if (buf[x][y] != 'S' && buf[x][y] != 'R')
            return true;
    }
    return false;
}

int bfs() {
    priority_queue<Node> step;
    pass[nowx][nowy] = 1;
    step.push(Node(nowx, nowy));
    while (!step.empty()) {
        Node now = step.top();
        step.pop();
        if (now.x == tarx && now.y == tary)
            return now.count;
        else {
            for (int i = 0; i < 4; ++i) {
                int nx = now.x + ud[i];
                int ny = now.y + lr[i];
                if (nx >= 0 && nx < m && ny >= 0 && ny < n && pass[nx][ny] == 0) {
                    if (buf[nx][ny] == 'S' || buf[nx][ny] == 'R')
                        continue;
                    pass[nx][ny] = 1;
                    step.push(Node(nx, ny, (buf[nx][ny] == 'B' ? now.count + 2 : now.count + 1)));
                }
            }
        }
    }
    return -1;
}

int main() {
    while (scanf("%d%d", &m, &n) != EOF && m && n) {
        memset(pass, 0, sizeof(pass));
        for (int i = 0; i < m; ++i) {
            scanf("%s", buf + i);
            for (int j = 0; j < n; ++j) {
                if (buf[i][j] == 'Y') {
                    nowx = i;
                    nowy = j;
                }
                if (buf[i][j] == 'T') {
                    tarx = i;
                    tary = j;
                }
            }
        }
        printf("%d\n", bfs());
    }
    return 0;
}


相关文章

  • 【优先队列+BFS】POJ_2312_Battle City

    Battle CityTime Limit: 1000MS Memory Limit: 65536KTo...

  • 数据结构基础--栈和队列

    目录 基本性质 栈和队列的基本操作 双端队列和优先级队列 深度优先遍历(DFS)和广度优先遍历(BFS) 递归函数...

  • python-数据结构 队列和广度优先搜索(BFS)

    1.队列和广度优先搜索(BFS) 原题来自LeetCode 广度优先搜索(BFS)的一个常见应用是找出从根结点到目...

  • 广度优先搜索与队列

    在算法中,广度优先搜索算法(BFS)和队列(Queue)是一对好朋友,基本上用到BFS思路的地方都可以用队列来解决...

  • 2017-5-14 省赛模板

    简介 搜索迷宫(BFS+队列) 最短路Dijkstra+邻接矩阵Dijkstra+链式前向星+优先队列Bellma...

  • 朋友圈

    解法一:深度优先遍历DFS(递归) 解法二:广度优先遍历BFS(队列) 解法三:并查集 使用了一种合适的数据结构,...

  • 图论:DFS、BFS和优先级搜索框架实现最小支撑树、最短路

    1.BFS 优先访问最先发现的节点,可以根据FIFO队列的特点,选用队列实现可以实现连通域分解和最短路径等问题。等...

  • BFS与DFS

    1.什么是BFS,DFS BFS宽度优先搜索.一层一层搜索.把每行的结果存入到队列中,然后遍历求下一层.DFS深度...

  • 二叉树遍历/搜索

    遍历 1.宽度优先遍历BFS 利用队列 2.深度优先遍历DFS 1.递归:前序中序后序 2.非递归:前序中序后序 利用栈

  • 算法与数据结构 之 搜索算法

    搜索分为广度优先搜索、深度优先搜索、A*算法。 一、广度优先算法(BFS) 1.1、基本实现和特性:BFS是从一个...

网友评论

      本文标题:【优先队列+BFS】POJ_2312_Battle City

      本文链接:https://www.haomeiwen.com/subject/zhshuttx.html