알고리즘 문제/BOJ
16929번 Two Dots
parkit
2019. 3. 21. 12:14
728x90
반응형
https://www.acmicpc.net/problem/16929
계속 예제 2번과 출력이 다르길래 생각해보니
cnt 변수를 전역 변수로 쓰는 바람에 52번 째 if 조건문에 통과되었다.
재귀 함수 매개변수로 써야 했는데, 실수 하였다.
그리고 visit를 어디서 어떻게 활용하느냐도 중요하다.
cnt를 전역 변수로 말고, 매개 변수로 쓰고 제출하니 맞았다.
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 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> #include <set> #include <tuple> #pragma warning(disable:4996) #pragma comment(linker, "/STACK:336777216") using namespace std; // https://www.acmicpc.net/problem/16929 char Map[55][55]; int N = 0, M = 0, sy = 0, sx = 0; int dy[4] = { 0, 1, 0, -1 }; int dx[4] = { 1, 0, -1, 0 }; bool visit[55][55] = { false, }; bool stop = false; void DFS(int r, int c, int cnt) { if (stop) return; visit[r][c] = true; for (int i = 0; i < 4; i++) { int y = r + dy[i]; int x = c + dx[i]; if (y < 0 || y >= N || x < 0 || x >= M || Map[r][c] != Map[y][x]) continue; if (!visit[y][x]) { visit[y][x] = true; DFS(y, x, cnt + 1); } else { if (sy == y && sx == x && cnt >= 4) { stop = true; return; } } } } int main(void) { scanf("%d %d", &N, &M); for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) cin >> Map[i][j]; for (int i = 0; i < N && !stop; i++) { for (int j = 0; j < M && !stop; j++) { memset(visit, false, sizeof(visit)); sy = i; sx = j; DFS(i, j, 1); } } if (stop) printf("Yes\n"); else printf("No\n"); return 0; } | cs |
728x90
반응형