From 5a29a9bd63445f9e472f8d90b48af5772a23aa50 Mon Sep 17 00:00:00 2001 From: Megha1209 <51127835+Megha1209@users.noreply.github.com> Date: Mon, 14 Oct 2019 11:59:28 +0530 Subject: [PATCH 1/3] Rat Chases Cheese Question --- Ratchasesitscheese.java | 73 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Ratchasesitscheese.java diff --git a/Ratchasesitscheese.java b/Ratchasesitscheese.java new file mode 100644 index 0000000..9f2866a --- /dev/null +++ b/Ratchasesitscheese.java @@ -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 Date: Mon, 14 Oct 2019 22:58:00 +0530 Subject: [PATCH 2/3] Add files via upload --- Backtracking/Ratchasesitscheese.java | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Backtracking/Ratchasesitscheese.java diff --git a/Backtracking/Ratchasesitscheese.java b/Backtracking/Ratchasesitscheese.java new file mode 100644 index 0000000..9f2866a --- /dev/null +++ b/Backtracking/Ratchasesitscheese.java @@ -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 Date: Mon, 14 Oct 2019 22:59:56 +0530 Subject: [PATCH 3/3] ChessBoard Problem --- Backtracking/Chessboard2.java | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Backtracking/Chessboard2.java diff --git a/Backtracking/Chessboard2.java b/Backtracking/Chessboard2.java new file mode 100644 index 0000000..0718405 --- /dev/null +++ b/Backtracking/Chessboard2.java @@ -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