C Language Memory Search ___ Stroll Campus (Hdu 1428)

Source: Internet
Author: User

Problem DESCRIPTIONLL 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
31 2 31 2 31 2 331 1 11 1 11 1 1

Sample Output
16

Test instructions: Each point is an area, if this area has an attribute K is the shortest distance from the area to the room, then ll can only from K large to K small area walk (same also can't walk), ask a total of several ways.


Analysis:

Because the map scale reached 50x50, the direct DFS violence strong search absolute timeout, and from the answer on the way to reach 2^63 can also know that the violence of a strong search must time out.

This time we can consider the memory search (dp+ search).

(1) The shortest distance from each point to the machine room is obtained, this can use the shortest path algorithm (DIJJSTRA.SPFA), can also use the DP, also can use BFS, as well as priority queue. (But this problem I use Dijkstra processing when do not know is posture is wrong or how, timed out, according to reason the maximum size of 2,500 points, Dijskstra time complexity for O (n^2) should also not time out, do not understand.)

(2) After preprocessing, the shortest distance from each point to the machine room is stored in a two-dimensional array dis[[];

(3) We know that the rules of walking can only from K to K small, then if one of the routes, A, b two points, then a and a two point of the route direction is unique. So we can draw a theorem based on this point:

the number of valid paths in a point to the room is equal to the sum of the number of points adjacent to it.

By this time we have reduced the size of the search to a map that is O (n^2);


Four types of code () that preprocess the shortest distance from each point to the room:


1.Dijkstra (stored in visit [] [])

#include <stdio.h> #include <string.h>int time[2501][2501],map[51][51],n,visit[51][51];int a[4][2]={    0,1,1,0,0,-1,-1,0};int Main () {int i,j; while (scanf ("%d", &n)!=eof) {for (i=0;i<n;i++) for (j=0;j<n;j++) scanf ("%d", &A        MP;MAP[I][J]);                for (i=0;i<n*n;i++) for (j=0;j<n*n;j++) if (i==j) time[i][j]=0;        else time[i][j]=999999;        for (i=1;i<n;i++) time[i-1][i]=time[i][i-1]=map[0][i];            for (i=1;i<n;i++) {time[(i-1) *n][i*n]=time[i*n][(i-1) *n]=map[i][0];                for (j=1;j<n;j++) {time[i*n+j-1][i*n+j]=time[i*n+j][i*n+j-1]=map[i][j];            time[(i-1) *n+j][i*n+j]=time[i*n+j][(i-1) *n+j]=map[i][j];        }}//preprocessing adjacency matrix time[][] int dis[2501],min,u,book[2501]={0}; for (i=0;i<n*n;i++) dis[i]=time[n*n-1][i];        Book[n*n-1]=1;           for (i=1;i<n*n;i++)//Dijkstra algorithm core code {min=999999;           for (j=0;j<n*n;j++) {if (book[j]==0&&dis[j]<min) min=dis[j],u=j;           } book[u]=1; for (j=0;j<n*n;j++) {if (time[u][j]!=999999) {if (dis[j]>dis[               U]+TIME[U][J]) dis[j]=dis[u]+time[u][j]; }}} for (i=0;i<n;i++) for (j=0;j<n;j++) visit[i][j]=dis[i*n+j]+map[i                    ][J]; } return 0;}


2.DP (stored in visit [] [])

#include <stdio.h> #include <string.h>int map[51][51],n,visit[51][51];int a[4][2]={0,1,1,0,0,-1,-1,0};    int main () {int i,j,flag,k,tx,ty; while (scanf ("%d", &n)!=eof) {for (i=0;i<n;i++) for (j=0;j<n;j++) scanf ("%d", &A        MP;MAP[I][J]);        for (i=0;i<n;i++) for (j=0;j<n;j++) visit[i][j]=99999;        flag=1;        VISIT[N-1][N-1]=MAP[N-1][N-1];            while (flag)//flag is the tag variable {flag=0; for (i=n-1;i>=0;i--) {for (j=n-1;j>=0;j--) {for (k=0;k&lt                        ; 4;k++) {tx=i+a[k][0];                        TY=J+A[K][1]; if (tx<0| | tx>=n| | ty<0| |                        Ty>=n) continue; if (Visit[i][j]>visit[tx][ty]+map[i][j])//Judge a point from four directions on the nearest road Flag=1,visit[i][j]=visit[tx][ty ]+map[i][j];                    Once a bit of information has changed flag marked as 1 indicates to continue judging once. }}}} return 0;}

3.BFS (stored in dis[] [])

#include <iostream> #include <stdio.h> #include <string.h> #include <queue>using namespace s Td;typedef __int64 SS; #define MAXX 100000000struct node{SS X, y;};    SS N,t[4][2]={1,0,-1,0,0,1,0,-1};ss dis[100][100],vist[100][100],map[100][100];void DJ () {queue<node>q;    for (SS-i=0;i<=n;i++) for (SS j=0;j<=n;j++) Dis[i][j]=maxx;    Dis[n][n]=map[n][n];    memset (vist,0,sizeof (vist));    Vist[n][n]=1;    node tmp;    Tmp.x=n;    Tmp.y=n;    Q.push (TMP);    while (!q.empty ())//The main idea is to update the information of adjacent points with the point at which information changes. Because the information in the queue is changing, you do not have to search for each point in the above DP.        {node Tmp1=q.front ();        Q.pop ();        vist[tmp1.x][tmp1.y]=0;            for (SS i=0;i<4;i++) {SS xx=tmp1.x+t[i][0];            SS Yy=tmp1.y+t[i][1]; if (xx>=1&&xx<=n&&yy>=1&&yy<=n&&dis[xx][yy]>dis[tmp1.x][tmp1.y]+                Map[xx][yy]) {DIS[XX][YY]=DIS[TMP1.X][TMP1.Y]+MAP[XX][YY];if (!vist[xx][yy]) {node tmp2;                    Tmp2.x=xx;                    Tmp2.y=yy;                    Q.push (TMP2);      Vist[xx][yy]=1;                The visit array is a representation of those points in the queue. }            }        }    }}


4. Priority queue (stored in dis[] []) (Minimum time complexity)

<pre name= "code" class= "CPP" > #include <stdio.h> #include <string.h> #include <queue>using namespace Std;int dis[51][51],map[51][51],k[4][2]={0,1,1,0,0,-1,-1,0},n;int visit[51][51];struct node{int x,y,step; friend bool Operator < (Node A, Node B)    {        return a.step > b.step;//struct, step small Priority    }};p riority_queue& Lt;node>q;int Main () {int i,j,tx,ty;while (scanf ("%d", &n)!=eof) {for (i=0;i<n;i++) for (j=0;j<n;j++) scanf ("%d", &map[i][j]); memset (visit,0,sizeof (visit)); Node A;a.x=n-1,a.y=n-1,a.step=map[n-1][n-1];q.push (a); while (!q.empty ()) {node tmp1=q.top (); Q.pop (); if (visit[tmp1.x][tmp1.y]==0) visit[tmp1.x][tmp1.y]=1,dis[tmp1.x][tmp1.y]=tmp1.step; Elsecontinue;for (i=0;i<4;i++) {tx=tmp1.x+k[i][0];ty=tmp1.y+k[i][1];if (tx<0| | tx>=n| | ty<0| | ty>=n) Continue;node Tmp2;tmp2.x=tx;tmp2.y=ty;tmp2.step=tmp1.step+map[tx][ty];q.push (TMP2);}} return 0;}



Memory search part code (preprocessing in dis[[], search results placed in dp[[])

Long Long Dp[51][51];long long dfs (int x,int y) {if (Dp[x][y]) return dp[x][y];int i,tx,ty;for (i=0;i<4;i++) {tx=x+k[i][ 0];ty=y+k[i][1];if (tx<0| | ty<0| | tx>=n| | ty>=n| | Dis[tx][ty]>=dis[x][y]) Continue;dp[x][y]+=dfs (tx,ty);} return dp[x][y];} int main () {int i,j,tx,ty;while (scanf ("%d", &n)!=eof) {... memset (DP));DP [Dp,0,sizeof ("n-1][n-1]=1;printf" , DFS (0,0));} return 0;}


The Dijkstra preprocessing timed out, and the remaining 3 were AC.

C Language Memory Search ___ Stroll Campus (Hdu 1428)

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.