forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknightsearch.cpp
55 lines (43 loc) · 1.08 KB
/
knightsearch.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
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> dx = {1,2,2,1,-1,-2,-2,-1};
vector<int> dy = {-2,-1,1,2,2,1,-1,-2};
bool inrange(int i, int j) {
return i >= 0 && j >= 0 && i < n && j < n;
}
bool dfs(vector<vector<char>>& v, int i, int j, int pos, string& s) {
if(pos == s.size()) {
return true;
}
for(int k = 0; k < 8; k++) {
int nexti = i + dx[k];
int nextj = j + dy[k];
if(!inrange(nexti,nextj)) continue;
if(v[nexti][nextj] == s[pos]) {
if(dfs(v,nexti,nextj,pos+1,s)) {
return true;
}
}
}
return false;
}
int main() {
cin >> n;
vector<vector<char>> v(n, vector<char>(n));
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> v[i][j];
}
}
string target = "ICPCASIASG";
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(dfs(v,i,j,0,target)) {
cout << "YES" << endl;
return 0;
}
}
}
cout << "NO" << endl;
}