POJ 2312 Battle City 優先隊列+bfs

Battle City

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8597 Accepted: 2882
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. 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?
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

題目鏈接:http://poj.org/problem?id=2312

題意

計算從'Y'走到'T'的最短時間,其中'S'和'R'不能走,'E'花費時間爲1,'B'花費時間爲2。

題解

優先隊列bfs,在遍歷的過程中取出當前時間最少的節點進行展開即可。

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
#define INF 0xffffffff
using namespace std;

const int maxn = 305;
int ax, bx, ay, by;
int m, n;
char pic[maxn][maxn];
int vis[maxn][maxn];
int dr[4] = {-1,0,1,0};
int dc[4] = {0,1,0,-1};

struct Node{
    int r, c, t;
    Node(){}
    Node(int r, int c, int t):r(r), c(c), t(t){}
    bool operator < (const Node & rhs) const{   //定義小於,傳進來的比當前小爲真 
        return t > rhs.t;
    }
};

int go(int r, int c){
    if(r<0 || r>=m || c<0 || c>=n || pic[r][c] == 'S' || pic[r][c] == 'R' || vis[r][c]) return 0;
    else if(pic[r][c] == 'T' || pic[r][c] == 'E') return 1;
    else return 2;
}

void solve(){
    priority_queue<Node> q;
    Node st(ax, ay, 0);
    q.push(st);
    vis[ax][ay] = 1;
    while(!q.empty()){
        Node u = q.top();
        q.pop();
        for(int i=0; i<4; i++){
            int nextr = u.r + dr[i];
            int nextc = u.c + dc[i];
            int t = go(nextr, nextc);
            if(t){
                vis[nextr][nextc] = 1;
                Node u1(nextr, nextc, u.t+t);
                q.push(u1);
                if(nextr == bx && nextc == by){
                    printf("%d\n", u1.t);
                    return;
                }
            }
        }
    }
    printf("-1\n");
}

int main(){
    while(scanf("%d%d", &m, &n) == 2 && m && n){
        for(int i=0; i<m; i++){
            scanf("%s", pic[i]);
            for(int j=0; j<n; j++){
                if(pic[i][j] == 'Y'){
                    ax = i, ay = j;
                }
                if(pic[i][j] == 'T'){
                    bx = i, by = j;
                }
            }
        }
        memset(vis, 0, sizeof(vis));
        if(ax == bx && ay == by){
            printf("0\n");
        } 
        else{
            solve();
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章