Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views4 pages

Rat in Maze

The document contains a Java program that implements solutions for two problems: the Rat in a Maze and placing N Knights on a chessboard. It includes methods for navigating the maze and checking safe placements for knights on the board. The program features recursive functions to explore paths and validate moves, along with methods to display the board state.

Uploaded by

rahulsuthrapu616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Rat in Maze

The document contains a Java program that implements solutions for two problems: the Rat in a Maze and placing N Knights on a chessboard. It includes methods for navigating the maze and checking safe placements for knights on the board. The program features recursive functions to explore paths and validate moves, along with methods to display the board state.

Uploaded by

rahulsuthrapu616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

public class RatInMaze {

public static void main(String[] args) {


// int grid[][]={
// {1,0,0,0},
// {1,1,0,1},
// {0,1,0,0},
// {1,1,1,1},
// };
// int path[][]=new int [grid.length][grid[0].length];
// directions(grid, 0, 0, "",path);

int n=4;
boolean board[][]=new boolean [n][n];
nknights(board, 0, 0, n);
// display(board);

public static void printgrid(int grid[][]){


for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
System.out.print(grid[i][j]);
}
System.out.println();
}
}
public static void directions(int grid[][],int row,int col,String
p,int path[][]){
//base
if( row==grid.length-1 && col==grid[0].length-1){
path[row][row]=1;
System.out.println(p);
printgrid(path);
return;
}
//recursion
if(grid[row][col]==0){
return;
}
grid[row][col]=0;
path[row][col]=1;

if(row>0){
directions(grid, row-1, col, p+"U",path);
}
if(row<grid.length-1){
directions(grid, row+1, col, p+"D",path);
}
if(col>0){
directions(grid, row, col-1, p+"L",path);
}
if(col<grid[0].length-1){
directions(grid, row, col+1, p+"R",path);
}
grid[row][col]=1;
path[row][col]=0;
}

/**** KNIGHTS ****/

public static void nknights(boolean board[][],int row, int col,int


knights){
//base
if(knights==0){
display(board);
System.out.println();
return ;
}

if(row==board.length-1 && col==board.length){


return ;
}
if(col==board.length){
nknights(board, row+1,0, knights);
return;
}

if(issafe(board,row,col)){
board[row][col]=true;
nknights(board, row, col+1, knights-1);
board[row][col]=false;
}
nknights(board, row, col+1, knights);

static boolean issafe(boolean board[][],int row,int col){


if(isValid(board, row-2, col-1)){
if(board[row-2][col-1]){
return false;
}
}

if(isValid(board, row-2, col+1)){


if(board[row-2][col+1]){
return false;
}
}
if(isValid(board, row-1, col+2)){
if(board[row-1][col+2]){
return false;
}
}
if(isValid(board, row-1, col-2)){
if(board[row-1][col-2]){
return false;
}
}
return true;
}

static boolean isValid(boolean board[][],int row,int col){


if(row>=0 &&row<board.length&&col>=0&&col<board[0].length){
return true;
}
return false;
}

static void display(boolean [][]board){


char bod[][]={
{'O','O','O','O'},
{'O','O','O','O'},
{'O','O','O','O'},
{'O','O','O','O'}

};
for(int i=0;i<board.length;i++){
for(int j=0;j<board.length;j++){
if(board[i][j]){
bod[i][j]='k';
}
else{
bod[i][j]='0';
}
}
System.out.println();
}
printboard(bod);
}

public static void printboard(char board[][]){


System.out.println("------------");
for(int i=0;i<board.length;i++){
for(int j=0;j<board.length;j++){
System.out.print(board[i][j]+" ");
}
System.out.println();
}
}
}

You might also like