标签:
1 #include <iostream> 2 #include <queue> 3 #define N 50 4 const int west = 0, north = 1, east = 2, south = 3; 5 const int left = 0, forw = 1, right = 2, back = 3; 6 char m[N][N]; 7 int n, w, h, x, y; 8 int d[4][2][4] = { //d[i][j][k]代表从当前点朝k方向时往i方向排列应改变的值 9 { { 1, 0, -1, 0 },{ 0, -1, 0, 1 } }, 10 { {0, -1, 0, 1}, { -1, 0, 1, 0 } }, 11 { {-1, 0, 1, 0}, { 0, 1, 0, -1 } }, 12 { {0, 1, 0, -1}, { 1, 0, -1, 0 } } 13 }; 14 int dirc[4] = {3,0,1,2}; 15 void solve1(int sr,int sc,int sdir){ 16 int res=2,r=sr,c=sc,dir=sdir; 17 while (m[r][c] != ‘E‘){ 18 for (int i = 0; i < 4; i++){//当前位置方向下,依次朝左右前后四个方向尝试移动,即左优先 19 if (m[r + d[i][0][dir]][c + d[i][1][dir]] != ‘#‘){ 20 r += d[i][0][dir]; 21 c += d[i][1][dir]; 22 dir = (dir+dirc[i])%4; 23 res++; 24 break; 25 } 26 } 27 } 28 printf("%d ", res); 29 res = 2, r = sr, c = sc,dir=sdir; 30 while (m[r][c] != ‘E‘){//右优先,因为对方向的定义,故不能像上面那样直接枚举 31 int i; 32 for (i = 2; i >=0; i--){ 33 if (m[r + d[i][0][dir]][c + d[i][1][dir]] != ‘#‘){ 34 r += d[i][0][dir]; 35 c += d[i][1][dir]; 36 dir = (dir+dirc[i])%4; 37 res++; 38 break; 39 } 40 } 41 if (i<0) 42 if (m[r + d[back][0][dir]][c + d[back][1][dir]] != ‘#‘){ 43 r += d[back][0][dir]; 44 c += d[back][1][dir]; 45 dir = (dir+dirc[back])%4; 46 res++; 47 } 48 } 49 printf("%d ", res); 50 } 51 struct loca 52 { 53 int r, c, d,step;//定义节点为当前结点位置,方向及步数 54 }; 55 56 int bfs(int sr,int sc,int sdir){ 57 queue<loca> q; 58 loca p = { sr, sc, sdir,2},t; 59 q.push(p); 60 while (!q.empty()) 61 { 62 p = q.front(); q.pop(); 63 for (int i = 0; i < 3; i++){ 64 if (m[p.r + d[i][0][p.d]][p.c + d[i][1][p.d]] != ‘#‘) 65 { 66 if (m[p.r + d[i][0][p.d]][p.c + d[i][1][p.d]] == ‘E‘)return p.step+1; 67 m[p.r + d[i][0][p.d]][p.c + d[i][1][p.d]] = ‘#‘; 68 t.r = p.r + d[i][0][p.d]; 69 t.c = p.c + d[i][1][p.d]; 70 t.d = (p.d + dirc[i]) % 4; 71 t.step = p.step + 1; 72 q.push(t); 73 } 74 } 75 } 76 return 0; 77 } 78 void work(){ 79 int dir; 80 for (dir = 0; dir < 4; dir++) 81 if (m[y + d[forw][0][dir] ][x + d[forw][1][dir]] != ‘#‘){ 82 y += d[forw][0][dir]; 83 x += d[forw][1][dir]; 84 break; 85 } 86 solve1(y, x, dir); 87 printf("%d\n",bfs(y,x,dir)); 88 } 89 int main() 90 { 91 scanf("%d", &n); 92 while (n--){ 93 scanf("%d%d", &w, &h); 94 for (int i = 1; i <= h; i++){ 95 scanf("%s", m[i] + 1); 96 m[i][0] = m[i][w+1] = ‘#‘; 97 }//初始化将边界也变为#,减小编程难度 98 for (int i = 0; i <= w+1; i++) 99 m[0][i] = m[h+1][i] = ‘#‘; 100 for (int i = 1; i <= h; i++) 101 for (int j = 1; j <= w; j++){ 102 if (m[i][j] == ‘S‘){ 103 y = i; 104 x = j; 105 } 106 } 107 work(); 108 } 109 return 0; 110 } 111
这题被放在dfs里,所以说不能盲目相信分类,正所谓自古迷宫必bfs,迷宫问题的深度一般dfs是不行的,其实这题没什么,关键是方向变换的定义,只是我以为不用做访问标记而错了好久,于是及其无聊地在那二分查找介于re与mle之间的内存大小(你可以去死了),这行为和我早上为了吃到外卖而坚持不去一条街,饿了好几个小时一样,都是不冷静,太意气用事了,要好好反思啊,发题解其实是因为我好久没有这么晚睡,而且好久没写了,无聊就写写,自从开始做poj后,发现我做的题都已经被人把题解写了,而且都是些简单题目,似乎没有写题解的必要,比如这道,我基本没话可说,只能吹水,codeforce也是,但无所谓了,现在要好好练习为4月份的校赛作准备了,不能只顾着玩了。加油,q(≧▽≦q),话说,受到小优女神的影响,现在编程好喜欢用函数将功能模块化,也许她只是为了题解更容易看,但不得不说,这种习惯使我调试代码的能力好了很多。
Poj 3083 Children of the Candy Corn
标签:
原文地址:http://www.cnblogs.com/Dadio/p/5271162.html