HDU 3152 obstacle course (BFS + priority queue overload)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3152


Problem description

You are working on the team creation ing with programming for the Mars Rover. to conserve energy, the rover needs to find optimal paths resume ss the rugged terrain to get from its starting location to its final location. the following is the first approximation for the problem.


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

Question:
From the top left to the bottom right, find the path with the smallest sum of digits in a passing grid.

You can move up, down, left, or right.

PS:

Since I have done hdu2084 before, I feel that I am using DP, but I can go through the previous steps,

Therefore, only the memory-based search is available!

Priority queue optimization!


The Code is as follows:

# include # include # include # include # include # include using namespace STD; const int maxn = 177; struct node {int X, Y; int DIS; bool operator <(const node & S) const {return dis> S. dis ;}}; int N; int mm [maxn] [maxn]; int vis [maxn] [maxn]; int DX [4] = }; int dy [4] = {0,-1, 1}; int BFS (INT dis) {priority_queue q; node fro, pre; fro. X = 0, Fro. Y = 0; fro. Dis = DIS; q. Push (fro); While (! Q. empty () {fro = Q. top (); q. pop (); For (INT I = 0; I <4; I ++) {int xx = fro. X + dx [I]; int YY = fro. Y + dy [I]; If (XX> = 0 & XX = 0 & YY pre. dis) {// no access, or the current is not the shortest vis [XX] [YY] = pre. DIS; If (XX! = N-1 | YY! = N-1) Q. push (pre) ;}}} return vis [n-1] [n-1];} int main () {int CAS = 0; while (scanf ("% d ", & N) {memset (VIS,-1, sizeof (VIS); For (INT I = 0; I


HDU 3152 obstacle course (BFS + priority queue overload)

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.