HD201404 Shortest Path
"Question description"
A, B, and C are 3 non-equal 1-bit positive numbers, with them and the number 0 can fill an n row n column of the grid array, each lattice has 4 kinds of digital one. A grid filled with 0 indicates an obstacle and cannot belong to any path. Can you find a path that starts at 1 rows, 1 columns, and reaches n rows n columns with the least cost? Note: Each lattice can only go to the adjacent upper, lower, left, and right non-0 and not out of bounds. The so-called path cost refers to the sum of the numbers in all the squares through which the path passes. You can program the minimum path cost to reach N rows n columns from the position of 1 rows and 1 columns, and output-1 if you cannot reach it.
"Input Requirements"
The first line enters the number n.
The next n rows are a number string of length n, and the N strings form the square of a number symbol. In addition to ' 0 ', the matrix can contain up to 3 numbers of characters.
"Output Requirements"
There is only a minimum cost or-1 this integer.
"Input Instance"
"Input Example 1" 41231200310021113 "Input Sample 2" 43150115333110530
"Output instance"
"Output Sample 1" 10 "output Example 2"-1
"Other Notes"
60% of data, n<10,80% data, n<100,100% data, n<1000
"Analysis of Test questions"
First glance: This is not the maze it!? Look at me for a second!!
Second eye: Not quite the same, the maze is to take a step ans++, this is to add a variable
Third Eye: on DFS code!!
So it took 1 hours to yy such a code:
#include <iostream> #include <cstring>using namespace Std;char map[1001][1001],cinm[1001][1001];int a[ 1001][1001],ans=0,m,n,k[1001][1001],k1[1001][1001],mk=0;//k unchanged, only k1.void res (int u,int v,int i,int j) {int t=k1[u][v ]; if (u==i&&v==j) ans=t,mk=1; if (v<m-1&&map[u][v+1]!= ' # ' &&a[u][v+1]>t+k[u][v+1]) {K1[u][v+1]=k[u][v+1]+t;a[u][v+1]=k1[u ][V+1]; Res (U,V+1,I,J); } if (u>0&&map[u-1][v]!= ' # ' &&a[u-1][v]>t+k[u-1][v]) {k1[u-1][v]=k[u-1][v]+t;a[u-1][v]=k 1[U-1][V]; Res (U-1,V,I,J); } if (v>0&&map[u][v-1]!= ' # ' &&a[u][v-1]>t+k[u][v-1]) {k1[u][v-1]=k[u][v-1]+t;a[u][v-1]=k 1[u][v-1]; Res (U,V-1,I,J); } if (u<n-1&&map[u+1][v]!= ' # ' &&a[u+1][v]>t+k[u+1][v]) {K1[u+1][v]=k[u+1][v]+t;a[u+1][v] =K1[U+1][V]; Res (U+1,V,I,J); }}int Main () {cin>>n; M=n; for (int i=0;i<n;i++) {cin>>Cinm[i]; for (int j=0;j<n;j++) {k[i][j]=cinm[i][j]-' 0 '; K1[I][J]=K[I][J]; }} int startx=0,starty=0,endx=n-1,endy=n-1; for (int i=0;i<n;i++) for (int j=0;j<m;j++) {if (i==startx&&j==starty) map[i][j]= ' S '; else if (i==endx&&j==endy) map[i][j]= ' T '; if (k[i][j]==0) map[i][j]= ' # '; Else map[i][j]= '. '; } memset (A,1,sizeof (a)); a[0][0]=0; Res (Startx,starty,endx,endy); if (mk==1) cout<<ans<<endl; else cout<<-1;}
The result doesn't look at the data range, 1000!!?
The result was right, but the time was super.
Think carefully and no God horse method optimization, strike ...
BFS:
Well, if DFS time is overrun, then only BFS can be used.
And then yy the code of a BFS:
#include <iostream>using namespace Std;char graph[1001][1001];int n,m,startx,starty;int dir[4][2]={{1,0},{0,1} , { -1,0},{0,-1}};int vis[1001][1001];int dis[1001][1001];char cinm[1001][1001],k[1001][1001];int Fit (int x,int y) { return (x>=1&&x<=n&&y>=1&&y<=m);} int BFS (int x,int y) {int queue[100000]; int i,head=0,tail=0; Queue[tail++]=x; Queue[tail++]=y; Vis[x][y]=1; while (head<tail) {int nowx=queue[head++]; int nowy=queue[head++]; for (i=0;i<4;i++) {int tmpx=nowx+dir[i][0]; int tmpy=nowy+dir[i][1]; if (Fit (tmpx,tmpy) &&tmpx==n&&tmpy==n&&graph[tmpx][tmpy]== '. ') return dis[nowx][nowy]+k[ Tmpx][tmpy]; if (Fit (tmpx,tmpy) &&graph[tmpx][tmpy]== '. ' &&dis[nowx][nowy]+k[tmpx][tmpy]<dis[tmpx][tmpy]) {dis[tmpx][tmpy]=dis[nowx][nowy]+k[tmpx][tmpy]; QUEUE[TAIL++]=TMPX; Queue[tail++]=tmpy; }}} return 0;} int main () {int I, j, Startx=1,starty=1; scanf ("%d", &n); M=n; for (int i=1;i<=n;i++) {scanf ("%s", cinm[i]+1); for (int j=1;j<=n;j++) k[i][j]=cinm[i][j]-' 0 ', dis[i][j]=999999;} DIS[1][1]=K[1][1]; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) {if (k[i][j]==0) graph[i][j]= ' # '; Else graph[i][j]= '. '; } int ans=0; ANS=BFS (Startx,starty); if (ans!=0) printf ("%d\n", ans); else printf (" -1\n");} /*73221000311001231230311123301221112300123333312001*/
The result is wrong.
Some of the points really can be a, but there are some points that are not.
Suddenly found such a condition: up to three in addition to 0 different numbers, and then yy a three queue maintenance code:
#include <iostream> #include <cstring> #include <queue>using namespace Std;inline int read () {int x=0,f =1;char C=getchar (); for (;! IsDigit (c); C=getchar ()) if (c== '-') f=-1; for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} Char cinm[1001][1001];int n,dis[1001][1001],vis[1001][1001],number[1001],timek,k[1001][1001];struct P{int x, y; BOOL operator < (const p& ths) {return dis[x][y]<dis[ths.x][ths.y];}}; Queue<p> que[3];int Getfront () {int c=-1; if (Que[0].size ()) c=0; if (Que[1].size () && (c<0| | Que[1].front () <que[c].front ()) c=1; if (Que[2].size () && (c<0| | Que[2].front () <que[c].front ()) c=2; return c;} int Fit (int x,int y) {return (x>=1&&x<=n&&y>=1&&y<=n);} int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//Direction array int BFS () {if (k[1][1]==0| | k[n][n]==0) return-1; Que[number[cinm[1][1]]].push (P) {n});D is[1][1]=k[1][1]; while (Que[0].size () +que[1].size () +que[2].size ())//When the size of the three queues and non-empty {int T=getfront (); int X=que[t].front (). X;int Y=que[t].front (). y;//Get coordinates que[t].pop (); if (x==n&&y==n) return dis[x][y]; if (vis[x][y]==1) continue; Vis[x][y]=1; for (int i=0;i<=3;i++) {int nx=x+dir[i][0];int ny=y+dir[i][1]; if (Fit (Nx,ny) &&k[nx][ny]!=0&&dis[x][y]+k[nx][ny]<dis[nx][ny]) {dis[nx][ny]=dis[x][y]+k[n X][ny]; Que[number[cinm[nx][ny]]].push ((P) {nx,ny}); }}} return-1;} int main () {n=read (); for (int i=1;i<=n;i++) scanf ("%s", cinm[i]+1); memset (number,-1,sizeof (number)); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {if (number[cinm[i][j]]<0&&cinm[i][j]!= ') 0 ') number[cinm[i][j]]=timek++;//for non-0-element marking dis[i][j]=99999999; k[i][j]=cinm[i][j]-' 0 '; } printf ("%d\n", BFS ());}
Code
#include <iostream> #include <cstring> #include <queue>using namespace Std;inline int read () {int x=0,f =1;char C=getchar (); for (;! IsDigit (c); C=getchar ()) if (c== '-') f=-1; for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} Char cinm[1001][1001];int n,dis[1001][1001],vis[1001][1001],number[1001],timek,k[1001][1001];struct P{int x, y; BOOL operator < (const p& ths) {return dis[x][y]<dis[ths.x][ths.y];}}; Queue<p> que[3];int Getfront () {int c=-1; if (Que[0].size ()) c=0; if (Que[1].size () && (c<0| | Que[1].front () <que[c].front ()) c=1; if (Que[2].size () && (c<0| | Que[2].front () <que[c].front ()) c=2; return c;} int Fit (int x,int y) {return (x>=1&&x<=n&&y>=1&&y<=n);} int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//Direction array int BFS () {if (k[1][1]==0| | k[n][n]==0) return-1; Que[number[cinm[1][1]]].push (P) {n});D is[1][1]=k[1][1]; while (Que[0].size () +que[1].size () +que[2].size ())//When the size of the three queues and non-empty {int T=getfront (); int X=que[t].front (). X;int Y=que[t].front (). y;//Get coordinates que[t].pop (); if (x==n&&y==n) return dis[x][y]; if (vis[x][y]==1) continue; Vis[x][y]=1; for (int i=0;i<=3;i++) {int nx=x+dir[i][0];int ny=y+dir[i][1]; if (Fit (Nx,ny) &&k[nx][ny]!=0&&dis[x][y]+k[nx][ny]<dis[nx][ny]) {dis[nx][ny]=dis[x][y]+k[n X][ny]; Que[number[cinm[nx][ny]]].push ((P) {nx,ny}); }}} return-1;} int main () {n=read (); for (int i=1;i<=n;i++) scanf ("%s", cinm[i]+1); memset (number,-1,sizeof (number)); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {if (number[cinm[i][j]]<0&&cinm[i][j]!= ') 0 ') number[cinm[i][j]]=timek++;//for non-0-element marking dis[i][j]=99999999; k[i][j]=cinm[i][j]-' 0 '; } printf ("%d\n", BFS ());}
"Hdnoip" HD201404 Shortest Path