zoukankan      html  css  js  c++  java
  • hdu 1242 Rescue

    Rescue

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 15501    Accepted Submission(s): 5632


    Problem Description
    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

    Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

    You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
     

    Input
    First line contains two integers stand for N and M.

    Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

    Process to the end of the file.
     

    Output
    For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
     

    Sample Input
    7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
     

    Sample Output
    13
     

    <span style="font-size:18px;">#include<stdio.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define M 1005
    char map[M][M];
    int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    int n,m,a,b;
    int k,flag;
    
    void dfs(int x,int y,int cur)
    {   
    	int i,j;
    	int dx,dy;
    	if(x==a && y==b)  // 跳出搜索
    	{
    		flag=1;
    		if(k>cur)  // 最小步数。刚開始没有考虑到
    			k=cur;
    		return ;
    	}
    
    	for(i=0;i<4;i++)
    	{
    		dx=x+dir[i][0];
    		dy=y+dir[i][1];
    		if(dx>=0&&dx<n&&dy>=0&&dy<m&&map[dx][dy]!='#')
    		{
    			if(map[dx][dy]=='.') 
    			{
    			 map[dx][dy]='#';
    			 dfs(dx,dy,cur+1);
    			 map[dx][dy]='.';
    			}
    			
    			if(map[dx][dy]=='x') 
    			{
    			  map[dx][dy]='#';
    			  dfs(dx,dy,cur+2);
    			  map[dx][dy]='x';
    			}
    		}
    	}
    }
    
    int main ()
    {
    	int i,j;
    	int x,y;
    	while(cin>>n>>m)
    	{
    		for(i=0;i<n;i++)
    		{
    			for(j=0;j<m;j++)
    			{
    				cin>>map[i][j];
    				if(map[i][j]=='a')
    				{
    					x=i;
    					y=j;
    					map[x][y]='#';
    				}
    				if(map[i][j]=='r')
    				{
    					a=i;
    					b=j;
    					map[i][j]='.';  // 一定要注意终点要变为'.'。
    				}
    			}
    		}
    		
    		k=M;  // k为全局变量
    		flag=0;
    
            dfs(x,y,0);
    		if(flag)
    			 printf("%d
    ",k);
    		else 
    			 printf("Poor ANGEL has to stay in the prison all his life.
    ");
    	}
    	return 0;
    }</span>


  • 相关阅读:
    三 、 Multivariance Linear Regssion练习(转载)
    二、Linear Regression 练习(转载)
    一 、机器学习基础知识(转载)
    R-CNN论文详解(转载)
    ROC,AUC,PR,AP介绍及python绘制
    SVM较全面介绍,干货!(转载)
    ubuntu14.04 + cuda8.0 + cudnnv5 + caffe + py-faster-rcnn配置
    Windows系统 环境变量
    网络加密相关
    急救知识收集
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/7009897.html
Copyright © 2011-2022 走看看