Hdu1428 walking Campus (Shortest circuit + BFS (priority queue) + Memory search (DFS ))

Source: Internet
Author: User

Problem Description
LL is addicted to AC recently, and has 2.1 million lines of dormitory and data center every day. Due to a long time of sitting on the computer side, lack of exercise. He decided to take a walk on campus every time from the dormitory to the data center. The entire HDU campus is in a square layout and can be divided into n x n small squares, representing various areas. For example, LL's Dormitory No. 18 is located at the northwest corner of the campus, that is, the square () represents the place, and the third lab building of the IDC is located at the southeast end (n, n ). Because there are multiple routes to choose from, LL hopes that each walking route will be different. In addition, he considers that from Area A to Area B, there is only one route from B to the data center closer than any one from Area A to the data center (otherwise it may never reach the data center ...). Now he wants to know how many routes meet the requirements. Can you tell him?

 

Input
The first behavior of each group of test data is n (2 = <n <= 50), and the next n rows have n numbers in each row, represents the time t (0 <t <= 50) spent in each area (because the dormitory and data center are on the third floor, the start and end points are also time-consuming ).

 

Output
The total route count (less than 2 ^ 63) is output for each group of test data ).

 

Sample Input
3
1 2 3
1 2 3
1 2 3
3
1 1 1
1 1 1
1 1 1

Sample Output
1

# Include <stdio. h >#include <iostream >#include <queue> using namespace std; typedef struct nn {int dist, x, y; friend bool operator <(nn n1, nn n2) {return n1.dist> n2.dist;} node; int map [55] [55], N [55] [55], n; _ int64 dp [55] [55]; int dir [4] [2] = {,-, 0,-1}; void BFS () // calculates the minimum distance from each point to the end point, starting from the end {priority_queue <node> Q; node q, p; int I, tx, ty; q. x = q. y = n; q. dist = map [n] [n]; Q. push (q); N [n] [n] = map [n] [n]; While (! Q. empty () {q = Q. top (); Q. pop (); for (I = 0; I <4; I ++) {tx = q. x + dir [I] [1]; ty = q. y + dir [I] [0]; if (tx> 0 & tx <= n & ty> 0 & ty <= n) if (N [ty] [tx] =-1 | N [ty] [tx]> N [q. y] [q. x] + map [ty] [tx]) // run {p. x = tx; p. y = ty; p. dist = N [q. y] [q. x] + map [ty] [tx]; N [ty] [tx] = p. dist; Q. push (p) ;}}// printf ("% d", N [1] [1]);} _ int64 DFS (int x, int y) // memory-based search, number of walk-through methods per point to the end point that meet the condition {int e, tx, ty; if (dp [y] [x]> 0) // The current vertex has passed, and the current vertex is directly reversed Return dp [y] [x]; if (x = n & y = n) return 1; for (e = 0; e <4; e ++) // the range of the current vertex. Therefore, all the walking methods of all its ranges from point to end are added up {tx = x + dir [e] [1]; ty = y + dir [e] [0]; if (ty> 0 & ty <= n & tx> 0 & tx <= n) if (N [y] [x]> N [ty] [tx]) // The distance from the current point to the end is greater than the distance from the point to the end, number of steps {dp [y] [x] + = DFS (tx, ty);} return dp [y] [x]; // when the range of the current vertex is completed, return the range of the other vertex where it is located} int main () {int I, j; _ int64 k; while (scanf ("% d", & n)> 0) {for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) {scanf ("% d", & map [I] [J]); N [I] [j] =-1; dp [I] [j] = 0;} BFS ();/* for (I = 1; I <= n; I ++) {printf ("\ n"); for (j = 1; j <= n; j ++) printf ("% d ", N [I] [j]);} */k = DFS (); printf ("% I64d \ n", k) ;}# include <stdio. h >#include <iostream >#include <queue> using namespace std; typedef struct nn {int dist, x, y; friend bool operator <(nn n1, nn n2) {return n1.dist> n2.dist;} node; int map [55] [55], N [55] [55], n ;__ int64 dp [55] [55]; int dir [4] [2] = {1, 0,-1, 0, 0 ,-1}; void BFS () // calculates the minimum distance from each point to the end point, starting from the end point {priority_queue <node> Q; node q, p; int I, tx, ty; q. x = q. y = n; q. dist = map [n] [n]; Q. push (q); N [n] [n] = map [n] [n]; while (! Q. empty () {q = Q. top (); Q. pop (); for (I = 0; I <4; I ++) {tx = q. x + dir [I] [1]; ty = q. y + dir [I] [0]; if (tx> 0 & tx <= n & ty> 0 & ty <= n) if (N [ty] [tx] =-1 | N [ty] [tx]> N [q. y] [q. x] + map [ty] [tx]) // run {p. x = tx; p. y = ty; p. dist = N [q. y] [q. x] + map [ty] [tx]; N [ty] [tx] = p. dist; Q. push (p) ;}}// printf ("% d", N [1] [1]) ;:int64 DFS (int x, int y) // memory-based search, number of walk-through methods per point to the end point that meet the condition {int e, tx, ty; if (dp [y] [x]> 0) // The current vertex has passed, and returns the number of walk-through methods of the current vertex. return dp [y] [x]; if (x = n & y = n) return 1; for (e = 0; e <4; e ++) // the range of the current vertex, therefore, all the steps from the points in all its ranges to the end are added up {tx = x + dir [e] [1]; ty = y + dir [e] [0]; if (ty> 0 & ty <= n & tx> 0 & tx <= n) if (N [y] [x]> N [ty] [tx]) // The distance from the current point to the end is greater than the distance from the point to the end, number of steps {dp [y] [x] + = DFS (tx, ty);} return dp [y] [x]; // when the range of the current vertex is completed, return the range of the other vertex where it is located} int main () {int I, j; _ int64 k; while (scanf ("% d", & n)> 0) {for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) {scanf ("% d", & map [I] [j]); N [I] [j] =-1; dp [I] [j] = 0;} BFS ();/* for (I = 1; I <= n; I ++) {printf ("\ n"); for (j = 1; j <= n; j ++) printf ("% d ", N [I] [j]);} */k = DFS (1, 1); printf ("% I64d \ n", k );}}

 

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.