85 lines
1.9 KiB
C
85 lines
1.9 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <assert.h>
|
||
|
|
||
|
#include "chess_paths.h"
|
||
|
|
||
|
struct chessboard {
|
||
|
unsigned int C;
|
||
|
unsigned int R;
|
||
|
int board[];
|
||
|
};
|
||
|
|
||
|
int value_at(const struct chessboard * this, unsigned column, unsigned row) {
|
||
|
row = this->R - row - 1;
|
||
|
return this->board[row*this->C + column];
|
||
|
}
|
||
|
|
||
|
unsigned int columns(const struct chessboard * this) {
|
||
|
return this->C;
|
||
|
}
|
||
|
|
||
|
unsigned int rows(const struct chessboard * this) {
|
||
|
return this->R;
|
||
|
}
|
||
|
|
||
|
struct chessboard * new_board(unsigned int cols, unsigned int rows) {
|
||
|
struct chessboard * this = malloc(sizeof(struct chessboard) + sizeof(int)*cols*rows);
|
||
|
if (this) {
|
||
|
this->C = cols;
|
||
|
this->R = rows;
|
||
|
}
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
int read_board(struct chessboard * this, FILE * input) {
|
||
|
for (unsigned int i = 0; i < this->C * this->R; ++i)
|
||
|
if (fscanf(input, "%d", this->board + i) != 1)
|
||
|
return 0;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
enum chess_piece char_to_piece(char piece) {
|
||
|
switch (piece) {
|
||
|
case 'P':
|
||
|
case 'p': return PAWN;
|
||
|
case 'N':
|
||
|
case 'n': return KNIGHT;
|
||
|
case 'B':
|
||
|
case 'b': return BISHOP;
|
||
|
case 'R':
|
||
|
case 'r': return ROOK;
|
||
|
case 'Q':
|
||
|
case 'q': return QUEEN;
|
||
|
case 'K':
|
||
|
case 'k': return KING;
|
||
|
default: return PAWN;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
unsigned int r, c;
|
||
|
assert (scanf("%u%u", &c, &r) == 2);
|
||
|
struct chessboard * board = new_board (c, r);
|
||
|
assert (board);
|
||
|
assert (read_board(board, stdin));
|
||
|
char piece[2];
|
||
|
char path_type[2];
|
||
|
struct piece_position p;
|
||
|
while (scanf("%1s%1s%u%u", path_type, piece, &c, &r) == 4) {
|
||
|
unsigned int len;
|
||
|
p.column = c;
|
||
|
p.row = r;
|
||
|
p.piece = char_to_piece(*piece);
|
||
|
switch (*path_type) {
|
||
|
case 'i':
|
||
|
case 'I': len = increasing_path_len(board, &p); break;
|
||
|
case 'd':
|
||
|
case 'D': len = decreasing_path_len(board, &p); break;
|
||
|
case 's':
|
||
|
case 'S': len = simple_path_len(board, &p); break;
|
||
|
}
|
||
|
printf("%s (%u,%u) --> (%u,%u) in %u steps\n", piece, c, r, p.column, p.row, len);
|
||
|
}
|
||
|
}
|