-
Notifications
You must be signed in to change notification settings - Fork 1
/
B.cpp
56 lines (39 loc) · 945 Bytes
/
B.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
#include <bits/stdc++.h>
using namespace std;
set<string> visited;
int N;
string board;
string winning_state;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
inline void change(int c) {
int i = c / N;
int j = c % N;
for(int k = 0; k < 4; ++k) {
int p = (i + dx[k]) * N + (j + dy[k]);
if (p > 0 && p < board.size()) board[p] = (1 - (board[p] - '0')) + '0';
}
}
bool dfs() {
if (board == winning_state) return true;
visited.insert(board);
for(int i = 0; i < board.size(); ++i) {
change(i);
if (!visited.count(board)) if(dfs()) return true;
change(i);
}
return false;
}
int main() {
scanf("%d", &N);
char line[50];
board = "";
for(int i = 0; i < N; ++i) {
scanf(" %[^\n]", line);
board += string(line);
}
winning_state = string(N * N, '1');
if (dfs()) printf("Y\n");
else printf("N\n");
return 0;
}