-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayingChess688.cpp
164 lines (157 loc) · 2.76 KB
/
PlayingChess688.cpp
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
#include <iostream>
#include <string>
#include <queue>
using namespace std;
char arr[8][8];
struct index {
int row;
int col;
};
queue <index>q;
int check(int row, int col) {
if (row >= 0 && row <= 7 && col >= 0 && col <= 7 && (arr[row][col] == '-'|| arr[row][col] == '@')) {
index in;
in.row = row;
in.col = col;
q.push(in);
if (arr[row][col] == '@')
return 1;
arr[row][col] = '*';
return 0;
}
return 0;
}
int main() {
int t = 0;
string input;
cin >> t;
for (int i = 1; i <= t; i++) {
index start;
index end;
q = queue <index>();
for (int j = 0; j < 8; j++) {
getline(cin, input);
if(input[0]=='\0'|| input[0] == '\n')
getline(cin, input);
for (int k = 0; k < 8; k++) {
arr[j][k] = input[k];
if (input[k] == '#') {
start.row = j;
start.col = k;
}
else if (input[k] == '@') {
end.row = j;
end.col = k;
}
}
}
int ctr = 0;
q.push(start);
bool flag = true;
bool flag1 = true;
arr[start.row][start.col] = '0';
int count = q.size();
while (!q.empty()) {
if (--count == 0) {//ignore the root
if (flag == true) {
count++;
flag = false;
}
else {
ctr++;
count = q.size();
}
}
index temp = q.front();
q.pop();
if (temp.row == end.row && temp.col == end.col) {
flag1 = false;
break;
}
int row;
int col;
int flag = 0;
row = temp.row - 2;
col = temp.col - 1;
flag = check(row, col);
if (flag == 1) {
ctr++;
flag1 = false;
break;
}
row = temp.row - 1;
col = temp.col - 2;
check(row, col);
flag = check(row, col);
if (flag == 1) {
ctr++;
flag1 = false;
break;
}
row = temp.row + 2;
col = temp.col + 1;
check(row, col);
flag = check(row, col);
if (flag == 1) {
ctr++;
flag1 = false;
break;
}
row = temp.row + 1;
col = temp.col + 2;
check(row, col);
flag = check(row, col);
if (flag == 1) {
ctr++;
flag1 = false;
break;
}
row = temp.row + 2;
col = temp.col - 1;
check(row, col);
flag = check(row, col);
if (flag == 1) {
ctr++;
flag1 = false;
break;
}
row = temp.row + 1;
col = temp.col - 2;
check(row, col);
flag = check(row, col);
if (flag == 1) {
ctr++;
flag1 = false;
break;
}
row = temp.row - 2;
col = temp.col + 1;
check(row, col);
flag = check(row, col);
if (flag == 1) {
ctr++;
flag1 = false;
break;
}
row = temp.row - 1;
col = temp.col + 2;
check(row, col);
flag = check(row, col);
if (flag == 1) {
ctr++;
flag1 = false;
break;
}
if (count == 0) {
count = q.size();
}
}
cout << "Case " << i << ": ";
if (flag1) {
cout << "IMPOSSIBLE" << endl;
}
else
cout << ctr << endl;
}
return 0;
}