标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5345 Accepted Submission(s): 1189
1 #include<stdio.h> 2 #include<queue> 3 #include<string.h> 4 #include<ctype.h> 5 #include<algorithm> 6 #include<math.h> 7 int T , maxn ; 8 int m , n , l , p ; 9 int val[15] ; 10 char map[55][55] ; 11 bool vis[55][55][4000] ; 12 int move[][2] = {{1,0} ,{-1,0} , {0,1} , {0,-1}} ; 13 struct node 14 { 15 int x , y , step ; 16 int jew , v; 17 }; 18 19 void bfs (int sx , int sy) 20 { 21 std::queue <node> q ; 22 while ( !q.empty ()) q.pop () ; 23 q.push ((node) {sx , sy , 0 , 0 , 0}) ; 24 node ans , tmp ; 25 vis[sx][sy][0] = 1 ; 26 while (!q.empty ()) { 27 ans = q.front () ; q.pop () ; 28 // printf ("S---(%d,%d) %d = %d\n" , ans.x , ans.y , ans.step , ans.v) ; 29 if (map[ans.x][ans.y] == ‘<‘) maxn = std::max (maxn , ans.v) ; 30 for (int i = 0 ; i < 4 ; i ++) { 31 tmp.x = ans.x + move[i][0] ; tmp.y = ans.y + move[i][1] ; 32 if (tmp.x < 0 || tmp.y < 0 || tmp.x >= n || tmp.y >= m) continue ; 33 if (map[tmp.x][tmp.y] == ‘*‘) continue ; 34 tmp.jew = ans.jew ; 35 tmp.step = ans.step +1 ; 36 if (tmp.step > l) continue ; 37 int k = -1 ; 38 tmp.jew = ans.jew ; tmp.v = ans.v ; 39 if ( isalpha (map[tmp.x][tmp.y]) ) { 40 k = map[tmp.x][tmp.y] - ‘A‘ ; 41 if (! (tmp.jew & (1 << k))) tmp.v += val[k] ; 42 tmp.jew = tmp.jew | (1 << k) ; 43 } 44 if ( vis[tmp.x][tmp.y][tmp.jew] ) continue ; 45 // printf ("(%d,%d) %d = %d\n" , tmp.x , tmp.y , tmp.step , tmp.v) ; 46 vis[tmp.x][tmp.y][tmp.jew] = 1 ; 47 q.push (tmp ) ; 48 } 49 } 50 } 51 52 int main () 53 { 54 // freopen ("a.txt" , "r" , stdin ) ; 55 int T , cas = 1 ; 56 scanf ("%d" , &T ) ; 57 while (T --) { 58 printf ("Case %d:\n" , cas ++) ; 59 int ex , ey , sx , sy ; 60 memset (vis , 0 , sizeof(vis)) ; 61 scanf ("%d%d%d%d" , &m , &n , &l , &p) ; 62 for (int i = 0 ; i < p ; i ++) scanf ("%d" , &val[i]) ; 63 for (int i = 0 ; i < n; i ++) scanf ("%s" , map[i]) ; //, puts (map[i]); 64 maxn = -1 ; 65 for (int i = 0 ; i < n; i ++) { 66 for (int j = 0 ; j < m ; j ++) { 67 if (map[i][j] == ‘@‘) 68 sx = i , sy = j ; 69 else if (map[i][j] == ‘<‘) 70 ex = i , ey = j ; 71 } 72 } 73 if (fabs (sx - ex ) + fabs (sy - ey) > l) { 74 printf ("Impossible\n") ; 75 if (T != 0) puts ("") ; 76 continue ; 77 } 78 bfs (sx , sy) ; 79 if (maxn != - 1) printf ("The best score is %d.\n" , maxn ) ; 80 else puts ("Impossible") ; 81 if (T != 0) puts ("") ; 82 } 83 return 0 ; 84 }
逗比的我把状压state | (1 << k) 一直写成 state | k ,然后....
有逗比的折腾出一种 + 宝藏 的方法,以后决定就先判定它有没有加入过,然后直接 + . (明明觉得很水的题orz)
hdu.1044.Collect More Jewels(bfs + 状态压缩)
标签:
原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4468634.html