Path Sum:four Ways
Note:this problem is a significantly more challenging version of Problem 81.
In the 5 by 5 matrix below, the minimal path sum is from the top of the bottom right, by moving left, right, up, and Dow N, is indicated in bold red and was equal to 2297.
| |
|
|
|
|
| 131 |
673 |
234 |
103 |
18 |
| 201 |
96 |
342 |
965 |
150 |
| 630 |
803 |
746 |
422 |
111 |
| 537 |
699 |
497 |
121 |
956 |
| 805 |
732 |
524 |
37 |
331 |
Find the minimal path sum, in Matrix.txt (right click and "Save link/target as ..."), a 31K text file containing a by 80 Matrix, from the top left to the bottom right by moving left, right, up, and down.
Path and: Four directions
Note: This is one of the most challenging versions of question 81st.
In the following 5-by-5 matrix, the smallest path that moves up, down, left, or right from the upper-left corner to the lower-right corner is 2297, given by a path labeled red.
| |
|
|
|
|
| 131 |
673 |
234 |
103 |
18 |
| 201 |
96 |
342 |
965 |
150 |
| 630 |
803 |
746 |
422 |
111 |
| 537 |
699 |
497 |
121 |
956 |
| 805 |
732 |
524 |
37 |
331
|
In this 31K text file Matrix.txt (right click and select "Save Target as ...") contains a 80-by-80 matrix, finding the smallest path that moves up, down, left, or right from the upper-left corner to the lower-right corner.
Solving
The expression is very complex, this should be used to figure, Dijkstra algorithm can be solved.
Refer to the program in the problem-solving forum, that is, the Dijkstra algorithm, just implemented with Python, relatively simple.
Implementation ideas:
1. Start the current node (0,0) and find the shortest path in the adjacent node
2. Is the shortest Path node location save
3, according to the node saved in 2, and then find the shortest path to the adjacent node
Python
Import Timedefreaddata (filename): FL=open (filename) data=[] forRowinchFl:row= Row.split (',') Line= [Int (i) forIinchRow] Data.append (line) fl.close ()returnDatadefnext_steps (POS): (j,i)=POSifi+1<Size:right= Minnum[j,i] + data[j][i+1] ifright< minnum[j,i+1]: Minnum[j,i+1] =Right Next_list.append ((J,i+1)) ifj+1<Size:down= Minnum[j,i] + data[j+1][i]ifDown < minnum[j+1, I]: minnum[j+1,i] =Down Next_list.append (J+1, i)) ifI-1 >-1: Left= Minnum[j,i] + data[j][i-1] ifLeft < Minnum[j,i-1]: Minnum[j,i-1] =Left next_list.append ((J,i-1)) ifJ-1 >-1: up= Minnum[j,i] + data[j-1][i]ifUp < Minnum[j-1, I]: minnum[j-1,i] =Up Next_list.append ((J-1, i)) T0=time.time () filename='E:/java/projecteuler/src/level3/p083_matrix.txt'Data=readdata (filename) Size= 80Infinity= 10**10Minnum= {} forIinchRange (0,size): forJinchRange (0,size): Minnum[j,i]=Infinity Next_list=[] minnum[0,0]=data[0][0]test=[(0,0)] whiletest!=[]: Next_list= [] forElinchtest:next_steps (EL) test=next_listPrintMinnum[size-1,size-1]t1=time.time ()Print "running Time=", (T1-T0),"s"#425185#running time= 0.112999916077 s
Java
PackageLevel3;ImportJava.io.BufferedReader;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.util.ArrayList; Public classpe083{Static int[] grid; Static voidRun ()throwsioexception{String filename= "Src/level3/p083_matrix.txt"; String lineString= ""; ArrayList<String> ListData =NewArraylist<string>(); BufferedReader Data=NewBufferedReader (Newfilereader (filename)); while((lineString = Data.readline ())! =NULL) {listdata.add (lineString); } //The defined grid of the allocated size space does not have a defined sizeAssignarray (Listdata.size ()); //add to array grid by row for(intindex = 0,row_counter=0;index <=listdata.size ()-1;++index,row_counter++) {Populatearray (Listdata.get (index), row_counter); } intresult = Minpath (grid,0,0,80-1,80-1); SYSTEM.OUT.PRINTLN (result); } //Matrix[a][b] to matrix[c][d] minimum value Public Static intMinpath (int[] Matrix,intAintBintCintd) { int[] D =New int[Matrix.length] [Matrix[0].length]; for(inti=0;i<d.length;i++) for(intj=0;j<d[0].length;j++) D[i][j]=Integer.max_value; D[A][B]=Matrix[a][b]; intx=a,y=b; while(true){ //calculates the path of the X Y node to the upper and lower left and right four directions, if the small one is updated//under if(X < D.length-1) if(D[x+1][y] > 0) D[x+1][y] = Math.min (Matrix[x+1][y] + d[x][y], d[x+1][y]); //Right if(y<d[0].length-1) if(D[x][y+1] >0) D[x][y+1] = Math.min (matrix[x][y+1] + d[x][y], d[x][y+1]); //on if(x>0) if(D[x-1][y] >0) D[x-1][y] = Math.min (Matrix[x-1][y] + d[x][y], d[x-1][y]); //left if(y>0) if(d[x][y-1]>0) D[x][y-1] = Math.min (Matrix[x][y-1] + d[x][y], d[x][y-1]); if(X==c && y==d)returnD[x][y]; //the nodes that have been visited take their opposite numberD[x][y] =-D[x][y]; //Select Next Node//in the node that is not accessed, select the path value of the least intMin =Integer.max_value; for(inti=0;i< d.length;i++){ for(intj=0;j<d[0].length;j++){ if(d[i][j]>0 && D[i][j] <min) {min=D[i][j]; X=i; Y=J; } } } } } Public Static intPath_min (int[] A) { intSize =a.length; intB[][] =New int[Size][size]; b[0][0] = a[0][0]; b[0][1] = a[0][0] + a[0][1]; b[1][0] = a[0][0] + a[1][0]; for(inti = 1;i<size; i++){ for(intj = 1;j<size; J + +) {B[i][j]= A[i][j] + get4min (b[i-1][j],b[i+1][j], b[i][j-1],b[i][j+1]); } } returnB[size-1][size-1]; } Public Static intGet4min (intAintBintCintd) { intMin1 =Math.min (A, b); intMin2 =Math.min (c, D); returnmath.min (min1, min2); } //each row of data is added to the array Public Static voidPopulatearray (String str,introw) { intCounter = 0; string[] Data= Str.split (","); for(intindex = 0;index<=data.length-1;++index) {Grid[row][counter++] =Integer.parseint (Data[index]); } } Public Static voidAssignarray (intNo_of_row) {Grid=New int[No_of_row][no_of_row]; } Public Static voidMain (string[] args)throwsioexception{LongT0 =System.currenttimemillis (); Run (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS");//425185//running Time=0s187ms }}Java Code
Project Euler 83:path Sum:four Ways Path and: 4 directions