oh,my goddess-OJ

来源:互联网 发布:Affinity photo mac 编辑:程序博客网 时间:2024/06/11 16:54

Oh, my goddess

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述

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 5O##########O#O#3 4
样例输出

14

个人理解:

1.首次接触此类关于优先队列的问题,所用的bfs(),真的是不怎么熟悉,也不是太明白怎么回事,首先的感觉就是,代码明显变长了很多!

2.做这题时,时间有限,表示真的没怎么太明白题目中的所有代码内容,但我相信后续的题目中还会有涉及这类的题目,做多了,理解也会更深刻。

结果时间内存语言Accepted28324c++代码:

int judge()
{
    if (temp.x<1||temp.x>n)
        return 0;
    if (temp.y<1||temp.y>m)
        return 0;
    if(vis[temp.x][temp.y]==1)
        return 0;
    if(temp.step>=ans)
     return 0;
     return 1;
}
void bfs()
{
    a.x=x;
    a.y=y
    a.step=0;
    priority_queue<node>q;
    q.push(a);
    memset(vis,0,sizeof(vis));
    vis [x][y]=1;
    while (!q.empty())
    {
        a=q.top();
        q.pop();
        for (int i=0;i<4;i++)
        {
            temp.x=a.x+dx[i]
            temp.y=a.y+dy[i]
            if (mp[temp.x][temp.y]=='#')
                temp.step=a.step+4;
            else
                temp.step=a.step+1;
            if (judge())
            {
                if (temp.x==e&&temp.y==ey)
                {
                    ans=temp.step;
                    return;
                }
                q.push(temp);
                V[temp.x][temp.y]=1;
            }
        }
    }
}


int main()
{
    while (~scanf("%d%d",&n,&m));
    {
        ans =INF
        for (int i=1;i<=n;i++)
        {
            scanf ("%s",M[i]+1);
            continue;
        }
        bfs()
        printf("%d\n",ans); 
}
return 0;
}

原创粉丝点击