HDNOIP201404 Shortest Path

Source: Internet
Author: User

HDNOIP201404 Shortest Path
Difficulty level: A; programming language: Unlimited; run time limit: 1000ms; run space limit: 51200KB; code length limit: 2000000B
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
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
There is only a minimum cost or-1 this integer.
Input example
"Input Sample 1"
4
1231
2003
1002
1113
"Input Sample 2"
4
3150
1153
3311
0530
Output example
"Output Example 1"
10
"Output Example 2"
-1
Other Notes
60% of data, n<10,80% data, n<100,100% data, n<1000

It is a good question indeed.

1000*1000 the shortest possible short-circuit may be a bit difficult, measured card when 1000s+. So how do you do it?

In addition to ' 0 ', the matrix can contain up to 3 numbers of characters.

This reminds us that we can do some articles on it. Consider why the heap is used to optimize Dijkstra because some edges are very long and some edges are very short, and for all the same points in the edge, the distance to them is incremented.

The algorithm is out of the way, with 3 monotone queues instead of Heap, pay attention to how to take the team head and how to join the team tail each time.

#include <cstdio>#include<cctype>#include<queue>#include<cstring>#include<algorithm>#defineRep (i,s,t) for (int i=s;i<=t;i++)#defineren for (int i=first[x];i!=-1;i=next[i])using namespaceStd;inlineintRead () {intx=0, f=1;CharC=GetChar ();  for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1;  for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=1010;CharA[MAXN][MAXN];intN,D[MAXN][MAXN],VIS[MAXN][MAXN],IDX[MAXN],TP;structPoint {intx, y; BOOL operator< (Constpoint& ths) {returnd[x][y]<d[ths.x][ths.y];}; Queue<Point> q[3];intGetfront () {intc=-1; if(q[0].size ()) c=0; if(q[1].size () && (c<0|| q[1].front () <q[c].front ()) c=1; if(q[2].size () && (c<0|| q[2].front () <q[c].front ()) c=2; returnC;}intmx[]={1,-1,0,0},my[]={0,0,1,-1};intsolve () {if(a[1][1]=='0'|| a[n][n]=='0')return-1; q[idx[a[1][1]]].push (point) {1,1});d [1][1]=a[1][1]-'0';  while(q[0].size () +q[1].size () +q[2].size ()) {intT=getfront ();intX=q[t].front (). x,y=Q[t].front (). Y;q[t].pop (); if(x==n&&y==n)returnD[x][y]; if(Vis[x][y])Continue; vis[x][y]=1; Rep (dir,0,3) {            intnx=x+mx[dir],ny=y+My[dir]; if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&a[nx][ny]!='0'&&d[x][y]+a[nx][ny]-'0'<D[nx][ny]) {D[nx][ny]=d[x][y]+a[nx][ny]-'0';                                           Q[idx[a[nx][ny]]].push (point) {nx,ny}); }                  }                                             }    return-1;}intMain () {n=read (); Rep (I,1, N) scanf ("%s", a[i]+1); memset (IDX,-1,sizeof(IDX)); Rep (I,1, N) Rep (J,1, N) {        if(idx[a[i][j]]<0&&a[i][j]!='0') idx[a[i][j]]=tp++; D[I][J]=1<< -; } printf ("%d\n", Solve ()); return 0;}
View Code

HDNOIP201404 Shortest Path

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.