【优先队列+BFS】POJ_2312_Battle City

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.

【优先队列+BFS】POJ_2312_Battle City_第1张图片
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).

【优先队列+BFS】POJ_2312_Battle City_第2张图片
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
#include
#include
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 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)