hdu.1254.推箱子(bfs + 优先队列)
生活随笔
收集整理的這篇文章主要介紹了
hdu.1254.推箱子(bfs + 优先队列)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
推箱子
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6021????Accepted Submission(s): 1718
現在給定房間的結構,箱子的位置,搬運工的位置和箱子要被推去的位置,請你計算出搬運工至少要推動箱子多少格.
?
Input 輸入數據的第一行是一個整數T(1<=T<=20),代表測試數據的數量.然后是T組測試數據,每組測試數據的第一行是兩個正整數M,N(2<=M,N<=7),代表房間的大小,然后是一個M行N列的矩陣,代表房間的布局,其中0代表空的地板,1代表墻,2代表箱子的起始位置,3代表箱子要被推去的位置,4代表搬運工的起始位置.?
Output 對于每組測試數據,輸出搬運工最少需要推動箱子多少格才能幫箱子推到指定位置,如果不能推到指定位置則輸出-1.?
Sample Input 1 5 5 0 3 0 0 0 1 0 1 4 0 0 0 1 0 0 1 0 2 0 0 0 0 0 0 0?
Sample Output 4 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<queue> 5 int T ; 6 int n , m ; 7 const int M = 10 ; 8 int map[M][M] ; 9 bool vis[M][M][M][M] ; 10 int move[][2] = {{1,0} , {-1 , 0} , {0,1} , {0 , -1} } ; 11 struct node 12 { 13 int x , y ; 14 int a , b ; 15 int time ; 16 bool operator < (const node &rhs ) const 17 { 18 return time > rhs.time ; 19 } 20 }; 21 22 int bfs (int sx , int sy , int mx , int my , int ex , int ey) 23 { 24 //printf ("Last---> (%d,%d)\n" , ex , ey ) ; 25 node ans , tmp ; 26 std::priority_queue<node> q ; 27 memset (vis , 0 , sizeof(vis)) ; 28 while ( !q.empty ()) q.pop () ; 29 q.push ( (node) {sx , sy , mx , my , 0}) ; 30 vis[sx][sy][mx][my] = 1 ; 31 if (mx == ex && my == ey) return 0 ; 32 while ( !q.empty ()) { 33 ans = q.top () ; q.pop () ; 34 // printf ("S----(%d,%d) tui (%d,%d)\n" , ans.x , ans.y , ans.a , ans.b ) ; 35 for (int i = 0 ; i < 4 ; i ++) { 36 tmp = ans ; 37 tmp.x += move[i][0] ; tmp.y += move[i][1] ; 38 if (tmp.x < 0 || tmp.y < 0 || tmp.x == n || tmp.y == m) continue ; 39 if (map[tmp.x][tmp.y] == 1 ) continue ; 40 if (tmp.x == tmp.a && tmp.y == tmp.b ) { 41 int x = tmp.x + move[i][0] , y = tmp.y + move[i][1] ; 42 if (x < 0 || y < 0 || x == n || y == m) continue ; 43 if (map[x][y] == 1) continue ; 44 tmp.a = x , tmp.b = y ; 45 tmp.time ++ ; 46 } 47 if (vis[tmp.x][tmp.y][tmp.a][tmp.b]) continue ; 48 vis[tmp.x][tmp.y][tmp.a][tmp.b] = 1 ; 49 q.push (tmp ) ; 50 // printf ("(%d,%d) tui (%d,%d)\n" , tmp.x , tmp.y , tmp.a , tmp.b ) ; 51 if (tmp.a == ex && tmp.b == ey) return tmp.time ; 52 } 53 } 54 return - 1 ; 55 } 56 57 int main () 58 { 59 //freopen ("a.txt" , "r" , stdin ) ; 60 scanf ("%d" , &T ) ; 61 while (T --) { 62 scanf ("%d%d" , &n , &m ) ; 63 int k ; 64 int sx , sy , ex , ey , mx , my ; 65 for (int i = 0 ; i < n ; i ++) for (int j = 0 ; j < m ; j ++) scanf ("%d" , &map[i][j]) ; 66 for (int i = 0 ; i < n ; i ++) { 67 for (int j = 0 ; j < m ; j ++) { 68 if (map[i][j] == 4) sx = i , sy = j ; 69 else if (map[i][j] == 2) mx = i , my = j ; 70 else if (map[i][j] == 3) ex = i , ey = j ; 71 } 72 } 73 if ( (k = bfs (sx , sy , mx , my , ex , ey )) == -1) puts ("-1") ; 74 else printf ("%d\n" , k ) ; 75 } 76 return 0 ; 77 } 78 [ Copy to Clipboard ] [ Save to File] View Code
?
轉載于:https://www.cnblogs.com/get-an-AC-everyday/p/4494740.html
總結
以上是生活随笔為你收集整理的hdu.1254.推箱子(bfs + 优先队列)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VTP (vlan trunking p
- 下一篇: IDEA 导包快捷键