题目635:Oh, my goddess

来源:互联网 发布:微信报名软件 编辑:程序博客网 时间:2024/05/17 02:34

题目链接:

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:YangLinfengNYOJ(635):Oh, my goddessDate: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 10005struct 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:YangLinfengNYOJ(635):Oh, my goddessDate: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 10005int 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;}
原创粉丝点击