Hdu 1428 very good one topic memory Search + wide search realizes Dijkstra

Source: Internet
Author: User
Walk the campus Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4369 Accepted Submission (s): 1355


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 each set of test data of the first behavior N (2=<n<=50), the next n rows have n number per row, representing the time elapsed through each region T (0&LT;T&LT;=50) (because the bedroom and room are on the third floor, so the starting point and the end will be time-consuming).

Output outputs the total number of routes (less than 2^63) for each set 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

6

Code:

/* This topic is mainly to understand test instructions, he said a to B area exists a B to the end of the area B any one to the end of the area is less, in fact, the ball out of the n,n point to the shortest path of the other road, and then memory search the upper left to the lower right corner of the distance, but the previous step than the shortest distance
include<stdio.h> #include <string.h> #include <queue> using namespace std;
#define MAX #define INF 999999999 struct Node {int x, y;};
int n;
int Map[max][max];
int Dis[max][max];
Long long int Dp[max][max];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; BOOL Jude (int x,int y) {if (x<1| | x>n| | y<1| |
    Y>n) return false;
return true; } void BFS ()//bfs to find the shortest path is the Dijkstra algorithm {for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) dis[i][

     J]=inf;
    } queue<node>q;
    Node Next={n,n};
    Q.push (next);
    Dis[n][n]=map[n][n];
        while (!q.empty ()) {node A=q.front ();
        Q.pop ();
            for (int i=0;i<4;i++) {int xx=a.x+dir[i][0];

            int yy=a.y+dir[i][1];
            if (!jude (XX,YY)) continue;
           if (Dis[xx][yy]>dis[a.x][a.y]+map[xx][yy]) {dis[xx][yy]=dis[a.x][a.y]+map[xx][yy];//As soon as this state has been updated to the short, go into the queue to follow the new points he can reach.
                Node Nexttt={xx,yy};
            Q.push (NEXTTT);
        }}}} "long long int dfs (int x,int y)//Memory search is not a problem; {if (dp[x][y]!=-1) return dp[x][y];
        Long long int count=0;
            for (int i=0;i<4;i++) {int xx=x+dir[i][0];
            int yy=y+dir[i][1];
            if (!jude (XX,YY)) continue;
            if (Dis[xx][yy]<dis[x][y]) {Count+=dfs (XX,YY);
        }} Dp[x][y]=count;
return dp[x][y];
              } int main () {while (~SCANF ("%d", &n)) {for (int. i=1;i<=n;i++) {for (int j=1;j<=n;j++)
        scanf ("%d", &map[i][j]);
       } BFS ();
            /* for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) printf ("%d", dis[i][j]);
        printf ("\ n");
       }*/memset (Dp,-1,sizeof (DP)); Dp[n][n]=1;
        DFS (a);
    printf ("%lld\n", dp[1][1]);
 }
}


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.