Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=3152
Obstacle Coursedescription
You is working on the team assisting with programming for the Mars rover. To conserve-energy, the rover-needs to find optimal paths across the rugged terrain-to-get from it starting location to I TS final location. The following is the first approximation for the problem.
$N * n$ square matrices contain the expenses for traversing each individual cell. For each of them, your task was to find the Minimum-cost traversal from the top left cell $[0][0]$ to the bottom right cell $[n-1][n-1]$. Legal moves is up, down, left, and right; That's, either the row index changes by one or the column index changes by one and not both.
Input
Each problem are specified by a single integer between 2 and giving the number of rows and columns in the $N * n$ Squar E Matrix. The file is terminated by the case $N = 0$.
Following the specification of $N $ you'll find $N $ lines, each containing $N $ numbers. These numbers would be given as a single digits, zero through nine, separated by single blanks.
Output
Each problem set is numbered (beginning at one) and would generate a single line giving the problem set and the Expens E of the Minimum-cost path from the top left to the bottom right corner, exactly as shown in the sample output A single space after the ' problem ' and after the colon.
Sample Input
3
5 5 4
3 9 1
3 2 7
5
3 7 2) 0 1
2 8 0) 9 1
1 2 1) 8 1
9 8 9) 2 0
3 6 5) 1 5
7
9 0 5 1 1 5 3
4 1 2 1 6 5 3
0 7 6 1 6 8 5
1 1 7 8 3 2 3
9 4 0 7 6 4 1
5 8 3 2 4 8 3
7 4 8 4 8 3 4
0
Sample Output
Problem 1:20
Problem 2:19
Problem 3:36
BFS Search, note that the points passed can be repeated walk.
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <vector>7#include <queue>8#include <map>9 usingstd::cin;Ten usingstd::cout; One usingStd::endl; A usingStd::find; - usingStd::sort; - usingstd::p air; the usingstd::vector; - usingStd::multimap; - usingstd::p riority_queue; - #definePB (E) push_back (e) + #defineSZ (c) (int) (c). Size () - #defineMP (A, b) Make_pair (A, B) + #defineAll (c) (c). Begin (), (c). End () A #defineITER (c) Decltype ((c). Begin ()) at #defineCLS (arr,val) memset (arr,val,sizeof (arr)) - #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) - Const intN = About; -typedef unsignedLong Longull; in intH, G[n][n], vis[n][n]; - Const intDx[] = {0,0, -1,1}, dy[] = {-1,1,0,0 }; to structNode { + intx, y, S; -Node (inti =0,intj =0,intK =0): X (i), Y (j), S (k) {} theInlineBOOL operator< (ConstNode &a)Const { * returns >A.S; $ }Panax Notoginseng }; -InlinevoidRead () { the Rep (i, H) { + Rep (J, H) { Ascanf"%d", &g[i][j]); theVIS[I][J] =-1; + } - } $ } $ intBFs () { -Priority_queue<node>Q; -Q.push (Node (0,0, g[0][0])); the while(!Q.empty ()) { -Node T =q.top (); Q.pop ();Wuyi if(T.x = = H-1&& T.y = = H-1) Break; theRep (I,4) { - intx = T.x + dx[i], y = t.y +Dy[i]; Wu if(X <0|| X >= H | | Y <0|| Y >= H)Continue; - if(T.s + g[x][y] < Vis[x][y] | | vis[x][y] = =-1) { AboutQ.push (Node (x, y, T.s +g[x][y])); $Vis[x][y] = T.s +G[x][y]; - } - } - } A returnVis[h-1][h-1]; + } the intMain () { - #ifdef LOCAL $Freopen ("In.txt","R", stdin); theFreopen ("OUT.txt","w+", stdout); the #endif the intK =1; the while(~SCANF ("%d", &h), h) { - read (); inprintf"problem%d:%d\n", k++, BFS ()); the } the return 0; About}
View Code
HDU 3152 Obstacle Course