0. [c++] 백준 - |
https://www.acmicpc.net/problem/1780
1. 풀이 |
1) 라인을 읽어들여서 string을 가공한 이후 문제를 해결하려고 생각을 하였다.
test코드 까지는 통과하였지만, 왠지 모르게 채점에서 오류가 발생하여 다른 방법으로 넘어가 보았다.
2) 애초에 int로 읽어들인 이후, N*N이 동일한지 확인하는 과정을 따로 나누어 구현하였다.
2. 소스코드 |
1) string 활용
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 | #include<iostream> #include<string> #include<algorithm> using namespace std; int ret[3]; char arr[20000]; std::string trim_arr[2200]; void cut(int y, int x, int yy, int xx) { int flag = 0; char find = trim_arr[y].at(x); if (x == xx) { ret[find - '0']++; flag++; } else { int temp = (yy - y + 1) / 3; for (int i = y; i < yy; i++) { for (int j = x; j < xx; j++) { //만일 같지 않은 숫자가 나오면, 9조각으로 나눠 자른다. if (find != trim_arr[i].at(j) && flag == 0) { cut(y, x, y + temp - 1, x + temp - 1); cut(y, x + temp, y + temp - 1, x + temp * 2 - 1); cut(y, x + temp * 2, y + temp - 1, x + temp * 3 - 1); cut(y + temp, x, y + temp * 2 - 1, x + temp - 1); cut(y + temp, x + temp, y + temp * 2 - 1, x + temp * 2 - 1); cut(y + temp, x + temp * 2, y + temp * 2 - 1, x + temp * 3 - 1); cut(y + temp * 2, x, y + temp * 3 - 1, x + temp - 1); cut(y + temp * 2, x + temp, y + temp * 3 - 1, x + temp * 2 - 1); cut(y + temp * 2, x + temp * 2, y + temp * 3 - 1, x + temp * 3 - 1); flag++; } } } } if (flag == 0) { ret[find - '0']++; } } std::string ReplaceAll(std::string &str, const std::string& from, const std::string& to) { size_t start_pos = 0; //string처음부터 검사 while ((start_pos = str.find(from, start_pos)) != std::string::npos) //from을 찾을 수 없을 때까지 { str.replace(start_pos, from.length(), to); start_pos += to.length(); // 중복검사를 피하고 from.length() > to.length()인 경우를 위해서 } return str; } int main() { int N; std::cin >> N; for (int y = 0; y <= N; y++) { //공백을 포함하여 입력받는다. std::cin.getline(arr, 8000); trim_arr[y] = arr; //공백을 제거하자. trim_arr[y].erase(std::remove(trim_arr[y].begin(), trim_arr[y].end(), ' '), trim_arr[y].end()); //-1을 2로 교체하자. ReplaceAll(trim_arr[y], string("-1"), string("2")); } cut(1, 0, N, N - 1); cout << ret[2] << "\n" << ret[0] << "\n" << ret[1] << endl; return 0; } | cs |
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 | #include<iostream> using namespace std; int num[2200][2200]; int ret[3]; bool check(int y, int x, int N) { int find = num[y][x]; for (int i = y; i < y + N; i++) for (int j = x; j < x + N; j++) if (find != num[i][j]) return false; return true; } int divide(int y, int x, int N) { if (check(y, x, N)) { ret[num[y][x]]++; } //만일 모든 수가 동일하지 않다면, 9등분으로 나눠준다. else { int size = N / 3; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { divide(y + size * i, x + size * j, size); } } } return 0; } int main() { int N; cin >> N; for (int y = 1; y <= N; y++) { for (int x = 1; x <= N; x++) { cin >> num[y][x]; num[y][x] += 1; } } divide(1, 1, N); cout << ret[0] << "\n" << ret[1] << "\n" << ret[2]; } | cs |
3. 참고 |
- string에서 치환하기
- string 클래스 함수 정리
질문이나 지적 있으시면 댓글로 남겨주세요~
도움 되셨으면 하트 꾹!
728x90
반응형
'<백준> > |C++| hard' 카테고리의 다른 글
[c++] 백준 1015 - 수열 정렬 (0) | 2019.04.30 |
---|---|
[c++] 백준 6549 - 히스토그램에서 가장 큰 직사각형 (0) | 2019.04.27 |
[C++] 백준 1107 - 리모컨(brute force) (4) | 2019.04.24 |
[c++] 백준 2981 - 검문 (0) | 2019.04.17 |
[c++] 백준 1022 - 소용돌이 예쁘게 출력하기 (0) | 2019.04.16 |