标签:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int Max = 105; 6 int g[Max][Max]; 7 int x, y, k, n, m; 8 int fx[4] = {-1, 0, 1, 0}; 9 int fy[4] = {0, 1, 0, -1}; //上右下左 10 char path[4] = {‘L‘, ‘U‘, ‘R‘, ‘D‘}; 11 int choose(char dir, int gra) 12 { 13 if(dir == ‘L‘) 14 { 15 if(gra) 16 return 0; 17 return 2; 18 } 19 else if(dir == ‘R‘) 20 { 21 if(gra) 22 return 2; 23 return 0; 24 } 25 else if(dir == ‘U‘) 26 { 27 if(gra) 28 return 1; 29 return 3; 30 } 31 else if(dir == ‘D‘) 32 { 33 if(gra) 34 return 3; 35 return 1; 36 } 37 } 38 void dfs(int x, int y, char dir, int cnt) 39 { 40 if(cnt == k) 41 { 42 printf("%d %d\n", x, y); 43 return; 44 } 45 int gid = choose(dir, g[x][y]); // 判断当前朝向dir,格子g[x][y]时往哪走 46 int gx = x + fx[gid]; 47 int gy = y + fy[gid]; 48 int st; 49 for(st = 0; path[st] != dir; st++); //st找到当前dir的位置 50 if(g[x][y]) 51 { 52 //如果是1的话,就方向顺时针,别忘了更改格子状态 53 g[x][y] = !g[x][y]; 54 dfs(gx, gy, path[ (st + 1) % 4], cnt + 1); 55 } 56 else 57 { 58 // 如果是0的话,方向逆时针, 59 g[x][y] = !g[x][y]; 60 dfs(gx, gy, path[ (st - 1 + 4) % 4], cnt + 1); 61 } 62 63 } 64 65 int main(int argc, char** argv) 66 { 67 scanf("%d%d", &n, &m); 68 for(int i = 0; i < n; i++) 69 { 70 for(int j = 0; j < m; j++) 71 scanf("%d", &g[i][j]); 72 } 73 char s; 74 scanf("%d%d %s%d", &x, &y, &s, &k); 75 int st; 76 for(int i = 0; i < 4; i++) 77 if(path[i] == ‘s‘) 78 st = i; 79 dfs(x, y, s, 0); 80 return 0; 81 }
标签:
原文地址:http://www.cnblogs.com/zhaopAC/p/5277064.html