zoukankan      html  css  js  c++  java
  • zoj2110

    Tempter of the Bone

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

    The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.


    Input

    The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

    'X': a block of wall, which the doggie cannot enter; 
    'S': the start point of the doggie; 
    'D': the Door; or
    '.': an empty block.

    The input is terminated with three 0's. This test case is not to be processed.


    Output

    For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.


    Sample Input

    4 4 5
    S.X.
    ..X.
    ..XD
    ....
    3 4 5
    S.X.
    ..X.
    ...D
    0 0 0


    Sample Output


    NO
    YES

    #include <cstdio>
    #include <cmath>	//用到了求绝对值的函数fabs
    
    char map[9][9];	//迷宫地图
    int n, m, t;	//迷宫的大小,及迷宫的门会在第t秒开启
    int di, dj;		//(di,dj):门的位置
    bool escape;	//是否成功逃脱的标志,escape为1表示能成功逃脱
    int dir[4][2] = { {0,-1}, {0,1}, {1,0}, {-1,0} }; //分别表示下、上、左、右四个方向
    
    //已经到达(si,sj)位置,且已经花费cnt秒
    void dfs( int si, int sj, int cnt )
    {
    	int i, temp;
    
    	if( si>n || sj>m || si<=0 || sj<=0 )  return;	//边界
    
    	if( si==di && sj==dj && cnt==t )	//成功逃脱
    	{
    		escape = 1;  return;
    	}
    	
    	//abs(x-ex) + abs(y - ey)表示现在所在的格子到目标格子的距离(不能走对角线)
    	//t-cnt是实际还需要的步数,将他们做差
    	//如果temp < 0或者temp为奇数,那就不可能到达!
    	temp = (t-cnt) - fabs(si-di) - fabs(sj-dj);
    	if( temp<0 || temp%2 )  return;	//搜索过程当中的剪枝
    
    	for( i=0; i<4; i++ )
    	{
    		if( map[ si+dir[i][0] ][ sj+dir[i][1] ] != 'X')
    		{
    			//前进方向!将当前方格设置为墙壁‘X’
    			map[ si+dir[i][0] ][ sj+dir[i][1] ] = 'X';
    			dfs(si+dir[i][0], sj+dir[i][1], cnt+1);	//从下一个位置继续搜索
    			if(escape)  return;
    			map[ si+dir[i][0] ][ sj+dir[i][1] ] = '.'; //后退方向!恢复现场!
    		}
    	}
    	return;
    }
    
    int main( )
    {
    	int i, j;	//循环变量
    	int si, sj;	//小狗的起始位置
    	while( scanf("%d%d%d", &n, &m, &t) )
    	{
    		if( n==0 && m==0 && t==0 )  break;	//测试数据结束
    		int wall = 0;
    		char temp;
    		scanf( "%c", &temp );	//见下面的备注
    		for( i=1; i<=n; i++ )
    		{
    			for( j=1; j<=m; j++ )
    			{ 
    				scanf( "%c", &map[i][j] );
    				if( map[i][j]=='S' ){  si=i;  sj=j;  } 
    				else if( map[i][j]=='D' ){  di=i;  dj=j;  }
    				else if( map[i][j]=='X' )  wall++;
    			}
    			scanf( "%c", &temp );
    		}
    		if( n*m-wall <= t )	//搜索前的剪枝
    		{
    			printf( "NO
    " );  continue;
    		}
    		escape = 0;
    		map[si][sj] = 'X';
    		dfs( si, sj, 0 );
    		if( escape )  printf( "YES
    " );	//成功逃脱
    		else  printf( "NO
    " );
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    [Andriod]计时器实现-基于线程消息机制三种方式
    [Android]语音识别中出现的问题和经验
    [工具]SublimeText编辑器一些设置-C++编译运行
    [工具]SublimeText编辑器一些设置-JAVA编译运行
    [HTML5]网页代码编辑器
    [Jetty]基于Java Servlet的支持WebSocket的服务器
    [网络包抓取分析软件]Wireshark
    [网络包抓取分析软件]Fiddler
    [JAVA]在Eclipse中使用JUnit4进行单元测试-1
    [JAVA]在Eclipse中使用JUnit4进行单元测试-2
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3219510.html
Copyright © 2011-2022 走看看