在队列中存储2D数组

时间:2019-05-15 10:35:22

标签: java multidimensional-array queue

我想将2D数组存储为Queue的元素。对它进行一些操作之后。

喜欢8个谜题

这是我的目标状态,初始状态来自文件

最终静态int [] []目标=新int [] [] {{3,2,1},{5,6,4},{7,9,8}}; 公共静态队列queue = new LinkedList();

    public void ARotateRight(int [][]parent){
        int swap=parent[0][0];    
        parent[0][0]=parent[1][0];
        parent[1][0]=parent[1][1];
        parent[1][1]=parent[0][1];
        parent[0][1]=swap;

        queue.add(parent);//add element in array
    }


   public boolean DFS(){        
        while(!queue.isEmpty()){

            int[][] queueblock = queue.remove();

   //printing array 
            for(int i=0 ;i<3;i++){
                for(int j=0 ;j<3;j++){
                    System.out.print(queueblock [i][j]);
                }
                System.out.println();
            }           

            if(isGoal(queueblock)){
                return true;
            } 

            ARotateRight(queueblock);
            ARotateLeft(queueblock);
            BRotateRight(queueblock);
            BRotateLeft(queueblock);
            CRotateRight(queueblock);
            CRotateLeft(queueblock);
            DRotateRight(queueblock);
            DRotateLeft(queueblock);
        }
        return false;
    }
    //main function.
    public static void main(String[] args) throws FileNotFoundException {

        DFSTask dfsTask=new DFSTask(); //DFSTask is my class name

        File file=new File("D://newfile.txt");
        Scanner input=new Scanner(file);        
        int [][] block=new int[3][3]; 

        for(int i=0 ;i<3;i++){
            for(int j=0 ;j<3;j++){
                block [i][j]=Integer.parseInt(input.next());
            }   
        }

        queue.add(block);             //adding initial state in Queue
        boolean check=dfsTask.DFS();
        System.out.println(check);
    }
    }

打印后所有队列元素都是相同的。 我希望所有元素都应该有所不同

0 个答案:

没有答案