九宫重排

/*
标题:九宫重排

    如图1的九宫格中,放着 1~8 的数字卡片,还有一个格子空着。与空格子相邻的格子中的卡片可以移动到空格中。经过若干次移动,可以形成图2所示的局面。

    我们把图1的局面记为:12345678.
    把图2的局面记为:123.46758

    显然是按从上到下,从左到右的顺序记录数字,空格记为句点。

    本题目的任务是已知九宫的初态和终态,求最少经过多少步的移动可以到达。如果无论多少步都无法到达,则输出-1。

例如:
输入数据为:
12345678.
123.46758
则,程序应该输出:
3

再如:
输入:
13524678.
46758123.
则,程序输出:
22


资源约定:
峰值内存消耗(含虚拟机) < 64M
CPU消耗  < 2000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
*/

自己没有做出来,借鉴他人的

1、Set集合在这里的使用查重的使用

2、方向还可以这样使用:dir = {{1,3,-1,-1},{0,2,4,-1},{1,5,-1,-1},{0,6,4,-1},{1,3,5,7},{2,4,8,-1},{3,7,-1,-1,},{4,6,8,-1},{5,7,-1,-1}};

dir 中的九个一维数组分别代表九宫格中的九个位置,每个一维数组表示的是在满足边界条件与题目要求的情况下,‘.’从这个位置能到达的其他的位置。

这样就不用记录整张图,一个String就可以解决了 

import java.util.*;

public class Main{
    static int[][] dir = {{1,3,-1,-1},{0,2,4,-1},{1,5,-1,-1},{0,6,4,-1},{1,3,5,7},{2,4,8,-1},{3,7,-1,-1,},{4,6,8,-1},{5,7,-1,-1}};
    static String start,end;
    static Queue<node> queue;
    static Set<String> set;
    static class node{
        String string;
        int step;
        public node(String string,int step) {
            this.string = string;
            this.step = step;
        }
    }
    static String swap(String string,int k) {
        
        StringBuilder stringBuilder = new StringBuilder(string);
        char temp = string.charAt(k);
        stringBuilder.setCharAt(k, '.');
        stringBuilder.setCharAt(string.indexOf('.'), temp);
        return stringBuilder.toString();
        
    }
    static int bfs() {
        queue = new LinkedList<node>();
        set = new HashSet<String>();
        
        queue.add(new node(start, 0));
        while(!queue.isEmpty()) {
            node temp = queue.poll();
            String s = temp.string;
            int k = s.indexOf('.');
            for(int i=0;i<4;i++) {
                if(dir[k][i]==-1) break;
                else {
                    String string = swap(s,dir[k][i]);
                    if(set.contains(string)) continue;
                    if(string.equals(end)) return temp.step+1;
                    set.add(string);
                    queue.add(new node(string, temp.step+1));
                    
                }
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        start = scanner.next();
        end = scanner.next();
        System.out.println(bfs());
        
    }
}

我的错误代码:待解决中。。。。

import java.util.*;
public class Main{
    static class node{
        int x;
        int y;
        char[][] maze;
        boolean[][] vis;
        int step;
        public node(int x,int y,char[][] maze,boolean[][] vis,int step) {
            this.x = x;
            this.y = y;
            this.maze = maze;
            this.vis = vis;
            this.step = step;
        }
    }
    static int[][] dir = {{1,0},{-1,0},{0,1},{0,-1}};
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[][] smaze = new char[3][3];
        boolean[][] svis = new boolean[3][3];
        
        
        String start = scanner.next();
        String end = scanner.next();
        
        int index = 0,sx = 0,sy = 0,cnt = 0;
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++) {
                smaze[i][j] = start.charAt(index++);
                if(smaze[i][j]=='.') {sx = i; sy = j;} 
            }
                
//        for(int i=0;i<3;i++)
//            System.out.println(smaze[i]);
//        System.out.println(sx+" "+sy);
        Queue<node> queue = new LinkedList<node>();
        svis[sx][sy] = true;
        queue.add(new node(sx,sy,smaze,svis,0));
        
        while(!queue.isEmpty()) {
            node temp = queue.poll();
            for(int i=0;i<4;i++) {
                int xx = temp.x + dir[i][0];
                int yy = temp.y + dir[i][1];
                if(xx<0||xx>=3||yy<0||yy>=3||temp.vis[xx][yy]) continue;
                temp.vis[xx][yy] = true;
                char change = temp.maze[temp.x][temp.y];temp.maze[temp.x][temp.y] = temp.maze[xx][yy];temp.maze[xx][yy] = change;
                
                String string ="";
                for(int r=0;r<3;i++)
                    for(int c=0;c<3;c++)
                        string += temp.maze[r][c];
                
                System.out.println(string);
                if(string.equals(end)) {
                    cnt = temp.step+1;
                    break;
                }
                System.out.println("A");
                queue.add(new node(xx, yy, temp.maze, temp.vis, (temp.step+1)));
                change = temp.maze[temp.x][temp.y];temp.maze[temp.x][temp.y] = temp.maze[xx][yy];temp.maze[xx][yy] = change;
                temp.vis[xx][yy] = false;
            }
            
        }
        
        System.out.println(cnt);
        
    }
}
原文地址:https://www.cnblogs.com/Lemon1234/p/10664028.html