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();
        }
    }                
    

      

  • 相关阅读:
    FreeMarker的<#if></#if>标签
    ubuntu的dpkg命令安装和卸载软件
    ubuntu建立软链接注意事项
    halo的工作目录,有一个是在代码里配置的,硬编码了
    Springboot的多环境配置
    idea中的springboot+gradle项目报错springboot configuration annotation processor not found in classpath
    maven中的pom.xml中的scope的作用
    设置idea的快捷键组合 设置为默认
    springboot无法查询到后台的数据
    ssh互信条件下的多机拷贝脚本和执行远程命令
  • 原文地址:https://www.cnblogs.com/zhangliu/p/7053070.html
Copyright © 2011-2022 走看看