題目635:Oh, my goddess

題目鏈接:

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

描述

Shining Knight is the embodiment of justice and he has a very sharp sword can even cleavewall. Many bad guys are dead on his sword.

One day, two evil sorcerer cgangee and Jackchess decided to give him some colorto see. So they kidnapped Shining Knight’s beloved girl–Miss Ice! They built a M x Nmaze with magic and shut her up in it.

Shining Knight arrives at the maze entrance immediately. He can reach any adjacent emptysquare of four directions – up, down, left, and right in 1 second. Or cleave one adjacent wall in 3

seconds, namely,turn it into empty square. It’s the time to save his goddess! Notice: ShiningKnight won’t leave the maze before he find Miss Ice.

輸入

The input consists of blocks of lines. There is a blank line between two blocks.

The first line of each block contains two positive integers M <= 50 and N <= 50separated by one space. In each of the next M lines there is a string of length N contentsO and #.

O represents empty squares. # means a wall.

At last, the location of Miss Ice, ( x, y ). 1 <= x <= M, 1 <= y <= N.

(Shining Knight always starts at coordinate ( 1, 1 ). Both Shining and Ice’s locationguarantee not to be a wall.)

輸出

The least amount of time Shining Knight takes to save hisgoddess in one line.

樣例輸入

3 5
O####
#####
#O#O#
3 4

樣例輸出

14

算法思想:

這是一道典型的廣度優先遍歷,但是運用的隊列需要使用優先隊列,因爲每次選取的應該是隊列中需要步數最少的點來進行擴展。

當然,也可以使用深度搜索加動態規劃來解決該題,但是時間複雜度過大,會超時。

廣度搜索源代碼

/*
Authot:YangLinfeng
NYOJ(635):Oh, my goddess
Date:2017.10.13
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
int visited[55][55], dir[4][2] = { { 1, 0 }, { 0, -1 }, { -1, 0 }, { 0, 1 } };
int m, n, sa, sb, ans;
char map[55][55];
#define MAXNUM 10005
struct Node{
    int x, y, step;
    friend bool operator<(Node node1, Node node2)
    {
        return node1.step > node2.step;
    }
};
priority_queue<Node> Q;
/*
廣度搜索,使用優先隊列來進行
*/
int BFS(int x, int y, int s)
{
    int r, c, steps;
    while (!Q.empty()) Q.pop();
    Node node, node1;
    node = { x, y, s };
    Q.push(node);
    while (!Q.empty())
    {
        node1 = Q.top();
        if (node1.x == sa && node1.y == sb)
        {
            return node1.step;
        }
        Q.pop();
        visited[node1.x][node1.y] = 1;
        for (int i = 0; i < 4; i++)
        {
            r = node1.x + dir[i][0], c = node1.y + dir[i][1];
            if (map[r][c] && !visited[r][c])
            {
                if (map[r][c] == 'O')
                    steps = node1.step + 1;
                if (map[r][c] == '#')
                    steps = node1.step + 4;
                node = { r, c, steps };
                Q.push(node);
                visited[r][c] = 1;
            }           
        }
    }
    return 0;
}
int main()
{
    while (cin >> m >> n)
    {
        ans = MAXNUM;
        memset(map, 0, sizeof(map));
        memset(visited,0,sizeof(visited));
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                cin >> map[i][j];
            }
        }
        cin >> sa >> sb;
        cout << BFS(1, 1, 0) << endl;
    }
    return 0;
}

深度搜索加動態規劃源代碼(TLE)

/*
Authot:YangLinfeng
NYOJ(635):Oh, my goddess
Date:2017.10.13
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
char map[55][55];
int dp[55][55], dir[4][2] = { { 1, 0 }, { 0, -1 }, { -1, 0 }, { 0, 1 } };
int sa, sb, ans;
#define MAXNUM 10005
int DFS(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])
        {
            if (map[r][c] == 'O' && dp[r][c] > dp[x][y] + 1)
            {
                dp[r][c] = dp[x][y] + 1;
                DFS(r, c);
            }
            if (map[r][c] == '#' && dp[r][c] > dp[x][y] + 4)
            {
                dp[r][c] = dp[x][y] + 4;
                DFS(r, c);
            }
        }
    }
    return dp[sa][sb];
}
int main()
{
    int m, n;
    while (cin >> m >> n)
    {
        ans = MAXNUM;
        for (int i = 0; i < 55; i++)
        {
            for (int j = 0; j < 55; j++)
            {
                dp[i][j] = MAXNUM;
            }
        }
        dp[1][1] = 0;
        memset(map, 0, sizeof(map));
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                cin >> map[i][j];
            }
        }
        cin >> sa >> sb;
        cout << DFS(1, 1) << endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章