-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChess.java
121 lines (98 loc) · 2.78 KB
/
Chess.java
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
import java.io.*;
import java.util.*;
public class Chess {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter printer = new PrintWriter(new BufferedOutputStream(System.out));
int iterations = sc.nextInt();
int[][] board = new int[8][8];
for (int i = 0; i < 8; i += 2)
{
for (int j = 0; j < 8; j += 2)
{
board[i][j] = 1;
board[i][j + 1] = 2;
board[i + 1][j] = 2;
board[i + 1][j + 1] = 1;
}
}
while (iterations-- > 0)
{
int startCol = (int) sc.next().charAt(0) - 65;
int startRow = 8 - sc.nextInt();
int endCol = (int) sc.next().charAt(0) - 65;
int endRow = 8 - sc.nextInt();
if (board[startRow][startCol] != board[endRow][endCol])
{
printer.println("Impossible");
} else {
if (startRow == endRow && startCol == endCol)
{
printer.println("0 " + (char) (startCol + 65) + " " + (8 - startRow));
} else {
int[][] board2 = new int[8][8];
TrackMoves(board2, startRow, startCol);
if (board2[endRow][endCol] == 1)
{
printer.println("1 " + (char) (startCol + 65) + " " + (8 - startRow) + " " + (char) (endCol + 65)
+ " " + (8 - endRow));
} else {
TrackMoves(board2, endRow, endCol);
int row = -1, col = -1;
for (int i = 0; i < 8; i++){
for (int j = 0; j < 8; j++){
if (board2[i][j] == 2) {
row = i;
col = j;
break;
}
}
}
printer.println("2 " + (char) (startCol + 65) + " " + (8 - startRow) + " " + (char) (col + 65) + " "
+ (8 - row) + " " + (char) (endCol + 65) + " " + (8 - endRow));
}
}
}
}
printer.close();
}
static void TrackMoves(int[][] board, int rowPos, int colPos)
{
int row = rowPos, col = colPos;
while (true) {
row = row - 1;
col = col + 1;
if (row == -1 || col == 8)
break;
board[row][col]++;
}
row = rowPos;
col = colPos;
while (row >= 0 && row < 8 && col >= 0 && col < 8) {
row = row + 1;
col = col + 1;
if (row == 8 || col == 8)
break;
board[row][col]++;
}
row = rowPos;
col = colPos;
while (row >= 0 && row < 8 && col >= 0 && col < 8) {
row = row - 1;
col = col - 1;
if (row == -1 || col == -1)
break;
board[row][col]++;
}
row = rowPos;
col = colPos;
while (row >= 0 && row < 8 && col >= 0 && col < 8)
{
row = row + 1;
col = col - 1;
if (row == 8 || col == -1)
break;
board[row][col]++;
}
}
}