AcWing 845. 八数码

解题思路

AcWing 845. 八数码_第1张图片

相关代码

import java.util.*;

public class Main {
    static Queue queue = new LinkedList<>();
    static int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, 1, 0, -1 };
    static Map hash = new HashMap<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String start="";
        for (int i = 0; i < 9; i++)
            start=start+scanner.next().charAt(0);
        System.out.println(bfs(start));
        scanner.close();
    }

    public static int bfs(String start) {
        String end = "12345678x";
        hash.put(start, 0);
        queue.add(start);
        while (queue.isEmpty() == false) {
            String temp = queue.poll();
         

你可能感兴趣的:(算法)