NYOJ 635 Oh, my goddess

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

郑大第六届校赛

上传者

ACM_赵铭浩

题目描述:

骑士要去拯救的心爱的女孩(即骑士要以最快的时间到达女孩的位置),骑士最开始的位置总是(1,1),如果他将要走的位置是空的,那么他需要1秒时间到达该位置;否则,他需要3分钟的时间将它劈开使它变成空的,然后再花1秒的时间到达这一位置。

这道题还是提交的别人的代码,自己做的不知道为什么总是内存超限,大致思想还是优先队列,用宽度优先搜索。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
char mp[55][55];
int vis[55][55];
int n,m;
int x,y,ex,ey;
int ans;
int dx[4]= {0,1,-1,0};
int dy[4]= {1,0,0,-1};
struct node
{
    int x,y,step;
    bool friend operator < (node a,node b)
    {
        return a.step>b.step;//最小值优先
    }
} a,temp;
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==ex&&temp.y==ey)
                {
                    ans=temp.step;
                    return;
                }
                q.push(temp);
                vis[temp.x][temp.y]=1;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        ans=INF;                        //***
        for(int i=1; i<=n; i++)
        {
            scanf("%s",mp[i]+1);
        }
        x=1;
        y=1;
        scanf("%d%d",&ex,&ey);
        if(ex==x&&ey==y)
        {
            printf("0\n");
            continue;
        }
        bfs();
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值