Hdu 1010 Tempter of the Bone(未完成)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 96499 Accepted Submission(s):
26147
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.
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.
Output
For each test case, print in one line "YES" if the
doggie can survive, or "NO" otherwise.
1 #include <cstdio> 2 int t,m,n,k; 3 int c[7][7]; 4 void move(int x[7][7],int i,int j,int s) 5 { 6 if(x[i][j]==2&&t==s) 7 { 8 k=1; 9 return; 10 } 11 if(s>t) return; 12 x[i][j]=1; 13 /* printf("n:%d\n",s); 14 for(int p=0; p<n; p++) 15 { 16 for(int q=0; q<m; q++) 17 { 18 printf("%d ",x[p][q]); 19 } 20 printf("\n"); 21 } 22 printf("\n"); 23 */ 24 if(i-1>=0&&(x[i-1][j]==0||x[i-1][j]==2)&&!k) 25 { 26 move(x,i-1,j,s+1); 27 x[i-1][j]=0; 28 } 29 if(j+1<m&&(x[i][j+1]==0||x[i][j+1]==2)&&!k) 30 { 31 move(x,i,j+1,s+1); 32 x[i][j+1]=0; 33 } 34 if(i+1<n&&(x[i+1][j]==0||x[i+1][j]==2)&&!k) 35 { 36 move(x,i+1,j,s+1); 37 x[i+1][j]=0; 38 } 39 if(j-1>=0&&(x[i][j-1]==0||x[i][j-1]==2)&&!k) 40 { 41 move(x,i,j-1,s+1); 42 x[i][j-1]=0; 43 } 44 } 45 int main() 46 { 47 while(scanf("%d%d%d",&n,&m,&t)==3&&n!=0) 48 { 49 k=0; 50 char ch[n][m]; 51 52 for(int i=0; i<n; i++) 53 scanf("%s",ch[i]); 54 for(int i=0; i<n; i++) 55 { 56 for(int j=0; j<m; j++) 57 { 58 if(ch[i][j]=='S'||ch[i][j]=='X') c[i][j]=1; 59 else if(ch[i][j]=='D') c[i][j]=2; 60 else c[i][j]=0; 61 } 62 } 63 move(c,0,0,0); 64 if(k==1) 65 printf("YES\n"); 66 else 67 printf("NO\n"); 68 } 69 }
题目超时,原因是遍历中还需剪枝,但是这是自己第一次尝试写回溯法,并且很好的完成了,至于效率的优化慢慢积累吧。
//////////////////////
加了这部分
int g,h;
g=ei>i?ei-i:i-ei;
h=ej>j?ej-j:j-ej;
if(g+h>t-s||(g+h)%2!=(t-s)%2)
{
return;
}
//////////////////////还是不行啊

浙公网安备 33010602011771号