Rocky(模拟)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2718
题意:如果没有障碍就按原方向直走,否则就右转直走,若右边走不通就左转直走,若左边也走不通就后转直走,直到走出去。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <string.h> 3 const int N=50; 4 using namespace std; 5 int a[N][N]; 6 int main() 7 { 8 int n,m,k,x,y,p=0; 9 while(~scanf("%d %d %d",&n,&m,&k)) 10 { 11 p++; 12 if (n==0&&m==0&&k==0) 13 break; 14 memset(a,0,sizeof(a)); 15 for (int j = 0; j <= m+1; j++) 16 { 17 a[0][j] = 1; 18 a[n+1][j] = 1; 19 20 } 21 for (int i = 0; i <= n+1; i++) 22 { 23 a[i][0] = 1; 24 a[i][m+1] = 1; 25 } 26 for (int i = 0; i < k; i ++) 27 { 28 scanf("%d %d",&x,&y); 29 a[x][y] = -1; 30 } 31 int dir; 32 scanf("%d %d",&x,&y); 33 if (x==1) 34 dir = 0; 35 else if (x==n) 36 dir = 2; 37 else if (y==m) 38 dir = 1; 39 else if (y==1) 40 dir = 3; 41 int cnt = 0; 42 while(a[x][y]!= 1) 43 { 44 cnt++; 45 if (dir==0) 46 { 47 if (a[x+1][y]!=-1) 48 { 49 x++; 50 dir = 0; 51 } 52 else 53 { 54 if (a[x][y-1]!=-1) 55 { 56 y--; 57 dir = 1; 58 } 59 else 60 { 61 if (a[x][y+1]!=-1) 62 { 63 y++; 64 dir = 3; 65 } 66 else 67 { 68 69 x--; 70 dir = 2; 71 } 72 } 73 } 74 } 75 else if (dir==1) 76 { 77 if (a[x][y-1]!=-1) 78 { 79 y--; 80 dir = 1; 81 } 82 else 83 { 84 if (a[x-1][y]!=-1) 85 { 86 x--; 87 dir = 2; 88 } 89 else 90 { 91 if (a[x+1][y]!=-1) 92 { 93 x++; 94 dir = 0; 95 } 96 else 97 { 98 y++; 99 dir = 3; 100 } 101 } 102 } 103 104 } 105 else if (dir==2) 106 { 107 if (a[x-1][y]!=-1) 108 { 109 x--; 110 dir = 2; 111 } 112 else 113 { 114 if (a[x][y+1]!=-1) 115 { 116 y++; 117 dir = 3; 118 } 119 else 120 { 121 if (a[x][y-1]!=-1) 122 { 123 y--; 124 dir = 1; 125 } 126 else 127 { 128 x++; 129 dir = 0; 130 } 131 } 132 } 133 } 134 else 135 { 136 if (a[x][y+1]!=-1) 137 { 138 y++; 139 dir = 3; 140 } 141 else 142 { 143 if (a[x+1][y]!=-1) 144 { 145 x++; 146 dir = 0; 147 } 148 else 149 { 150 if (a[x-1][y]!=-1) 151 { 152 x--; 153 dir = 2; 154 } 155 else 156 { 157 y--; 158 dir = 1; 159 } 160 } 161 } 162 } 163 } 164 if (dir==0) 165 x--; 166 else if (dir==1) 167 y++; 168 else if (dir==2) 169 x++; 170 else 171 y--; 172 printf("Case %d: %d %d %d\n",p,x,y,cnt); 173 } 174 return 0; 175 } 176