Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rat Chases Cheese Question #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Backtracking/Chessboard2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package lect12backtracking;

import java.util.Scanner;

public class Chessboard2 {
static int count =0;
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
boolean[][] arr = new boolean[n][n];
int[] mp = Makemines(n*n);
Chessboard2(arr,0,0,"",mp);
System.out.println();
System.out.println(count);

}
public static void Chessboard2(boolean[][] board, int row, int col, String ans,int[] mp) {
if (row == board.length - 1 && col == board[0].length - 1) {
System.out.print("{0-0}" + ans+" ");
count++;
return;
}
if (row >= board.length || col >= board[0].length || board[row][col])
return;
board[row][col] = true;

int cell = row * (board[0].length) + col + 1;

if (mp[cell] == 1) {
return ;
} else if (mp[cell] == 2) {
Chessboard2(board,board.length-1,board[0].length-1, ans + "P{" + (board.length - 1) + "-" + (board[0].length-1) + "}", mp);
}


Chessboard2(board, row + 2, col + 1, ans + "K" + "{" + (row + 2) + "-" +( col + 1 )+ "}",mp);
Chessboard2(board, row + 1, col + 2, ans + "K" + "{" + (row + 1) + "-" + (col + 2) + "}",mp);
if (row == 0 || col == 0 || col == board[0].length - 1 || row == board.length - 1) {
for(int i =1;i<board.length;i++)
Chessboard2(board, row , col+i, ans + "R" + "{" + (row ) + "-" + (col+i)+ "}",mp);
for(int i =1;i<board.length;i++)
Chessboard2(board, row+i, col , ans + "R" + "{" + (row+i) + "-" + (col ) + "}",mp);}
if (row == col|| row + col == board.length - 1 ) {
for(int i =1;i<board.length;i++)
Chessboard2(board, row + i, col + i, ans + "B" + "{" + (row + i) + "-" + (col + i) + "}",mp);}
board[row][col] = false;
}
public static int[] Makemines(int n)
{ int arr[] = new int[n+1];
int c=1;
for(int i=2;i<arr.length;i++)
{ if(isPrime(i))
{if(c%2==1)
arr[i] = 1;
else
arr[i] = 2;
c++;

}
}
return arr;

}
public static boolean isPrime(int n)
{
for(int i =2;i<n;i++)
{if(n%i==0)
return false;}
return true;
}
}









73 changes: 73 additions & 0 deletions Backtracking/Ratchasesitscheese.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package lect12backtracking;

import java.util.Scanner;

public class Ratchasesitscheese {
static Scanner x = new Scanner(System.in);

public static void main(String[] args) {

char[][] maze = takeInput();
int[][] AuxiMaze = new int[maze.length][maze[0].length];
//int[][] temp = new int[maze.length][maze[0].length];
paths(maze, 0, 0, AuxiMaze);
if (c == 0) {
System.out.println("NO PATH FOUND");
}

}

static int c = 0;

public static void paths(char[][] maze, int row, int col, int[][] Auxi) {
if (row == maze.length - 1 && col == maze[0].length - 1) {
Auxi[row][col] = 1;
//for(int i=0;i<Auxi.length;i++){
//for(int j=0;j<Auxi[0].length;j++){
// temp[i][j]=Auxi[i][j];
//display(temp);

display(Auxi);
c = 1;
return;
}
if (row == maze.length || col == maze[0].length || row < 0 || col < 0 || maze[row][col] == 'X'
|| Auxi[row][col] == 1) {
return;
}

Auxi[row][col] = 1;

paths(maze, row + 1, col, Auxi);

paths(maze, row, col + 1, Auxi);

paths(maze, row - 1, col, Auxi);

paths(maze, row, col - 1, Auxi);
Auxi[row][col] = 0;

}

public static char[][] takeInput() {
int n = x.nextInt();
int m = x.nextInt();
char[][] maze = new char[n][m];
for (int i = 0; i < n; i++) {
String a = x.next();
for (int j = 0; j < m; j++) {
maze[i][j] = a.charAt(j);
}
}
return maze;
}

public static void display(int[][] maze) {
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[0].length; j++) {
System.out.print(maze[i][j] + " ");
}
System.out.println();
}
}
}
73 changes: 73 additions & 0 deletions Ratchasesitscheese.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package lect12backtracking;

import java.util.Scanner;

public class Ratchasesitscheese {
static Scanner x = new Scanner(System.in);

public static void main(String[] args) {

char[][] maze = takeInput();
int[][] AuxiMaze = new int[maze.length][maze[0].length];
//int[][] temp = new int[maze.length][maze[0].length];
paths(maze, 0, 0, AuxiMaze);
if (c == 0) {
System.out.println("NO PATH FOUND");
}

}

static int c = 0;

public static void paths(char[][] maze, int row, int col, int[][] Auxi) {
if (row == maze.length - 1 && col == maze[0].length - 1) {
Auxi[row][col] = 1;
//for(int i=0;i<Auxi.length;i++){
//for(int j=0;j<Auxi[0].length;j++){
// temp[i][j]=Auxi[i][j];
//display(temp);

display(Auxi);
c = 1;
return;
}
if (row == maze.length || col == maze[0].length || row < 0 || col < 0 || maze[row][col] == 'X'
|| Auxi[row][col] == 1) {
return;
}

Auxi[row][col] = 1;

paths(maze, row + 1, col, Auxi);

paths(maze, row, col + 1, Auxi);

paths(maze, row - 1, col, Auxi);

paths(maze, row, col - 1, Auxi);
Auxi[row][col] = 0;

}

public static char[][] takeInput() {
int n = x.nextInt();
int m = x.nextInt();
char[][] maze = new char[n][m];
for (int i = 0; i < n; i++) {
String a = x.next();
for (int j = 0; j < m; j++) {
maze[i][j] = a.charAt(j);
}
}
return maze;
}

public static void display(int[][] maze) {
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[0].length; j++) {
System.out.print(maze[i][j] + " ");
}
System.out.println();
}
}
}