반응형
해결 방법
누가봐도 DFS 문제라 DFS 돌림.
처음엔 string "ASCR" 이런식으로 밟은 알파벳 저장하고 find로 밟았는지 찾다가, 시간초과 나와서 밟았는지 체크용 전역 배열UsingAlphabet[26] 선언. 배열 인덱스는 A~Z다.
아쉬웠던 점
없음
코드
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 <iostream>
#include <string>
using namespace std;
char board[20][20];
pair<int,int> location[4] = {{0,1},{1,0},{0,-1},{-1,0}};
int returnCount = 0;
bool UsingAlphabet[26] = { false, };
int x, y; //사실 xy가 반대긴하다.
void Move(int startX, int startY, int count)
{
if(returnCount < count)
returnCount = count;
for (int i = 0; i < 4; i++)
{
int nextX = startX + location[i].first;
int nextY = startY + location[i].second;
if(nextX < 0 || nextX > x-1 || nextY < 0 || nextY > y-1)
continue;
if(UsingAlphabet[board[nextX][nextY]-'A'] == false)
{
UsingAlphabet[board[nextX][nextY]-'A'] = true;
Move(nextX, nextY, count+1);
}
}
UsingAlphabet[board[startX][startY]-'A'] = false;
}
int main(int argc, char* argv[])
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> x >> y;
for (int i = 0; i < x; i++)
{
string str;
cin >> str;
for (int j = 0; j < y; j++)
{
board[i][j] = str[j];
}
}
UsingAlphabet[board[0][0]-'A'] = true;
Move(0,0, 1);
cout << returnCount;
return 0;
}
|
cs |
반응형
'코딩 > 문제풀이-백준' 카테고리의 다른 글
백준 1753번 - 최단경로 (C++) (0) | 2025.01.09 |
---|---|
백준 1149번 - RGB거리 (C++) (0) | 2025.01.08 |
백준 1083번 - 소트 (C++) (0) | 2024.10.28 |
백준 10282번 - 해킹 (C++) (2) | 2021.11.03 |
백준 13460번 - 구슬 탈출 2 (C++) (0) | 2021.10.26 |
댓글