aboutsummaryrefslogtreecommitdiff
path: root/challenge-118/paulo-custodio/cpp/ch-2.cpp
blob: e82e6b045fd44c48a88a326efe4358b994dc679b (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Challenge 118

TASK #2 - Adventure of Knight
Submitted by : Cheok - Yin Fung
A knight is restricted to move on an 8�8 chessboard.The knight is denoted by
N and its way of movement is the same as what it is defined in Chess.
* represents an empty square. x represents a square with treasure.

The Knight�s movement is unique.It may move two squares vertically and one
square horizontally, or two squares horizontally and one square vertically
(with both forming the shape of an L).

There are 6 squares with treasures.

Write a script to find the path such that Knight can capture all treasures.
The Knight can start from the top - left square.

  a b c d e f g h
8 N * * * * * * * 8
7 * * * * * * * * 7
6 * * * * x * * * 6
5 * * * * * * * * 5
4 * * x * * * * * 4
3 * x * * * * * * 3
2 x x * * * * * * 2
1 * x * * * * * * 1
  a b c d e f g h

https://en.m.wikipedia.org/wiki/Knight%27s_tour
*/

#include <cassert>
#include <cctype>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define NROWS       8
#define NCOLS       8
#define MAX_MOVES   8

struct Board {
    Board() {
        clear();
    }

    void clear() {
        memset(cells, 0, NROWS);
    }

    bool empty() const {
        for (int i = 0; i < NROWS; i++)
            if (cells[i] != 0)
                return false;
        return true;
    }

    void set(int row, int col) {
        assert(row >= 0 && row < NROWS);
        assert(col >= 0 && col < NCOLS);
        int mask = 1 << col;
        cells[row] |= mask;
    }

    void reset(int row, int col) {
        assert(row >= 0 && row < NROWS);
        assert(col >= 0 && col < NCOLS);
        int mask = 1 << col;
        cells[row] &= ~mask;
    }

    bool peek(int row, int col) const {
        assert(row >= 0 && row < NROWS);
        assert(col >= 0 && col < NCOLS);
        int mask = 1 << col;
        return (cells[row] & mask) == 0 ? false : true;
    }

    char cells[NROWS];
};

struct Game {
    Game() {
        clear();
    }

    void clear() {
        row = col = 0;
        visited.clear();
        treasure.clear();
        path.clear();
    }

    void set_visited(int row, int col) {
        visited.set(row, col);
    }

    bool peek_visited(int row, int col) const {
        return visited.peek(row, col);
    }

    void set_treasure(int row, int col) {
        treasure.set(row, col);
    }

    void clear_treasure(int row, int col) {
        treasure.reset(row, col);
    }

    bool peek_treasure(int row, int col) const {
        return treasure.peek(row, col);
    }

    bool done() const {
        return treasure.empty();
    }

    void move(int row, int col) {
        set_visited(row, col);
        clear_treasure(row, col);
        path += 'a' + col;
        path += to_string(NROWS - row);
        path += " ";
        this->row = row;
        this->col = col;
    }

    friend ostream& operator<<(ostream& os, const Game& game) {
        os << "  a b c d e f g h" << endl;
        for (int row = 0; row < NROWS; row++) {
            os << NROWS - row;
            for (int col = 0; col < NCOLS; col++) {
                if (game.peek_treasure(row, col))
                    os << " x";
                else if (game.row == row && game.col == col)
                    os << " N";
                else if (game.peek_visited(row, col))
                    os << " .";
                else
                    os << " *";
            }
            os << " " << NROWS - row << endl;
        }
        os << "  a b c d e f g h" << endl;
        os << endl << "> " << game.path << endl;
        return os;
    }

    void parse_header() {
        string line;
        if (!getline(cin, line)) {
            cerr << "expected header or footer" << endl;
            exit(EXIT_FAILURE);
        }
        const char* p = line.c_str();
        while (*p && isspace(*p)) p++;
        if (strncmp(p, "a b c d e f g h", 15) != 0) {
            cerr << "invalid header or footer" << endl;
            exit(EXIT_FAILURE);
        }
    }

    void parse_row(int row) {
        string line;
        if (!getline(cin, line)) {
            cerr << "expected row " << NROWS - row << endl;
            exit(EXIT_FAILURE);
        }
        const char* p = line.c_str();
        while (*p && isspace(*p)) p++;
        if (!isdigit(*p) || *p - '0' != NROWS - row) {
            cerr << "invalid row " << NROWS - row << endl;
            exit(EXIT_FAILURE);
        }
        p++;
        for (int col = 0; col < NCOLS; col++) {
            while (*p && isspace(*p)) p++;
            switch (*p) {
                case 'N': move(row, col); break;
                case 'x': set_treasure(row, col); break;
                case '*': break;
                default:
                    cerr << "invalid row " << NROWS - row << endl;
                    exit(EXIT_FAILURE);
            }
            p++;
        }
    }

    void parse() {
        clear();
        parse_header();
        for (int row = 0; row < NROWS; row++)
            parse_row(row);
        parse_header();
    }

    int row{ 0 }, col{ 0 };
    Board visited, treasure;
    string path;
};

struct Move {
    int row, col, num_moves;

    Move(int row = 0, int col = 0, int num_moves = 0)
        : row(row), col(col), num_moves(num_moves) {}
};

void try_move(vector<Move>& moves, Game& game, int nrow, int ncol) {
    if (nrow < 0 || nrow >= NROWS) return;
    if (ncol < 0 || ncol >= NCOLS) return;
    if (game.peek_visited(nrow, ncol)) return;

    moves.push_back(Move(nrow, ncol, 0));
}

void try_num_moves(vector<Move>& moves, Game& game, int row, int col) {
    moves.clear();
    try_move(moves, game, row + 2, col + 1);
    try_move(moves, game, row + 2, col - 1);
    try_move(moves, game, row - 2, col + 1);
    try_move(moves, game, row - 2, col - 1);
    try_move(moves, game, row + 1, col + 2);
    try_move(moves, game, row - 1, col + 2);
    try_move(moves, game, row + 1, col - 2);
    try_move(moves, game, row - 1, col -