summaryrefslogtreecommitdiffstats
path: root/Algorithms/Part-I/4-Puzzle/Board.java
blob: e4d9b11dfa7688a4324c2d4b0d271d46e421cbcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* vim: set expandtab tabstop=4 shiftwidth=4 : */

public class Board {

    private int d;
    private byte[][] blocks;
    private int openRow;
    private int openCol;

    private Board(byte[][] blocks, int openI, int openJ)
    {
        this.d = blocks[0].length;
        this.blocks = blocks;
        this.openRow = openI;
        this.openCol = openJ;
    }

    // construct a board from an N-by-N array of blocks
    // (where blocks[i][j] = block in row i, column j)
    public Board(int[][] blocks)
    {

        this.d = blocks[0].length;
        this.blocks = new byte[d][d];

        for (int i = 0; i < d; i++)
        {
            for (int j = 0; j < d; j++)
            {
                this.blocks[i][j] = (byte) blocks[i][j];
                if (blocks[i][j] == 0)
                {
                    this.openRow = i;
                    this.openCol = j;
                }
            }
        }
    }

    // board dimension N
    public int dimension()
    {
        return d;
    }

    // number of blocks out of place
    public int hamming()
    {
        int k = 1;
        int hamming = 0;

        for (int i = 0; i < d; i++)
            for (int j = 0; j < d; j++, k++)
                if (blocks[i][j] != 0 && blocks[i][j] != k) hamming += 1;

        return hamming;
    }

    // sum of Manhattan distances between blocks and goal
    public int manhattan()
    {
        int manhattan = 0;

        for (int i = 0; i < d; i++)
            for (int j = 0; j < d; j++)
            {
                int v = blocks[i][j] -1;
                if (v == -1) continue;
                manhattan += Math.abs((v / d) - i) + Math.abs((v % d) - j);
            }

        return manhattan;
    }

    // is this board the goal board?
    public boolean isGoal()
    {
        int k = 1;

        if (blocks[d-1][d-1] != 0) return false;

        for (int i = 0; i < d; i++)
            for (int j = 0; j < d; j++)
                if (blocks[i][j] != k++ && !((i == j) && (j == d - 1))) return false;

        return true;
    }

    private byte[][] copy()
    {
        byte[][] bytes = new byte[d][d];

        for (int i = 0; i < d; i++)
            for (int j = 0; j < d; j++)
                bytes[i][j] = blocks[i][j];

        return bytes;
    }

    // a board obtained by exchanging two adjacent blocks in the same row
    public Board twin()
    {
        int row = 0;
        if (openRow == 0) row = 1;

        byte [][]bytes = copy();

        byte tmp = bytes[row][0];
        bytes[row][0] = bytes[row][1];
        bytes[row][1] = tmp;

        return new Board(bytes, openRow, openCol);
    }

    // does this board equal y?
    public boolean equals(Object y)
    {
        if (y == null) return false;
        if (y.getClass() != this.getClass()) return false;
        Board other = (Board) y;

        if (d != other.d) return false;
        for (int i = 0; i < d; i++)
            for (int j = 0; j < d; j++)
                if (blocks[i][j] != other.blocks[i][j]) return false;
        return true;
    }

    private Board move(int rowDelta, int colDelta)
    {
        byte [][]bytes = copy();

        int or = openRow + rowDelta;
        int oc = openCol + colDelta;

        byte tmp = bytes[openRow][openCol];
        bytes[openRow][openCol] = bytes[or][oc];
        bytes[or][oc] = tmp;

        return new Board(bytes, or, oc);
    }

    // all neighboring boards
    public Iterable<Board> neighbors()
    {
        Queue<Board> q = new Queue<Board>();
        if (openRow != 0)
            q.enqueue(move(-1, 0));
        if (openRow < d - 1)
            q.enqueue(move(1, 0));
        if (openCol != 0)
            q.enqueue(move(0, -1));
        if (openCol < d - 1)
            q.enqueue(move(0, 1));

        return q;
    }

    // string representation of the board
    public String toString()
    {
        String out = d+"\n";
        // out += "open      "+openRow+" "+openCol+"\n";
        // out += "goal      "+(isGoal() ? "OK" : "KO")+"\n";
        // out += "hamming   "+hamming()+"\n";
        // out += "manhattan "+manhattan()+"\n";
        for (int i = 0; i < d; i++)
        {
            for (int j = 0; j < d; j++)
                out += " "+(int) blocks[i][j];
            out += "\n";
        }
        return out; //.substring(0, out.length() - 1);
    }
}