zoukankan      html  css  js  c++  java
  • nyoj--635--Oh, my goddess(dfs)

    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


    很简单的一个bfs,但是写的真揪心

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<queue>
    #include<algorithm>
    using namespace std;
    int n,m;
    int ex,ey;
    char map[55][55];
    int vis[55][55],hp[55][55];
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    struct node
    {
    	int x;
    	int y;
    	int step;
    	friend bool operator<(node n1,node n2)
        {    
            return n2.step<n1.step;    
        } 
    }p,temp;
    int check(int xx,int yy)
    {
    	if(xx<=0||yy<0||xx>n||yy>=m)
    	return 1;
    	if(vis[xx][yy])
    	return 1;
    	return 0;
    }
    int bfs()
    {
    	int i;
    	priority_queue<node>q; 
    	p.x=1;
    	p.y=0;
    	p.step=0;
    	vis[p.x][p.y]=1;
    	q.push(p);
    	while(!q.empty())
    	{
    		p=q.top();
    		q.pop();
    		if(p.x==ex&&p.y==ey-1)
    		{
    			return p.step;	
    		}
    		for(i=0;i<4;i++)
    		{
    			temp.x=p.x+dx[i];
    			temp.y=p.y+dy[i];
    			if(check(temp.x,temp.y)==0)
    			{
    				temp.step=p.step+hp[temp.x][temp.y]+1;
    				vis[temp.x][temp.y]=1;
    				q.push(temp);
    		    }
    		}
    	}
    	return 0;
    }
    int main()
    {
    	int i,j;
    	int sum;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		memset(hp,0,sizeof(hp));
    		memset(vis,0,sizeof(vis));
    		for(i=1;i<=n;i++)
    		{
    			scanf("%s",map[i]);
    			for(j=0;j<m;j++)
    			{
    				if(map[i][j]=='O')
    				hp[i][j]=0;
    				else if(map[i][j]=='#')
    				hp[i][j]=3;
    			}
    		}
    		scanf("%d%d",&ex,&ey);
    		sum=bfs();
    		printf("%d
    ",sum);
    	}
    	return 0;
    }                                



  • 相关阅读:
    Wireshark抓包分析TCP 3次握手、4次挥手过程
    Wireshark基本介绍和学习TCP三次握手
    关于TCP窗口大小
    stat
    Disk
    内存对齐
    Openssl asn1parse命令
    checkinstall
    Nginx
    Linux top
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273470.html
Copyright © 2011-2022 走看看