Rocky(dfs)
题目描述
Sylvester Stallion is an old horse who likes nothing better than to wander around in the fields around his stable. Sylvester is a little single minded and will walk in a straight line unless there is a rock in this path. If that\'s the case, he does one of three things: 1) if there is no rock to his right, he turns right and continues walking straight in that direction; 2) otherwise, if there is no rock to his left, he turns left and walks in that direction; 3) otherwise, he turns around and walks back the way he came. In a particularly rocky field, he may make several turns in his walk and exit the field in quite an unexpected location. For example, in the field shown below, if Sylvester enters the field at square (1,4), he will follow the path shown (a total of 12 squares), exiting at square (3,5).
Many of his other animal friends are concerned about Sylvester, and would like to know where he ends up on his walks (in particular, his good friend, the ram Beau). That\'s where you come in - given a description of a field and the location of Sylvester\'s entrance, you are to determine where he will exit the field and how long it will take him to get there.
Many of his other animal friends are concerned about Sylvester, and would like to know where he ends up on his walks (in particular, his good friend, the ram Beau). That\'s where you come in - given a description of a field and the location of Sylvester\'s entrance, you are to determine where he will exit the field and how long it will take him to get there.
输入
Each case starts with three positive integers n m rindicating the number of columns (n) and rows (m) in the field and the number of rocks (r), with n, m ≤ 20. Following the first line will be lines containing the locations of the r rocks. Each location will be of the form c r, indicating the column and row of the rock. There will be no more than 1 rock in any location. Following the rock locations will be the entrance location for Sylvester. Sylvester\'s starting direction will always be perpendicular to the side of the field he enters from (this location will never be a corner square) and there will never be a rock in his entrance location square. A line containing three zeros will terminate input.
输出
For each test case, output the case number followed by the last square Sylvester hits before he leaves the field (Sylvester will never get trapped in any field) and the number of squares that Sylvester visited during his walks, counting repeated squares.
示例输入
6 5 7 2 2 2 5 3 1 4 4 5 1 5 3 6 2 1 4 5 5 0 1 2 0 0 0
示例输出
Case 1: 3 5 12 Case 2: 5 2 5
题意:输入n,m,k;代表一个m*n的矩阵,其中有k个格子中有岩石,最后输入一个入口坐标,问从入口进入要走多少步可以出来,并输出走出来时的坐标。
前进的规则是:若前方有路,就直走,否则,向右转继续直走,若右方也没路就左转继续直走,若右方也没路,则后转往回走。优先顺序:直走,右转,左转,后转。
思路:设置一个方向数组,先确定入口的行走方向,按优先顺序递归。注意最后判断它走出去的方法是如果按行走的方向到出口一直有路,说明该方向能走出去,递归结束。
因为我建图和上图的相反,横坐标较小的在上方,输出的时候整的特晕,应该是先输出列再输出行。。。
1 #include<stdio.h> 2 #include<string.h> 3 int map[30][30]; 4 int dir[4][2] = {{0,1},{-1,0},{0,-1},{1,0}};//0,1,2,3分别代表向右,向上,向左,向下走 5 int n,m,k; 6 int dfs(int r,int c,int d, int step) 7 { 8 //printf(",,%d %d %d %d\n",r,c,d,step); 9 int x,y,i; 10 //判断是否能走出去 11 if(d == 0) 12 { 13 for(i = c; i <= n; i++) 14 if(map[r][i] == 1) 15 break; 16 if(i >= n+1) 17 { 18 printf("%d %d ",n,m+1-r); 19 return step + (n-c); 20 } 21 } 22 else if(d == 1) 23 { 24 for(i = r; i >= 1; i--) 25 if(map[i][c] == 1) 26 break; 27 if(i <= 0) 28 { 29 printf("%d %d ",c,m); 30 return step + r-1; 31 } 32 } 33 else if(d == 2) 34 { 35 for(i = c; i >= 1; i--) 36 if(map[r][i] == 1) 37 break; 38 if(i <= 0) 39 { 40 printf("%d %d ",1,m+1-r); 41 return step + c-1; 42 } 43 } 44 else 45 { 46 for( i = r; i <= m; i++) 47 if(map[i][c] == 1) 48 break; 49 if(i >= m+1) 50 { 51 printf("%d %d ",c,1); 52 return step+(m-r); 53 } 54 } 55 56 x = r+dir[d][0]; 57 y = c+dir[d][1]; 58 if(map[x][y] != 1)//直走 59 { 60 dfs(x,y,d,++step); 61 } 62 else 63 { 64 d = (d+3)%4; 65 if(map[ r+dir[d][0] ][ c+dir[d][1] ] != 1) 66 dfs(r+dir[d][0],c+dir[d][1],d,++step);//右转有路,直走 67 else 68 { 69 d = (d+1)%4;//右边没路,变回原来的方向 70 d = (d+1)%4;//左转; 71 if(map[ r+dir[d][0] ][ c+dir[d][1] ] != 1) 72 dfs(r+dir[d][0],c+dir[d][1],d,++step);//左转有路,直走 73 else 74 { 75 d = (d+1)%4;//左转没路,后转 76 dfs(r+dir[d][0],c+dir[d][1],d,++step);//后转直走 77 } 78 } 79 } 80 } 81 82 int main() 83 { 84 int item = 1; 85 while(~scanf("%d %d %d",&n,&m,&k)) 86 { 87 if(n == 0 && m == 0 && k == 0) 88 break; 89 memset(map,0,sizeof(map)); 90 int a,b; 91 int r,c,d; 92 for(int i = 0; i < k; i++) 93 { 94 scanf("%d %d",&a,&b); 95 map[m+1-b][a] = 1; 96 } 97 scanf("%d %d",&c,&r); 98 /*for(int i = 1; i <= m; i++) 99 { 100 for(int j = 1; j <= n; j++) 101 printf("%d ",map[i][j]); 102 printf("\n"); 103 }*/ 104 //确定入口直走的方向 105 if(r == m) 106 d = 3; 107 else if(r == 1) 108 d = 1; 109 else if(c == n) 110 d = 2; 111 else d = 0; 112 int ans; 113 printf("Case %d: ",item++); 114 ans = dfs(m+1-r,c,d,1); 115 printf("%d\n",ans); 116 117 } 118 return 0; 119 }