zoukankan      html  css  js  c++  java
  • 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

    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    char f[52][52];
    bool vis[52][52];
    int Ex,Ey,N,M;
    const int dir[4][2]={-1,0,1,0,0,1,0,-1};
    struct Data
    {
    	int time;
    	int x,y;
    };
    struct cmp
    {
        bool operator() (Data a,Data b)
        {
            return a.time>b.time;
        }
    };
    priority_queue<Data,vector<Data>,cmp>Q;
    void BFS()
    {
        while(!Q.empty())Q.pop();
        memset(vis,0,sizeof(vis));
        Data t,elem;
        t.time=0;t.x=t.y=1;Q.push(t);
        int nx,ny;vis[1][1]=true;
        while(!Q.empty())
        {
            t=Q.top();Q.pop();
            if(t.x==Ex&&t.y==Ey){printf("%d
    ",t.time);break;}
            for(int i=0;i<4;i++)
            {
                nx=t.x+dir[i][0];
                ny=t.y+dir[i][1];
                if(nx>0&&nx<=N&&ny>0&&ny<=M&&!vis[nx][ny])
                {
                    elem.time=t.time+(f[nx][ny]=='#'?4:1);
                    elem.x=nx;elem.y=ny;
                    Q.push(elem);
                    vis[nx][ny]=true;
                }     
            } 
        }
    }
    int main()
    {   
        while(~scanf("%d%d",&N,&M))
        {
            for(int i=1;i<=N;i++)
                scanf("%s",f[i]+1);
            scanf("%d%d",&Ex,&Ey);
            BFS();
        }
    }                
    

      

  • 相关阅读:
    python 引用和对象理解
    ABP .Net Core 部署到IIS 问题汇总
    Ionic2 cordova angular2 打包到Android apk环境搭建
    学习ABP ASP.NET Core with Angular 环境问题
    [AngularJS 2 实践 一]My First Angular App
    即时通信系统Openfire分析之一:Openfire与XMPP协议
    S3C6410启动过程分析
    使用Word发表博客园博文
    github学习笔记
    Mac环境下 配置Python数据分析环境
  • 原文地址:https://www.cnblogs.com/zhangliu/p/7053070.html
Copyright © 2011-2022 走看看