Wednesday, July 11, 2012


Hi Friends there is a problem .....

a mouse is at one end of the table say (0,0) and table is divided in to the grids ,some blocks are blocked
so it does not know . when he is mowing then it came to know that way is blocked .....
find  a algo and write the code to send the mouse to (n,n) position means at last.





below is the code i made by me only '




Arun Kumar Gupta     cc




import java.util.*;

public class Mouse
{
public static void main (String [] args)
{
//public int backtrack(int r , int t) ;
Scanner sc = new Scanner(System.in);
//System.out.println("Please Enter the Value for Square Array  :");
//int table = sc.nextInt();
//System.out.println("Array is of" +table+"*"+table);
int table = 5;
int row = 7 , colum = 7;
int table1 [] []  = new int[table+2][table+2];
int i =0 , j =0;
for( i = 0 ; i< (row) ; ++i)
{

for( j = 0 ; j< (colum) ; ++j)
{
//System.out.print("i am here");
if((i == 0) || (i == (row -1)   ))
{
//System.out.print("i am here");
table1[i][j] = 0;
}
else if((j == 0) || (j == (colum -1)   ))
{
table1[i][j] = 0;
}

else {
table1[i][j] = 1;
}

}


}
for( i = 0 ; i< (row) ; ++i)
{

for( j = 0 ; j< (colum) ; ++j)
{
System.out.print("\t"+table1[i][j]);
}
System.out.println("\n");

}
System.out.println("\n");
System.out.println("\n");
table1[1][2] = 0;
table1[2][2] = 0;
table1[4][0] = 0;
table1[4][1] = 0;
table1[3][4] = 0;
table1[4][4] = 0;
table1[5][4] = 0;
for( i = 0 ; i< (row) ; ++i)
{

for( j = 0 ; j< (colum) ; ++j)
{
System.out.print("\t"+table1[i][j]);
}
System.out.println("\n");

}
i = 1;
j = 1;
int dx = 1;
int dy = 1;
int pdx = 1 ,pdy = 1;
for(i =1 ; i <= (row *colum) ; ++i)
{
if((dx < row)&& (dy < row))
{
if((dx == (row -2)) && (dy == (colum -2)))
{
System.out.print("\n\n\n\n\nFinally Mouse At the End\n\n\n ");
for( i = 0 ; i< (row) ; ++i)
{

for( j = 0 ; j< (colum) ; ++j)
{
System.out.print("\t"+table1[i][j]);
}
System.out.println("\n");

}
System.exit(0);
}
if( table1[dx][dy+1] == 0)
{
System.out.print("i am here " +dx+ ":" +dy+ "\t0\n");
table1[dx][dy] = 9;
dx = dx +1;
if(dx == (row-1))
{
dx = dx-1;

}

}
if((dx == (row -2)) && (dy == (colum -2)))
{
System.out.print("\n\n\n\n\nFinally Mouse At the End\n\n\n ");
for( i = 0 ; i< (row) ; ++i)
{

for( j = 0 ; j< (colum) ; ++j)
{
System.out.print("\t"+table1[i][j]);
}
System.out.println("\n");

}
System.exit(0);
}
if (table1[dx][dy+1] == 1)
{
System.out.print("i am here " +dx+ ":" +dy+ "\t1\n" );
table1[dx][dy] = 9;
dy = dy +1;
continue ;
}
else if ((table1[dx][dy+1] == 0 ) && (table1[dx+1][dy] == 0))
{
System.out.print("i am here goint to call Back track with "+dx+ ":"+dy+":");
int roww = backtrack(dx ,dy , table1 , row , colum);
dx = roww;
continue;

}

}
}



}
public static int backtrack(int r , int t , int[][] arr , int row , int colum )
{
System.out.print("I am at back tecrk \n");
for( int u = 0 ; u<(row *colum) ; ++u)
{
if(arr[r-1][t +1] == 0)
{
arr[r][t] = 1;
r =r-1;
System.out.print("i am here Back track " +r+ ":" +t+ "\t1\n" );
continue;

}
else if(arr[r-1][t +1] == 1)
{
System.out.print("i am here Back track " +r+ ":" +t+ "\t1\n" );
r = r -1;

break ;
}
}
return r;
}

}

No comments: