zoukankan      html  css  js  c++  java
  • ZOJ 2110 Tempter of the Bone


    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



    普通的搜索题,写完以后不幸TLE

    之后看题解,发现这道题作为一道教程(完全没看)题,专门讲剪枝…… WTF!

    于是加了个剪枝过了。300+ms,实际上可以各种花式剪枝把时间压到80ms,但是看写博客的时间就知道我为什么不想多写


    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    char map[20][20];
    int mx[5]={0,-1,0,1,0},
    	my[5]={0,0,1,0,-1};
    int n,m,Time;
    int tx,ty;
    int flag=0;
    void dfs(int x,int y,int t){
    	if(x==tx && y==ty && t==Time){
    		flag=1;
    		return;
    	}
    	if(map[x][y]=='X')return;
    //
    	if(t>=Time)return;//剪枝 
    	int temp=(Time-t)-fabs(x-tx)-fabs(y-ty);
    	if(temp<0 ||temp %2)return;
    	
    //
    	int i;
    	for(i=1;i<=4;i++){
    		int nx=x+mx[i];
    		int ny=y+my[i];
    		if(nx>=0 && nx<n && ny>=0 && ny<m)
    //			if(map[nx][ny]!='X')
    			{
    				map[x][y]='X';
    				dfs(nx,ny,t+1);
    				if(flag)return;
    				map[x][y]='.';
    			}
    	}
    	return;
    }
    int main(){
    	while(scanf("%d%d%d",&n,&m,&Time)!=EOF)
    	{
    		flag=0;
    		if(n==0 && m==0 && Time==0)break;
    		int i,j;
    		int xx,yy;
    		for(i=0;i<n;i++){
    			scanf("%s",map[i]);
    			for(j=0;j<m;j++){
    			  if(map[i][j]=='S')xx=i,yy=j;
    			  if(map[i][j]=='D')tx=i,ty=j;//目标点 
    			}
    		}
    		dfs(xx,yy,0);
    		if(flag==1)printf("YES
    ");
    		else printf("NO
    ");
    	}
    	return 0;
    }






  • 相关阅读:
    .net jquery ajax应用(后台)
    .net jquery ajax应用(前端)
    echarts 添加Loading 等待。
    js将数字转换为带有单位的中文表示
    关于Pre-bound JDBC Connection found! HibernateTransactionManager does not 异常小结
    java 并发容器一之ConcurrentHashMap(基于JDK1.8)
    java 并发容器一之BoundedConcurrentHashMap(基于JDK1.8)
    23中java设计模式(1)-- 策略模式
    解决Eclipse自动补全变量名的问题
    Tomcat+Jenkins+SonarQube+SVN+Maven 集成自动化环境搭建(Windows10环境下)
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5550583.html
Copyright © 2011-2022 走看看