Problem Description:

LL recently addicted to AC extricate oneself, every day bedroom, room 2.1 line. Lack of exercise because of sitting on the computer for a long time. He decided to take full advantage of every time from the bedroom to the computer room to take a walk on campus. The entire HDU campus is a square layout that can be divided into n*n squares, representing each area. For example, I will live in the 18th dormitory located in the northwest corner of the campus, that is, the square (a quarter) representative of the place, and the room is located in the third laboratory building in the southeast End (N,n). Because there are many routes to choose from, LL hope to take a different route each time. In addition, he considered from the a area to the B area only when there is a route from B to the machine room is closer than any one from a to the computer room (otherwise it may never be able to go to the engine room ...). What he wants to know now is how many lines there are to meet the requirements. Can you tell him?

Input:

The first behavior of each group of test data N (2=<n<=50), the next n rows have n number of rows per row, representing the time spent in each region T (0<T<=50) (because the bedroom and the room are on the third floor, so the starting point and the end will be time-consuming).

Output:

For each set of test data, the total number of routes is output (less than 2^63).

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

6

#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath > #include <vector> #include <queue> #include <stack> #include <set> #include <map># Define ll long longusing namespace Std;const Int. MAXN = + 10;int c[maxn][maxn];int dis[maxn][maxn];long long dp[maxn][ Maxn];int Vis[maxn][maxn];int Move[][2] = {{-1, 0}, {1, 0}, {0,-1}, {0, 1}};int n;struct node{int x, y; int step; BOOL operator < (const node& RHS) Const {return step > rhs.step; }};void BFs () {priority_queue<node> Q; memset (Vis, 0, sizeof (VIS)); Node Pre, now; pre.x = PRE.Y = N; Pre.step = C[n][n]; Vis[n][n] = 1; Dis[n][n] = C[n][n]; Q.push (pre); while (! Q.empty ()) {pre = Q.top (); Q.pop (); if (pre.x > 1 &&!vis[pre.x-1][pre.y]) {now.x = pre.x-1; Now.y = Pre.y; Now.step = Pre.step + c[now.x][now.y]; DIS[NOW.X][NOW.Y] = Now.step; VIS[NOW.X][NOW.Y] = 1; Q.push (now); } if (Pre.x < n &&!vis[pre.x+1][pre.y]) {now.x = pre.x + 1; Now.y = Pre.y; Now.step = Pre.step + c[now.x][now.y]; DIS[NOW.X][NOW.Y] = Now.step; VIS[NOW.X][NOW.Y] = 1; Q.push (now); } if (Pre.y > 1 &&!vis[pre.x][pre.y-1]) {now.x = pre.x; Now.y = pre.y-1; Now.step = Pre.step + c[now.x][now.y]; DIS[NOW.X][NOW.Y] = Now.step; VIS[NOW.X][NOW.Y] = 1; Q.push (now); } if (Pre.y < n &&!vis[pre.x][pre.y+1]) {now.x = pre.x; NOW.Y = pre.y + 1; Now.step = Pre.step + c[now.x][now.y]; DIS[NOW.X][NOW.Y] = Now.step; VIS[NOW.X][NOW.Y] = 1; Q.push (now); }}}long long Solve (int x, int y) {if (Dp[x][y]! =-1) return dp[x][y]; Dp[x][y] = 0; if (x > 1 && dis[x][y] > Dis[x-1][y]) dp[x][y] + = Solve (x-1, y); if (x < n && dis[x][y] > Dis[x+1][y]) dp[x][y] + = Solve (x+1, y); if (Y > 1 && dis[x][y] > Dis[x][y-1]) dp[x][y] + = Solve (x, y-1); if (Y < n && dis[x][y] > Dis[x][y+1]) dp[x][y] + = Solve (x, y+1); return dp[x][y];} int main () {while (scanf ("%d", &n)!=eof) {for (int. i=1;i<=n;i++) {for (int j=1;j<= n;j++) scanf ("%d", &c[i][j]); } BFS (); for (int i=1;i<=n;i++)//{//for (int j=1;j<=n;j++)//cout << Dis[i][j] <& Lt ' '; cout << Endl; } for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) DP[I][J] =-1; } Dp[n][n] = 1; printf ("%i64d\n", Solve (1, 1)); } return 0;}

HDU 1428 Stroll Campus (BFS + memory Search)