-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerate_Word.cpp
More file actions
65 lines (65 loc) · 1.34 KB
/
Generate_Word.cpp
File metadata and controls
65 lines (65 loc) · 1.34 KB
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
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll = long long;
int n, m, ok = 1;
char a[30][30];
string s, res;
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
bool used[30][30];
void Try(int i, int j)
{
if(res == s)
{
ok = 0; return;
}
for(int k = 0; k < 4; k++)
{
int i1 = i + dx[k], j1 = j + dy[k];
if(i1 >= 1 && i1 <= n && j1 >= 1 && j1 <= m && !used[i1][j1])
{
res += a[i1][j1];
used[i1][j1] = true;
if(res == s)
{
ok = 0;
return;
}
else if(s.find(res) != string::npos)
{
Try(i1, j1);
}
used[i1][j1] = false;
res.pop_back();
}
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> a[i][j];
}
}
cin >> s;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(a[i][j] == s[0])
{
res = "";
memset(used, false, sizeof(used));
used[i][j] = true;
res += a[i][j];
Try(i, j);
}
}
}
if(!ok) cout << "YES" << endl;
else cout << "NO" << endl;
}