0% found this document useful (0 votes)
107 views7 pages

8 Puzzle DFS

This document describes an algorithm to solve the 8 puzzle problem using depth-first search (DFS). It takes in a 3x3 puzzle board as input, represented as a 2D array. It uses DFS recursion to explore all possible board configurations by swapping the empty space tile with its neighbors, until it reaches the goal configuration. It tracks explored configurations in a HashSet to avoid duplicates. When the goal is reached, it prints the solution board.

Uploaded by

Meera Suthar
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)
107 views7 pages

8 Puzzle DFS

This document describes an algorithm to solve the 8 puzzle problem using depth-first search (DFS). It takes in a 3x3 puzzle board as input, represented as a 2D array. It uses DFS recursion to explore all possible board configurations by swapping the empty space tile with its neighbors, until it reaches the goal configuration. It tracks explored configurations in a HashSet to avoid duplicates. When the goal is reached, it prints the solution board.

Uploaded by

Meera Suthar
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/ 7

8 Puzzle(DFS)

package interview;

import java.util.*;

/**

* @author Meera Suthar

*/

public class EightPuzzleDFS {

static int fin[][];

static boolean flag;

static Set<Integer> set;

public static void main(String args[]){

flag=false;

Scanner scan=new Scanner(System.in);

set=new HashSet<>();

int[][] puz=new int[3][3];

fin=new int[][]{{1,2,3},{4,5,6},{7,8,0}};

int a=0,b=0;

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){

puz[i][j]=scan.nextInt();

if(puz[i][j]==0){

a=i;b=j;

dfs(a,b,puz);

public static boolean issame(int[][] puz){

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

if(fin[i][j]!=puz[i][j]){

return false;

return true;

public static int arrtoint(int[][] puz){


int s=0;

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

s*=10;

s+=puz[i][j];

return s;

public static void print(int[][] puz){

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

System.out.print(puz[i][j]+" ");

System.out.println();

System.out.println();

System.out.println();

public static void dfs(int cx,int cy,int[][] puz){


if(issame(puz)){

System.out.println("Answer:");

print(puz);

flag=true;

return;

int a=arrtoint(puz);

if(!set.contains(a)&&!flag){

set.add(a);

print(puz);

if(cx-1>=0&&puz[cx][cy]==0){

int temp=puz[cx][cy];

puz[cx][cy]=puz[cx-1][cy];

puz[cx-1][cy]=temp;

dfs(cx-1,cy,puz);

int temp2=puz[cx][cy];

puz[cx][cy]=puz[cx-1][cy];

puz[cx-1][cy]=temp2;

}
if(cx+1<3&&puz[cx][cy]==0){

int temp=puz[cx][cy];

puz[cx][cy]=puz[cx+1][cy];

puz[cx+1][cy]=temp;

dfs(cx+1,cy,puz);

int temp2=puz[cx][cy];

puz[cx][cy]=puz[cx+1][cy];

puz[cx+1][cy]=temp2;

if(cy-1>=0&&puz[cx][cy]==0){

int temp=puz[cx][cy];

puz[cx][cy]=puz[cx][cy-1];

puz[cx][cy-1]=temp;

dfs(cx,cy-1,puz);

int temp2=puz[cx][cy];

puz[cx][cy]=puz[cx][cy-1];

puz[cx][cy-1]=temp2;

if(cy+1<3&&puz[cx][cy]==0){
int temp=puz[cx][cy];

puz[cx][cy]=puz[cx][cy+1];

puz[cx][cy+1]=temp;

dfs(cx,cy+1,puz);

int temp2=puz[cx][cy];

puz[cx][cy]=puz[cx][cy+1];

puz[cx][cy+1]=temp2;

// set.remove(set.size()-1);

I/P:

1 0 3

4 2 5

7 8 6
O/P:

Answer:

123

456

780

BUILD SUCCESSFUL (total time: 24 seconds)

You might also like