princeton algorithms part 1 第四周作业题

//solver.java的debug未完成

另外以后我将把我的project代码都放在GitHub上了:https://github.com/wuwentao1998

//Board.java
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
import java.util.Stack;
import java.lang.Math;

public class Board {
        private char[][] blocks;
        private int dimension;

        public Board(int[][] blocks) {
            this.dimension = blocks.length;
            this.blocks = new char[dimension][dimension];

            for (int i = 0;i<dimension;i++) {
                for (int j = 0;j<dimension;j++) {
                    this.blocks[i][j] =(char) blocks[i][j];
                }
            }
        }           // construct a board from an n-by-n array of blocks
        // (where blocks[i][j] = block in row i, column j)

        public int dimension() {
            return dimension;
        }                 // board dimension n

        public int hamming() {
            int num =0;
            for (int i = 0;i<dimension;i++) {
                for (int j =0;j<dimension;j++) {
                    if ( i +j != dimension*2 -1 && blocks[i][j] != i * dimension + j+1 ) {
                        num++;
                    }
                }
            }
            return num;
        }                   // number of blocks out of place

        public int manhattan() {
            int distance = 0;
            for (int i = 0;i<dimension;i++) {
                for (int j = 0;j<dimension;j++) {
                    if (blocks[i][j] != 0) {
                        int col = (blocks[i][j] - 1) % dimension;
                        int row = (blocks[i][j] - col - 1) / dimension;
                        distance += (((col > j) ? (col - j) : (j - col)) +
                                ((row > i) ? (row - i) : (i - row)));
                    }
                }
            }
            return distance;
        }                 // sum of Manhattan distances between blocks and goal

        public boolean isGoal() {
            return this.hamming() ==0;
        }                // is this board the goal board?

        public Board twin() {
            int[][] twinBoard = new int[dimension][dimension];
            for (int i=0; i<dimension; i++) {
                for (int j = 0; j < dimension; j++) {
                    twinBoard[i][j] = blocks[i][j];
                }
            }
                if (twinBoard[0][0] != 0 && twinBoard[0][1] != 0){
                    exch(twinBoard,0,0,0,1);
                }else{
                    exch(twinBoard,1,0,1,1);
                }

            return new Board(twinBoard);
        }                    // a board that is obtained by exchanging any pair of blocks

        public boolean equals(Object y) {
            if (this == y)
                return true;
            if (y == null)
                return false;
            if (this.getClass() != y.getClass())
                return false;
            Board that = (Board) y;
            if(this.dimension != that.dimension)
                return false;
            for (int i=0; i<dimension; i++){
                for (int j=0; j<dimension; j++){
                    if (this.blocks[i][j] !=that.blocks[i][j]){
                        return false;
                    }
                }
            }
            return true;
        }        // does this board equal y?

    public Iterable<Board> neighbors() // all neighboring boards
    {
        int blankRow = 0;
        int blankCol = 0;
        Stack<Board> neighbours = new Stack<Board>();

        int[][] clone = new int[dimension][dimension];

        for (int i = 0; i < dimension; i++) {
            for (int j = 0; j < dimension; j++) {
                clone[i][j] = (int) blocks[i][j];

                if (clone[i][j] == 0) {
                    blankRow = i;
                    blankCol = j;
                }
            }
        }

        if (blankCol != 0) {
            exch(clone, blankRow, blankCol - 1, blankRow, blankCol);
            neighbours.push(new Board(clone));
            exch(clone, blankRow, blankCol - 1, blankRow, blankCol);
        }

        if (blankCol != (dimension - 1)) {
            exch(clone, blankRow, blankCol + 1, blankRow, blankCol);
            neighbours.push(new Board(clone));
            exch(clone, blankRow, blankCol + 1, blankRow, blankCol);
        }

        if (blankRow != 0) {
            exch(clone, blankRow - 1, blankCol, blankRow, blankCol);
            neighbours.push(new Board(clone));
            exch(clone, blankRow - 1, blankCol, blankRow, blankCol);
        }

        if (blankRow != (dimension - 1)) {
            exch(clone, blankRow + 1, blankCol, blankRow, blankCol);
            neighbours.push(new Board(clone));
        }

        return neighbours;
    }

    // 这种exch的写法是错误的,因为是传递的是复制的值
//        private void exch(int a, int b) {
//            int temp = a;
//            a= b;
//            b =temp;
//    }

    private void exch(int[][] array, int i, int j, int a, int b) {
        int temp = array[i][j];
        array[i][j] = array[a][b];
        array[a][b] = temp;
    }

    public String toString() // string representation of this board (in the output format specified below)
    {
        StringBuilder s = new StringBuilder();
        s.append(dimension + "\n");
        for (int i = 0; i < dimension; i++) {
            for (int j = 0; j < dimension; j++) {
                s.append(String.format("%2d ", (int) blocks[i][j]));
            }

            s.append("\n");
        }

        return s.toString();
    }        // string representation of this board (in the output format specified below)

        public static void main(String[] args) {
            // read in the board specified in the filename
            In in = new In(args[0]);
            int n = in.readInt();
            int[][] tiles = new int[n][n];

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    tiles[i][j] = in.readInt();
                }
            }

            // solve the slider puzzle
            Board initial = new Board(tiles);
            StdOut.printf("hamming:%d manhattan:%d \n", initial.hamming(),
                    initial.manhattan());
            StdOut.println("dim:" + initial.dimension());
            StdOut.println(initial.toString());
            StdOut.println("goal:" + initial.isGoal());
            StdOut.println("twin:\n" + initial.twin().toString());

            StdOut.println("neighbours:");

            for (Board s : initial.neighbors()) {
                StdOut.println(s.toString());
            }
        }
        } // unit tests (not graded)
//Solver.java
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.MinPQ;
import edu.princeton.cs.algs4.StdOut;

import java.util.Comparator;
import java.util.Stack;

public class Solver {

    private MinPQ<Node> open = new MinPQ<Node>(new Comparator<Node>() {
        @Override
        public int compare(Node o1, Node o2) {
            if (o1.priority < o2.priority)
                return -1;
            else if (o1.priority > o2.priority)
                return  1;
            else
                return 0;
        }
    });

    int move =-1;
    boolean issolve = false;
    Node currentNode;

    private class Node implements Comparable<Node>{
        int move;
        boolean isTwin;
        Board board;
        Node parent;
        int priority;

        public Node(Board board, Node parent, boolean isTwin, int move) {
            this.board = board;
            this.parent = parent;
            this.isTwin = isTwin;
            this.move = move;
            priority = this.board.manhattan() + this.move;
        }

        public int compareTo(Node that)
        {
            if (this.priority > that.priority) {
                return 1;
            }
            else if (this.priority < that.priority) {
                return -1;
            }
            else
                return 0;
        }
    }


    public Solver(Board initial) {
        if (initial == null)
            throw new NullPointerException();
        currentNode = new Node(initial,null,false,0);
        Node twinNode = new Node(initial.twin(),null,true,0);
       // open.insert(twinNode);
        open.insert(currentNode);


        while (true) {
            Node currentNode = open.delMin();
            if (currentNode.board.isGoal() ) {
                if (!currentNode.isTwin) {
                    issolve = true;
                    break;
                }
                else
                {
                    issolve = false;
                    move = -1;
                    break;
                }
            }
            for (Board neigh : currentNode.board.neighbors()) {
                Node neighNode = new Node(neigh, currentNode, currentNode.isTwin, currentNode.move + 1);
                if (currentNode.parent == null) {
                    open.insert(neighNode);
                }
                else if ( !currentNode.parent.equals(neigh) ) {
                    open.insert(neighNode);
                }

            }

        }
    }           // find a solution to the initial board (using the A* algorithm)

    public boolean isSolvable() {
        return issolve;
    }            // is the initial board solvable?

    public int moves() {
        return move;
    }                     // min number of moves to solve initial board; -1 if unsolvable

    public Iterable<Board> solution() {
        Stack<Board> solution = new Stack<Board>();
        while (currentNode != null) {
            solution.push(currentNode.board);
            currentNode = currentNode.parent;
        }

        return solution;
    }      // sequence of boards in a shortest solution; null if unsolvable

    public static void main(String[] args) {
        // create initial board from file
        In in = new In(args[0]);
        int n = in.readInt();
        int[][] blocks = new int[n][n];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                blocks[i][j] = in.readInt();
        Board initial = new Board(blocks);

        // solve the puzzle
        Solver solver = new Solver(initial);

        // print solution to standard output
        if (!solver.isSolvable())
            StdOut.println("No solution possible");
        else {
            StdOut.println("Minimum number of moves = " + solver.moves());
            for (Board board : solver.solution())
                StdOut.println(board);
        }
    } // solve a slider puzzle (given below)

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《普林斯顿应用数学专家指南》是一本关于应用数学领域的权威参考书。本指南旨在为读者提供关于应用数学的广泛背景知识和深入理解。这本书由普林斯顿大学出版社出版,由一系列专家学者撰写。 《普林斯顿应用数学专家指南》涵盖了广泛的主题,包括基本的数学概念、方法和技巧,以及各个领域的应用,如物理学、化学、工程学和经济学等。这本书除了介绍基本概念和数学方法外,还提供了大量的实例和案例分析,帮助读者理解如何将数学应用于实际问题解决中。 《普林斯顿应用数学专家指南》的内容包括线性代数、微积分、概率论和统计学、偏微分方程、最优化理论等。每个主题都被详细阐述,配有清晰易懂的图表和例题。不仅如此,这本书还提供了额外的参考文献和进一步阅读的建议,帮助读者深入研究感兴趣的主题。 这本书对于应用数学领域的学生、教师和研究人员都是一本非常有价值的参考书。它提供了应用数学领域的全面知识和深入理解,帮助读者在应用数学问题的建模、分析和解决中更加熟练和自信。无论是作为教材、学习资料还是研究参考,这本书都是一个不可或缺的工具。 总之,《普林斯顿应用数学专家指南》是一本内容丰富、系统全面的书籍,适合所有对应用数学感兴趣的读者。无论是初学者还是专业人士,都能从中获得有关应用数学的宝贵知识和启发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值