반응형
문제 출처 :
https://www.acmicpc.net/problem/3024
알고리즘 분석 :
문제 해결에 필요한 사항
1. 구현
가로, 세로, 대각선 중 연속으로 같은 이름이 3번 나오면 게임이 종료되는 방식이다.
단순 재귀를 이용하여 쉽게 문제를 해결 할 수 있다.
limit 함수는 x,y가 좌표 범위 밖으로 벗어나는지 체크하고 있고, 그 외 4가지 함수는 가로, 세로, 대각선 2종류를 체크해준다.
소스 코드 :
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #include <iostream> #include <cstdio> #include <string> using namespace std; char str[32][32]; int n; bool limit(int y, int x) { if (!(0 <= y && y < n) || !(0 <= x && x < n)) return false; return true; } bool goGaro(int y, int x, int cnt, char ch) { if (!limit(y, x) || str[y][x] != ch) return false; if (cnt == 3) return true; if (ch == str[y][x]) return goGaro(y, x - 1, cnt + 1, ch); } bool goSero(int y, int x, int cnt, char ch) { if (!limit(y, x) || str[y][x] != ch) return false; if (cnt == 3) return true; if (ch == str[y][x]) return goSero(y - 1, x, cnt + 1, ch); } bool goDia1(int y, int x, int cnt, char ch) { if (!limit(y, x) || str[y][x] != ch) return false; if (cnt == 3) return true; if (ch == str[y][x]) return goDia1(y - 1, x - 1, cnt + 1, ch); } bool goDia2(int y, int x, int cnt, char ch) { if (!limit(y, x) || str[y][x] != ch) return false; if (cnt == 3) return true; if (ch == str[y][x]) return goDia2(y - 1, x + 1, cnt + 1, ch); } int main() { scanf("%d", &n); int cnt = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf(" %c", &str[i][j]); if (str[i][j] == '.') continue; // 가로 if (goGaro(i, j, 1, str[i][j])) { printf("%c", str[i][j]); return 0; } // 세로 if (goSero(i, j, 1, str[i][j])) { printf("%c", str[i][j]); return 0; } // ↘ 대각선 if (goDia1(i, j, 1, str[i][j])) { printf("%c", str[i][j]); return 0; } // ↙ 대각선 if (goDia2(i, j, 1, str[i][j])) { printf("%c", str[i][j]); return 0; } } } printf("ongoing"); return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[2866번] 문자열 잘라내기 (0) | 2017.11.13 |
---|---|
[9996번] 한국이 그리울 땐 서버에 접속하지 (0) | 2017.11.13 |
[3023번] 마술사 이민혁 (0) | 2017.11.11 |
[2367번] 파티 (0) | 2017.11.10 |
[14715번] 전생했더니 슬라임 연구자였던 건에 대하여 (Easy) (0) | 2017.11.10 |