题目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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章