HDU 3152 Obstacle Course (priority queue)

Source: Internet
Author: User

HDU 3152 Obstacle Course (priority queue)
Problem Description


You are working on the team creation ing with programming fZ? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys =" http://www.2cto.com/uploadfile/Collfiles/20150209/20150209090742323.jpg "alt =" \ ">
N* NSquare matrices contain the expenses for traversing each individual cell. for each of them, your task is to find the minimum-cost traversal from the top left cell [0] [0] to the bottom right cell [ N-1] [ N-1]. Legal moves are up, down, left, and right; that is, either the row index changes by one or the column index changes by one, but not both.

InputEach problem is specified by a single integer between 2 and 125 giving the number of rows and columns in N* NSquare matrix. The file is terminated by the case N= 0.

Following the specification NYou will find NLines, each containing NNumbers. These numbers will be given as single digits, zero through nine, separated by single blanks.

OutputEach problem set will be numbered (beginning at one) and will generate a single line giving the problem set and the expense of the minimum-cost path from the top left to the bottom right corner, exactly as shown in the sample output (with only a single space after "Problem" and after the colon ).

Sample Input
35 5 43 9 13 2 753 7 2 0 12 8 0 9 11 2 1 8 19 8 9 2 03 6 5 1 579 0 5 1 1 5 34 1 2 1 6 5 30 7 6 1 6 8 51 1 7 8 3 2 39 4 0 7 6 4 15 8 3 2 4 8 37 4 8 4 8 3 40

Sample Output
Problem 1: 20Problem 2: 19Problem 3: 36

Source2008 ACM-ICPC Pacific Northwest Region

Here is a map of N * N.

From the upper left corner to the lower right corner, the minimum cost of the trip. (Number is the cost)

Because not the shortest path, but the minimum cost.

Priority queue can be used, with a low priority. With BFS, a flag number is used to record the cost of any point.

The value of the flag array is constantly updated each time. Because of the priority queue, the highest priority (less cost) is always displayed)

Code on

# Include
 
  
# Include
  
   
# Include
   
    
# Include using namespace std; int n; int map [130] [130]; int flag [130] [130]; // record the cost of the specified point. Int dir [4] [2] = {0, 1}, {0,-1}, {}, {-}; # define inf 0x6fffffstruct node {int x, y; int cost; friend bool operator <(node a, node B) {return. cost> B. cost; // priority queue, which defines the priority and takes precedence if the cost is small .} }; Bool check (int x, int y) {if (x> = n | y> = n | x <0 | y <0) return 0; return 1;} int bfs (int x, int y) {int I; node st, ed; priority_queue
    
     
Q; // priority queue st. x = x; st. y = y; st. cost = map [0] [0]; memset (flag,-1, sizeof (flag); q. push (st); while (! Q. empty () {st = q. top (); // The minimum cost is incurred each time. Q. pop (); for (I = 0; I <4; I ++) {ed. x = st. x + dir [I] [0]; ed. y = st. y + dir [I] [1]; if (! Check (ed. x, ed. y) // It is enough to exclude cross-border access. If you do not want to exclude access, you can go back and traverse the entire map. Continue; ed. cost = st. cost + map [ed. x] [ed. y]; if (ed. cost
     
      


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.