java - Maze---> return the maximum sum -
i need find path in stings maze. in maze there numbers can get, , x represents closed position. output must path maximum sum.
i can find path, problem doesn't return maximum sum path. right i'm using 4 recursive calls: 1 left, 1 right, 1 , 1 down. i'm not using backtracking nor considering positions of numbers.
this code got far:
public static boolean verifica(char [][] t, int lin, int col){ try{ if(t[lin][col] == livre){ return true; }else if(t[lin][col]=='1'){ return true; }else if(t[lin][col]=='2'){ return true; }else if(t[lin][col]=='3'){ return true; }else if(t[lin][col]=='4'){ return true; }else if(t[lin][col]=='5'){ return true; }else if(t[lin][col]=='6'){ return true; }else if(t[lin][col]=='7'){ return true; }else if(t[lin][col]=='8'){ return true; }else if(t[lin][col]=='9'){ return true; } } catch(exception e){ } return false; } public static void imprime(char [][] t){ for(int = 0; < t.length; i++){ for(int j = 0; j < t[i].length; j++){ if(t[i][j] ==ocupado) system.out.print(" x "); else if(t[i][j] == livre) system.out.print(" . "); else system.out.printf("%2c ", t[i][j]); } system.out.println(); } system.out.println(); } public static boolean encontracaminho(char[][]m,int lin, int col,int d_lin, int d_col,char passo){ if(verifica(m,lin,col)){ caminhototal++; if(m[lin][col]=='1'){ soma+=1; }else if(m[lin][col]=='2'){ soma+=2; }else if(m[lin][col]=='3'){ soma+=3; }else if(m[lin][col]=='4'){ soma+=4; }else if(m[lin][col]=='5'){ soma+=5; }else if(m[lin][col]=='6'){ soma+=6; }else if(m[lin][col]=='7'){ soma+=7; }else if(m[lin][col]=='8'){ soma+=8; }else if(m[lin][col]=='9'){ soma+=9; } m[lin][col]=passo; (int = 0; < tlin.length;i++ ) { if(tlin[i]==-1){ tlin[i]=lin; break; } } (int = 0; < tcol.length;i++ ) { if(tcol[i]==-1){ tcol[i]=col; break; } } imprime(m); if(lin==d_lin && col ==d_col){ imprime(m); return true; } if(verifica(m,lin+1,col)) return encontracaminho(m, lin+1, col, d_lin, d_col, passo); else if(verifica(m,lin,col-1)){ encontracaminho(m, lin, col-1, d_lin, d_col, passo); }else if(verifica(m,lin-1,col)){ encontracaminho(m, lin-1, col, d_lin, d_col, passo); }else if(verifica(m,lin,col+1)){ encontracaminho(m, lin, col+1, d_lin, d_col, passo); }else{ soma=0; caminhototal=0; (int = 0; < tlin.length; i++) { tlin[i]=-1; } (int = 0; < tcol.length; i++) { tcol[i]=-1; } } } return false; } public static void lematriz(){ scanner in = new scanner(system.in); int lin = in.nextint(); int col = in.nextint(); mapa = new char[lin][col]; in.nextline(); (int = 0; < lin; i++) { string linha = in.nextline(); (int j = 0; j < col; j++) { mapa[i][j] = linha.charat(j); if(mapa[i][j]=='1'){ //aqui devemos fazer soma da coluna system.out.println(mapa[i][j]); }else if(mapa[i][j]=='2'){ system.out.println(mapa[i][j]); }else if(mapa[i][j]=='3'){ system.out.println(mapa[i][j]); }else if(mapa[i][j]=='4'){ system.out.println(mapa[i][j]); }else if(mapa[i][j]=='5'){ system.out.println(mapa[i][j]); }else if(mapa[i][j]=='6'){ system.out.println(mapa[i][j]); }else if(mapa[i][j]=='7'){ system.out.println(mapa[i][j]); }else if(mapa[i][j]=='8'){ system.out.println(mapa[i][j]); }else if(mapa[i][j]=='9'){ system.out.println(mapa[i][j]); } } } //endereço de origem int i_lin=in.nextint(); int i_col=in.nextint(); //endereço de chegada int d_lin=in.nextint(); int d_col=in.nextint(); encontracaminho(mapa, i_lin, i_col, d_lin, d_col,'0'); } public static void main(string[] args){ (int = 0; < tlin.length; i++) { tlin[i]=-1; } (int = 0; < tcol.length; i++) { tcol[i]=-1; } lematriz(); //leitura dos dados finais system.out.printf("%d %d",caminhototal,soma); system.out.println(); (int = 0; < tlin.length; i++) { if(tlin[i]!=-1 || tcol[i]!=-1){ system.out.print(tlin[i]+" "); system.out.println(tcol[i]); } } }
Comments
Post a Comment