HDU 1010 Tempter of the Bone(逃离地狱,DFS,剪枝,错题集)

Tempter of the Bone

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


D_Problem Description
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.
 

D_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.
 

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

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

D_Sample Output
  
  
NO YES
 

Author
ZHANG, Zheng
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1016  1241  1242  1072  1026 


题意:

给定你的坐标点,出口的坐标和出口打开的时间点,问你最后能不能走出迷宫。


思路:

坑人的题,给定的时间是开门时间,要恰恰好好的赶到,而且还一直错在 数据的处理 scanf()函数上。


参考:CSDN


代码:

#include <iostream>
#include <cstdio> 
#include <cstring>

using namespace std;

int xx[4]= {0,1,0,-1};
int yy[4]= {1,0,-1,0};
char map[10][10];
void init() {
	int i,j;
	for(i=0; i<10; i++) {
		for(j=0; j<10; j++)
			map[i][j]='X';
	}
}
int s1,s2,e1,e2,t,flag=0;
void DFS(int s1,int s2,int time) {
	if(flag==1)
		return ;
	if(s1==e1&&s2==e2) {
		if(time==t) {
			flag=1;
		}
		return ;
	}
	int nowx,nowy,i;
	for(i=0; i<4; i++) {
		nowx=s1+xx[i];
		nowy=s2+yy[i];
		if(map[nowx][nowy]!='X') {
			time++;

			map[nowx][nowy]='X';
			DFS(nowx,nowy,time);
			map[nowx][nowy]='.';
			time--;
		}
	}
}
int main() {
	int n,m;
	while(scanf("%d%d%d",&n,&m,&t)&&(n||m||t)) {
		init();
		int i,j,time=0,count=0;
		getchar();
		for(i=1; i<=n; i++) {
			for(j=1; j<=m; j++) {
				cin>>map[i][j];//使用scanf出错 
//				scanf("%c",&map[i][j]);
				if(map[i][j]=='D')
					e1=i,e2=j;
				else if(map[i][j]=='S')
					s1=i,s2=j;
				else if(map[i][j]=='.')
					count++;
			}
		}
		if(count+1<t) {
			puts("NO");
			continue;
		}

		flag=0;
		map[s1][s2]='X';
		DFS(s1,s2,time);
		if(flag)
			puts("YES");
		else
			puts("NO");

	}
	return 0;
}


代码(键入地图时出错):

#include<cstdio>
#include<cstring>
#define MYDD 16

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

int N,M,T;//矩阵的长宽,门打开的时间
char map[16][16];
int dog_x,dog_y;//小狗的初始坐标
int door_x,door_y;//出口的坐标
int dx[]= {0,0,-1,1};
int dy[]= {-1,1,0,0};//移动的位置坐标
int flag=0;//标识符,用于判断能不能离开迷宫

void init_map() {//地图的初始化,否则超时
	for(int j=0; j<16; j++) {
		for(int k=0; k<16; k++)
			map[j][k]='X';
	}
}

void DFS(int x,int y,int t) {
	if(flag)
		return ;
	if(x==door_x&&y==door_y) {
		if(t==T)
			flag=1;
		return ;
	}
	for(int j=0; j<4; j++) {
		int gx=x+dx[j];
		int gy=y+dy[j];
		if(map[gx][gy]!='X') {
			t++;
			map[gx][gy]='X';
			DFS(gx,gy,t);
			map[gx][gy]='.';
			t--;
		}
	}
}

int main() {
	while(scanf("%d%d%d",&N,&M,&T)&&(N||M||T)) {
		int num_block=0;// An empty block 的数目
		int need_t=0;//需要的时间
		init_map();

		for(int j=0; j<N; j++)
			scanf("%s",map[j]);//键入地图

		for(int j=0; j<N; j++) {
			for(int k=0; k<M; k++) {
				if(map[j][k]=='S') {
					dog_x=j;
					dog_y=k;//找到小狗坐标
				} else if(map[j][k]=='D') {
					door_x=j;
					door_y=k;//找到出口的坐标
				} else if(map[j][k]=='.')
					num_block++;
			}
		}

		if(num_block+1<T) {
			puts("NO");
			continue;
		}

		flag=0;
		map[dog_x][dog_y]='X';
		DFS(dog_x,dog_y,need_t);
		if(flag)
			puts("YES");
		else
			puts("NO");

	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值