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.
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.
'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
题意:
给定你的坐标点,出口的坐标和出口打开的时间点,问你最后能不能走出迷宫。
思路:
坑人的题,给定的时间是开门时间,要恰恰好好的赶到,而且还一直错在 数据的处理 scanf()函数上。
代码:
#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;
}

458

被折叠的 条评论
为什么被折叠?



