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
Consider from Area A to Area B if there is only one route from B to the data center closer than any one from A to the data center (otherwise, it may never go to 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
31 2 31 2 31 2 331 1 11 1 11 1
Sample output
16 *************************************** **************************************** **************************************** * ****** question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1428 the question asks how many routes are there in the upper left corner to the lower right corner. The shortest distance from Area A to Area B as described in the question is required. The topic is highlighted above. Method: memory-based search (BFS + DFS + status record) Code :# Include <iostream> # include <cstdio> # include <cstring> # include <queue> # define ll _ int64using namespace STD; ll N, TOT; ll map [55] [55], vis [55] [55]; // figure, the status of each vertex (How many paths are there) ll dis [55] [55]; // The shortest distance from the end point to each point ll dir [] [2] = {0,-1 }}; // direction struct node {int X, Y, step ;}; queue <node> que; // queue void BFS () {// wide search, determine the distance from the end point to node now, next; int I, j; now. X = N, now. y = N, now. step = 0, TOT = 0; que. push (now); DIS [N] [N] = map [N] [N]; Wh Ile (! Que. empty () {now = que. front (), que. pop (); for (I = 0; I <4; I ++) {next. X = now. X + dir [I] [0], next. y = now. Y + dir [I] [1], next. step = now. step + 1; if (next. x <1 | next. Y <1 | next. x> N | next. y> N) continue; If (DIS [next. x] [next. y]> dis [now. x] [now. y] + map [next. x] [next. y] | dis [next. x] [next. y] =-1) {dis [next. x] [next. y] = dis [now. x] [now. y] + map [next. x] [next. y]; que. push (next) ;}}} ll DFS (ll x, ll y) {// deep search to determine how many paths can reach int Sx, Sy, I; If (VI S [x] [Y]) return vis [x] [Y]; // The path has passed, and the existing value is returned (memory-based search) if (x = N & Y = N) {// It can reach the end. There is one more path, return 1; return 1 ;}for (I = 0; I <4; I ++) {SX = x + dir [I] [0], Sy = Y + dir [I] [1]; if (SX> N | Sy> N | SX <1 | Sy <1) continue; if (DIS [SX] [sy]> = dis [x] [Y]) continue; // select the short path vis [x] [Y] + = DFS (sx, Sy);} return vis [x] [Y];}. // main function int main () {ll I, j; while (~ Scanf ("% i64d", & N) {for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) scanf ("% i64d", & map [I] [J]); memset (DIS,-1, sizeof (DIS); memset (VIS, 0, sizeof (VIS); BFS (); DFS (); printf ("% i64d \ n", vis [1] [1]); // memory-based search, returns from the end, so it is vis [1] [1]} return 0 ;}
In particular, where is the memory-based search? in DFS, if we change the code below without memory-based search (General Deep Search ):
Void DFS (int x, int y) {int Sx, Sy, I; If (x = N & Y = N) {tot ++; // return;} for (I = 2; I <4; I ++) {SX = x + dir [I] [0], Sy = Y + dir [I] [1]; If (SX> N | Sy> N) continue; if (DIS [SX] [sy]> = dis [x] [Y]) continue; DFS (sx, Sy );}}
This will cause the crash !! Tle, MLE. Let's look at another figure. The most basic understanding of the memory-based search: deep search starts with vis [1] [1, for the first time, retrieve vis [N] [N] directly, and then return the path of vis [n-1] [N-2] that point, when the second search for VI [n-1] [N-2] This point of time does not need to search down, directly add in this point of the road, OK, save time.