Member 11274452
// надеюсь, это может помочь
import java.util.Scanner;
public class frogjump {
static int u,o;
static int min(int x, int y, int m, int n, int a[][],frogjumpdp tc[]){
tc[(m*4)+n].vis = 1;
if(m<0 || n<0 || m>3 || n>3 || a[m][n] == 0){
return 100;
}
else if(m==x && n==y){
return 0;
}
else{
int q,w,e,r;
if(m-1>=0 && n>=0 && m-1<4 && n<4){
if( a[m-1][n] == 1 && tc[((m-1)*4)+n].val == -1 && tc[((m-1)*4)+n].vis == 0){
tc[((m-1)*4)+n].vis = 1;
q = min(x,y,m-1,n,a,tc);
tc[((m-1)*4)+n].val = q;
}
else{
if(tc[((m-1)*4)+n].val != -1){
q = tc[((m-1)*4)+n].val;
}
else{
q = 100;
}
}
}
else{
q = 100;
}
if(m+1>=0 && n>=0 && m+1<4 && n<4 ){
if( a[m+1][n] == 1 && tc[((m+1)*4)+n].val == -1 && tc[((m+1)*4)+n].vis == 0){
tc[((m+1)*4)+n].vis = 1;
w = min(x,y,m+1,n,a,tc);
tc[((m+1)*4)+n].val = w;
}
else{
if(tc[((m+1)*4)+n].val != -1){
w = tc[((m+1)*4)+n].val;
}
else{
w = 100;
}
}
}
else{
w = 100;
}
if(m>=0 && n-1>=0 && m<4 && n-1<4 ){
if( a[m][n-1] == 1 && tc[(m*4)+n-1].val == -1 && tc[(m*4)+n-1].vis == 0){
tc[(m*4)+n-1].vis = 1;
e = min(x,y,m,n-1,a,tc);
tc[(m*4)+n-1].val = e;
}
else{
if(tc[(m*4)+n-1].val != -1){
e = tc[(m*4)+n-1].val;
}
else{
e = 100;
}
}
}
else{
e = 100;
}
if(m>=0 && n+1>=0 && m<4 && n+1<4){
if( a[m][n+1] == 1 && tc[(m*4)+n+1].val == -1 && tc[(m*4)+n+1].vis == 0){
tc[(m*4)+n+1].vis = 1;
r = min(x,y,m,n+1,a,tc);
tc[(m*4)+n+1].val = r;
}
else{
if(tc[(m*4)+n+1].val != -1){
r = tc[(m*4)+n+1].val;
}
else{
r = 100;
}
}
}
else{
r = 100;
}
int t = mini(mini(q,w),mini(e,r));
if( t == q || t == w){
t++;
}
return t;
}
}
static int mini(int x,int y)
{
return((x<y)?x:y);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter rows of the array: ");
u = scan.nextInt();
System.out.println("Please enter columns of the array: ");
o = scan.nextInt();
int cost[][] = new int[u][o] ;
for(int i=0 ; i<u ; i++){
for(int j=0 ; j<o ; j++){
System.out.println("Please enter element array[" + i + "][" + j + "]:");
cost[i][j] = scan.nextInt();
}
}
System.out.println("Please enter x cord of start pos: ");
int x = scan.nextInt();
System.out.println("Please enter y cord of start pos: ");
int y = scan.nextInt();
System.out.println("Please enter x cord of dest pos: ");
int m = scan.nextInt();
System.out.println("Please enter y cord of dest pos: ");
int n = scan.nextInt();
/*int cost[][]= { {1, 1, 1, 0},
{1, 1, 1, 1},
{1, 0, 1, 1},
{1, 1, 1, 0} };*/
frogjumpdp[] tc = new frogjumpdp[u*o];
for ( int i=0; i<u*o; i++) {
tc[i]=new frogjumpdp();
}
int t = min(x,y,m,n,cost,tc);
if(t != 10000){
System.out.println("minimum cost to reach (" + m +"," + n + ") from (" + x +"," + y +") is :" + t);
}
else{
System.out.println("minimum cost to reach (" + m +"," + n + ") from (" + x +"," + y +") is not reachable");
}
}
}
class frogjumpdp {
public int val,vis;
frogjumpdp(){
val = -1;
vis = 0;
}
}