간단한 dfs 문제
* 같은 방향으로 원하는 문자열을 찾으면 되는 문제이다
* 반드시 문자열을 찾을 수 있다.
* 답이 여러개인 경우 가장 왼쪽부근에 위치한 답을 출력하라고 하는데,
그냥 행->열 순서로 탐색하면 해결된다.
alpha 함수는 대소문자가 같은 경우를 리턴하는 함수
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
|
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits>
#include <cstring>
#include <string>
using namespace std;
string arr[51];
bool visit[52][52];
int row, col;
int mx[8] = { 0,1,1,1,0,-1,-1,-1 };
int my[8] = { -1,-1,0,1,1,1,0,-1 };
bool alpha(char now_al, char stand) {
if (now_al == stand || now_al - '0' + 32 == stand - '0' || now_al - '0' - 32 == stand - '0') return true;
else return false;
}
bool dfs(int y, int x, int cnt, string str, int direct) {
if (cnt == str.size()) return true;
for (int i = 0; i < 8; i++) {
if (direct == -1 || direct == i) {
int yy = y + my[i];
int xx = x + mx[i];
if (yy >= 0 && yy < row && xx >= 0 && xx < col && !visit[yy][xx]) {
if (alpha(arr[yy][xx], str[cnt])) {
visit[yy][xx] = true;
if (dfs(yy, xx, cnt + 1, str, i)) return true;
visit[yy][xx] = false;
}
}
}
}
return false;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int testcase;
cin >> testcase;
while (testcase--) {
cin >> row >> col;
for (int i = 0; i < row; i++)
cin >> arr[i];
vector<pair<int, pair<int, int> > > v;
int test;
cin >> test;
while (test--) {
string str;
cin >> str;
bool check = false;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (alpha(str[0], arr[i][j])) {
if (dfs(i, j, 1, str, -1)) {
cout << i + 1 << " " << j + 1 << "\n";
memset(visit, 0, sizeof(visit));
check = true;
break;
}
}
}
if (check) break;
}
}
cout << "\n";
}
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'algorithm > uva' 카테고리의 다른 글
uva 10150번 Doublets (0) | 2020.03.27 |
---|---|
uva 10469번 To Carry or not to Carry (0) | 2020.03.23 |
uva 679 - Dropping Balls (0) | 2020.03.19 |
uva 10137 - the trip, BOJ 4411번 (0) | 2020.03.19 |
uva 100 - 3n+1 problem (0) | 2020.03.18 |