-
[SWEA] 1974. 스도쿠 검증 c++Coding Test/SW Expert Academy 2022. 11. 16. 16:33728x90
- 문제
9 X 9 크기의 스도쿠 퍼즐의 숫자들이 주어졌을 때, 위와 같이 겹치는 숫자가 없을 경우, 1을 정답으로 출력하고 그렇지 않을 경우 0 을 출력한다.
- 문제 해결
처음에는 1~9 까지의 합을 이용해서 문제를 풀어나갔지만, 10개 test중 5개만 계속 통과해서
방법을 바꾸었다.
1~9가 나왔는지 검사하는 방법으로 풀었다.
#include<iostream> #include <map> #include <memory.h> using namespace std; int main(int argc, char** argv) { int test_case; int T; cin>>T; for(test_case = 1; test_case <= T; ++test_case) { int board[10][10]; bool check[3][10][10]; memset( check, false, sizeof(check)); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ cin >> board[i][j]; check[0][i][board[i][j]] = true; //행 검사 check[1][j][board[i][j]] = true; //열 검사 int index = ((i - 1) / 3) * 3 +1 + (j - 1) / 3; check[2][index][board[i][j]] = true; // 그룹 검사 } } int res = 1; for(int i = 0; i < 3; i++){ for(int j = 1; j < 10; j++){ for(int k = 1; k < 10; k++){ if( !check[i][j][k]){ res = 0; break; } } } } cout << "#" << test_case << " " << res << endl; } return 0; }
테스트 실패 코드
#include<iostream> #include <vector> using namespace std; int main(int argc, char** argv) { int test_case; int T; cin>>T; for(test_case = 1; test_case <= T; ++test_case) { int res = 1; int index =0; vector<int> group( 9, 0); int total = (9 * 10 ) / 2; int vec[9][9]; for(int i = 0; i < 9; i++){ int row = 0, col = 0; for(int j = 0; j < 9; j++){ row += vec[i][j]; col += vec[j][i]; index = ((j) / 3) + ((i) / 3) * 3; group[index] += vec[i][j]; } if( row != total || col != total){ res = 0; } } for(int i = 0; i < 9; i++){ if( group[i] != total){ res = 0; } } cout << "#" << test_case << " " << res << endl; } return 0;//정상종료시 반드시 0을 리턴해야합니다. }
#include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int main(){ vector<vector<int>> vec = { {7, 3, 6, 4, 2, 9, 5, 8, 1}, {5, 8, 9, 1, 6, 7, 3, 2, 4}, {2, 1, 4, 5, 8, 3, 6, 9, 7}, {8 ,4 ,7 ,9 ,3 ,6 ,1 ,5 ,2}, {1, 5, 3, 8, 4, 2, 9, 7, 6}, {9 ,6 ,2 ,7 ,5 ,1 ,8 ,4 ,3}, {4 ,2 ,1 ,3, 9, 8, 7, 6, 5}, {3, 9, 5, 6, 7, 4, 2, 1, 8}, {6 ,7 ,8 ,2 ,1, 5 ,4 ,3 ,9}, }; int index =0; vector<int> group( 9, 0); int total = (9 * 10 ) / 2; for(int i = 0; i < 9; i++){ int row = 0, col = 0; for(int j = 0; j < 9; j++){ row += vec[i][j]; col += vec[j][i]; index = j / 3 + (i / 3) * 3; group[index] += vec[i][j]; } if( row != total || col != total){ cout << "#" << 1 << " " << 0 << endl; return 0; } } for(auto &it : group){ if( it != total){ cout << "#" << 1 << " " << 0 << endl; return 0; } } cout << "#" << 1 << " " << 1 << endl; return 0; }
728x90'Coding Test > SW Expert Academy' 카테고리의 다른 글
[SWEA] 1961. 숫자 배열 회전 c++ (0) 2022.11.17 [SWEA] 1970. 쉬운 거스름돈 c++ (0) 2022.11.16 [SWEA] 1976. 시각 덧셈 c++ (0) 2022.11.16 [SWEA] 1979. 어디에 단어가 들어갈 수 있을까 c++ (0) 2022.11.16 [SWEA] 1983. 조교의 성적 매기기 c++ (0) 2022.11.15