題目284:坦克大戰

題目鏈接:

http://acm.nyist.net/JudgeOnline/problem.php?pid=284

描述

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

算法思想:

這道題正確的解法是使用動態規劃加深度搜索。使用map二維數組記錄輸入的地圖,M,N記錄輸入的行和列,dp[x][y]記錄從起始節點map[start.x][start.y]到達map[x][y]所需要的最少步數。在輸入地圖的時候將 ‘Y’和 ‘T’的位置記錄下來分別爲start,end,並將dp數組元素dp[start.x][start.y]設爲0(因爲是起始節點),dp數組其他元素設爲最大值。然後從起始節點start開始深度搜索其符合條件(符合條件:不越界、不是S和R,因爲S和R不能走)的四個方向的節點,並更新dp記錄表,這裏有一個剪枝的過程,就是當dp[r]c不需要更新時,說明這條路徑不需要繼續遍歷,此時不遞歸搜索。具體如圖所示:
這裏寫圖片描述
當前節點爲map[x][y],選擇其相鄰的四個方向的且符合條件的節點,判斷map[r][c] == ‘B’ && dp[r][c] > dp[x][y] + 2是否成立,如果成立,說明目前從map[x][y]到map[r][c]比其他路徑到map[r][c]更優,故更新dp[r][c],並遞歸;當map[r][c] == ‘E’時與之類似;當map[r][c] == ‘T’時,不需遞歸,因爲已經到達終點。

剛開始寫這道題時,寫了一個深度搜索的,明顯會超時,但還是忍不住寫了。

深搜源代碼

/*
Author:YangLinfeng
NYOJ(284):坦克大戰
*/
#include <iostream>
#include <queue>
#include <cstring>
char map[305][305];
#define MAXNUM 999999
int dp[305][305], M, N, cnt = MAXNUM;
int dir[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
using namespace std;
struct Node{
    int x, y, steps;
}start, pre, curr;
queue<Node> Q;
void dfs(int x, int y, int s,char ch)
{
    if (map[x][y] == 'R' || map[x][y] == 'S' || !map[x][y]) return;
    if (map[x][y] == 'T')
    {
        cnt = min(cnt, s);
        return;
    }
    s++;
    ch = map[x][y];
    if (ch == 'B')
        s++;
    map[x][y] = 'S';
    dfs(x + 1, y, s, ch);
    dfs(x - 1, y, s, ch);
    dfs(x, y + 1, s, ch);
    dfs(x, y - 1, s, ch);
    map[x][y] = ch;
}

int main()
{
    while (cin >> M >> N && (M + N))
    {
        memset(map, 0, sizeof(map));
        cnt = MAXNUM;
        for (int i = 1; i <= M; i++)
        {
            for (int j = 1; j <= N; j++)
            {
                cin >> map[i][j];
                if (map[i][j] == 'Y')
                {
                    start.x = i, start.y = j, start.steps = 0;
                }
            }
        }
        dfs(start.x,start.y,0,map[start.x][start.y]);
        if (cnt == MAXNUM)
            cout << "-1" << endl;
        else
            cout << cnt << endl;
    }
    return 0;
}

深搜加動規源代碼

/*
Author:YangLinfeng
NYOJ(284):坦克大戰
*/
#include <iostream>
#include <queue>
#include <cstring>
char map[305][305];
int dp[305][305], M, N, cnt;
int dir[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
using namespace std;
struct Node{
    int x, y;
}start, endnode, pre, curr;
queue<Node> Q;
#define MAXNUM 99999999
void dpf(int x,int y)
{
    int r, c;       
    for (int i = 0; i < 4; i++)
    {
        r = x + dir[i][0], c = y + dir[i][1];
        if (map[r][c] != 'S' && map[r][c] != 'R' && map[r][c])
        {
            if (map[r][c] == 'E' && dp[r][c] > dp[x][y] + 1)
            {
                dp[r][c] = dp[x][y] + 1;
                dpf(r, c);
            }
            else if (map[r][c] == 'B'&& dp[r][c] > dp[x][y] + 2)
            {
                dp[r][c] = dp[x][y] + 2;
                dpf(r, c);
            }
            else if (map[r][c] == 'T' && dp[r][c] > dp[x][y] + 1)
            {
                dp[r][c] = dp[x][y] + 1;
            }
        }
    }
    return;
}
int main()
{
    while (cin >> M >> N && (M + N))
    {
        memset(map, 0, sizeof(map));
        for (int i = 0; i < 305; i++)
        {
            for (int j = 0; j < 305; j++)
            {
                dp[i][j] = MAXNUM;
            }
        }
        for (int i = 1; i <= M; i++)
        {
            for (int j = 1; j <= N; j++)
            {
                cin >> map[i][j];
                if (map[i][j] == 'Y')
                {
                    start.x = i, start.y = j;
                    dp[i][j] = 0;
                }
                if (map[i][j] == 'T')
                {
                    endnode.x = i, endnode.y = j;
                }
            }
        }
        dpf(start.x, start.y);
        int ans = dp[endnode.x][endnode.y];
        if (ans == MAXNUM)
            cout << "-1" << endl;
        else
            cout << ans << endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章