/**
* Find the way through the maze, also find the key
* @author Prateek
*/
class MazeWithKey {
static int N;
static int size;
static boolean isKeyFound=false;
static int[][] solution;
private static boolean solveMaze(int maze[][] , int solution[][] , int row , int col){
// exit gate reached
if (row == size && col == size) {
if(isKeyFound){
solution[row][col] = 1;
System.
out.
println("Path Marix is :"); printSolution(solution);
System.
out.
println("==============================="); return true;
}
}
// Move Down
if (row < size)
{
if (maze[row + 1][col] == 1 || maze[row + 1][col] == 2)
{
if(solution[row + 1][col] != 1){ //to avoid re-selecting the same cell
if(maze[row + 1][col] == 2){
isKeyFound=true;
}
solution[row][col] = 1;
solveMaze(maze, solution, row + 1, col);
solution[row][col] = 0;
if(maze[row + 1][col] == 2){
isKeyFound=false;
}
}
}
}
// Move Right
if (col < size)
{
if (maze[row][col + 1] == 1 || maze[row][col + 1] == 2)
{
if(solution[row][col + 1] != 1){
if(maze[row][col + 1] == 2){
isKeyFound=true;
}
solution[row][col] = 1;
solveMaze(maze, solution, row, col + 1);
solution[row][col] = 0;
if(maze[row][col + 1] == 2){
isKeyFound=false;
}
}
}
}
// Move Up
if (row > 0)
{
if (maze[row - 1][col] == 1 || maze[row - 1][col] == 2)
{
if(solution[row - 1][col] != 1){
if(maze[row - 1][col] == 2){
isKeyFound=true;
}
solution[row][col] = 1;
solveMaze(maze, solution, row - 1, col);
solution[row][col] = 0;
if(maze[row - 1][col] == 2){
isKeyFound=false;
}
}
}
}
// Move Left
if (col > 0)
{
if (maze[row][col -1] == 1 || maze[row][col -1] == 2)
{
if(solution[row][col -1] != 1){
if(maze[row][col -1] == 2){
isKeyFound=true;
}
solution[row][col] = 1;
solveMaze(maze, solution, row, col -1);
solution[row][col] = 0;
if(maze[row][col -1] == 2){
isKeyFound=false;
}
}
}
}
//Move NW
if (row > 0 && col > 0 )
{
if (maze[row - 1][col - 1] == 1 || maze[row - 1][col - 1] == 2)
{
if(solution[row - 1][col - 1] != 1){
if(maze[row - 1][col - 1] == 2){
isKeyFound=true;
}
solution[row][col] = 1;
solveMaze(maze, solution, row - 1, col - 1);
solution[row][col] = 0;
if(maze[row - 1][col - 1] == 2){
isKeyFound=false;
}
}
}
}
//Move NE
if (row > 0 && col < size )
{
if (maze[row - 1][col + 1] == 1 || maze[row - 1][col + 1] == 2)
{
if(solution[row - 1][col + 1] != 1){
if(maze[row - 1][col + 1] == 2){
isKeyFound=true;
}
solution[row][col] = 1;
solveMaze(maze, solution, row - 1, col + 1);
solution[row][col] = 0;
if(maze[row - 1][col + 1] == 2){
isKeyFound=false;
}
}
}
}
//Move SE
if (row < size && col < size )
{
if (maze[row + 1][col + 1] == 1 || maze[row + 1][col + 1] == 2)
{
if(solution[row + 1][col + 1] != 1){
if(maze[row + 1][col + 1] == 2){
isKeyFound=true;
}
solution[row][col] = 1;
solveMaze(maze, solution, row + 1, col + 1);
solution[row][col] = 0;
if(maze[row + 1][col + 1] == 2){
isKeyFound=false;
}
}
}
}
//Move SW
if (row < size && col > 0 )
{
if (maze[row + 1][col - 1] == 1 || maze[row + 1][col - 1] == 2)
{
if(solution[row + 1][col - 1] != 1){
if( maze[row + 1][col - 1] == 2){
isKeyFound=true;
}
solution[row][col] = 1;
solveMaze(maze, solution, row + 1, col - 1);
solution[row][col] = 0;
if( maze[row + 1][col - 1] == 2){
isKeyFound=false;
}
}
}
}
return false;
}
private static void printSolution(int[][] solution) {
int rLen = solution.length;
int cLen = solution[0].length;
for (int i = 0; i < rLen; i++)
{
for (int j = 0; j < cLen; j++)
{
System.
out.
print(solution
[i
][j
] + "\t"); }
}
}
public static void main
(String[] args
) { MazeWithKey mazeObj=new MazeWithKey();
mazeObj.init();
}
private void init(){
int[][] maze = {
{1, 1, 0, 0, 1, 1 },
{1, 0, 1, 1, 0, 0 },
{1, 0, 0, 0, 1, 0 },
{1, 1, 1, 2, 0, 0 },
{1, 0, 0, 0, 0, 0 },
{1, 1, 1, 1, 1, 1 }
};
size = maze.length - 1;
int[][] solution= new int[maze.length][maze[0].length];
System.
out.
println("Input Matrix is "); printSolution(maze);
System.
out.
println("==========================="); solveMaze(maze,solution, 0 ,0 );
}
}
LyoqCiAqIEZpbmQgdGhlIHdheSB0aHJvdWdoIHRoZSBtYXplLCBhbHNvIGZpbmQgdGhlIGtleQogKiBAYXV0aG9yIFByYXRlZWsKICovCmNsYXNzIE1hemVXaXRoS2V5IHsKCXN0YXRpYyBpbnQgTjsKCXN0YXRpYyBpbnQgc2l6ZTsKCXN0YXRpYyBib29sZWFuIGlzS2V5Rm91bmQ9ZmFsc2U7CglzdGF0aWMgaW50W11bXSBzb2x1dGlvbjsKCglwcml2YXRlIHN0YXRpYyBib29sZWFuIHNvbHZlTWF6ZShpbnQgbWF6ZVtdW10gLCBpbnQgc29sdXRpb25bXVtdICwgaW50IHJvdyAsIGludCBjb2wpewoKCQkvLyBleGl0IGdhdGUgcmVhY2hlZAoJCWlmIChyb3cgPT0gc2l6ZSAmJiBjb2wgPT0gc2l6ZSkgewoJCQlpZihpc0tleUZvdW5kKXsKCQkJCXNvbHV0aW9uW3Jvd11bY29sXSA9IDE7CgkJCQlTeXN0ZW0ub3V0LnByaW50bG4oIlBhdGggTWFyaXggaXMgOiIpOwoJCQkJcHJpbnRTb2x1dGlvbihzb2x1dGlvbik7CgkJCQlTeXN0ZW0ub3V0LnByaW50bG4oIj09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0iKTsKCQkJCXJldHVybiB0cnVlOwoJCQl9CgkJfQoKCQkvLyBNb3ZlIERvd24KCQlpZiAocm93IDwgc2l6ZSkKCQl7CgkJCWlmIChtYXplW3JvdyArIDFdW2NvbF0gPT0gMSB8fCBtYXplW3JvdyArIDFdW2NvbF0gPT0gMikgCgkJCXsKCQkJCWlmKHNvbHV0aW9uW3JvdyArIDFdW2NvbF0gIT0gMSl7IC8vdG8gYXZvaWQgcmUtc2VsZWN0aW5nIHRoZSBzYW1lIGNlbGwKCgkJCQkJaWYobWF6ZVtyb3cgKyAxXVtjb2xdID09IDIpewoJCQkJCQlpc0tleUZvdW5kPXRydW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