[프로그래머스 C++] 혼자서 하는 틱택토
[프로그래머스 C++] 혼자서 하는 틱택토
https://school.programmers.co.kr/learn/courses/30/lessons/160585
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
해결전략
구현
정답코드
#include <iostream> #include <string> #include <vector> using namespace std; int solution(vector<string> b) { int answer = 1; int SumBingGo_O = 0; // O 전체 빙고 수 int SumBingGo_X = 0; // X 전체 빙고 수 int black = 0; // O 개수 int white = 0; // X 개수 vector<int> ColumnBingGo_O(3, 0); // O 세로 빙고 vector<int> ColumnBingGo_X(3, 0); // X 세로 빙고 for (int y = 0; y < 3; y++) { int RowBingGo_X = 0; int RowBingGo_O = 0; for (int x = 0; x < 3; x++) { if (b[y][x] == 'O') black++; else if (b[y][x] == 'X') white++; if (x < 2 && b[y][x] == b[y][x + 1] && b[y][x] == 'O') RowBingGo_O++; if (x < 2 && b[y][x] == b[y][x + 1] && b[y][x] == 'X') RowBingGo_X++; if (y < 2 && b[y][x] == b[y + 1][x] && b[y][x] == 'O') ColumnBingGo_O[x]++; if (y < 2 && b[y][x] == b[y + 1][x] && b[y][x] == 'X') ColumnBingGo_X[x]++; } // 가로 빙고 if (RowBingGo_O == 2) SumBingGo_O++; if (RowBingGo_X == 2) SumBingGo_X++; } // 세로 빙고 for (int i = 0; i < 3; i++) { if (ColumnBingGo_O[i] == 2) SumBingGo_O++; if (ColumnBingGo_X[i] == 2) SumBingGo_X++; } // 대각선 빙고 if (b[0][0] == b[1][1] && b[1][1] == b[2][2] && b[1][1] == 'O') SumBingGo_O++; if (b[0][2] == b[1][1] && b[1][1] == b[2][0] && b[1][1] == 'O') SumBingGo_O++; if (b[0][0] == b[1][1] && b[1][1] == b[2][2] && b[1][1] == 'X') SumBingGo_X++; if (b[0][2] == b[1][1] && b[1][1] == b[2][0] && b[1][1] == 'X') SumBingGo_X++; // 후공인 X가 더 많은 경우, 선공인 O가 1개 초과 더 많은 경우 if (black < white || black > white + 1) { return 0; } if (SumBingGo_O >= 1){ if (SumBingGo_X >= 1) return 0; if (black <= white) return 0; } if (SumBingGo_X >= 1){ if (SumBingGo_O >= 1) return 0; if (black != white) return 0; } return answer; } int main(){ vector<string> testcase1 = { "O.X", ".O.", "..X" }; vector<string> testcase2 = { "OOO", "...", "XXX" }; vector<string> testcase3 = { "...", ".X.", "..." }; vector<string> testcase4 = { "...", "...", "..." }; vector<string> testcase5 = { "XXO", ".OX", "O.." }; vector<string> testcase6 = { "OOO", "XOX", "OXX" }; vector<string> testcase7 = { "XOX", "OXO", "XOX" }; cout << solution(testcase1) << "\n"; // 1 cout << solution(testcase2) << "\n"; // 0 cout << solution(testcase3) << "\n"; // 0 cout << solution(testcase4) << "\n"; // 1 cout << solution(testcase5) << "\n"; // 0 cout << solution(testcase6) << "\n"; // 1 cout << solution(testcase7) << "\n"; // 0 return 0; }
참고자료
2022.11.09 - [⭐ Programming/자료구조와 알고리즘] - [알고리즘] 동적계획법 - Tic-Tae-Toe
'⭐ 코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 C++] 조이스틱 (0) | 2024.04.01 |
---|---|
[프로그래머스 C++] 숫자 블록 (0) | 2024.03.23 |
[프로그래머스 C++] 가장 긴 팰린드롬 (0) | 2024.03.20 |
[프로그래머스 C++] 이모티콘 할인행사 (0) | 2024.03.18 |
[프로그래머스 C++] 두 원 사이의 정수 쌍 (0) | 2024.03.17 |
댓글
이 글 공유하기
다른 글
-
[프로그래머스 C++] 조이스틱
[프로그래머스 C++] 조이스틱
2024.04.01[프로그래머스 C++] 조이스틱 https://school.programmers.co.kr/learn/courses/30/lessons/42860 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 해결전략 그리디 Greedy Algorithm 결국 못 풀고 다른사람 풀이를 봤다… 좌우 이동 생각하기 까다롭네… 정답 코드 #include #include #include #include using namespace std; int solution(string name) { int answer = 0; // 상하 조작 횟수 int CursorMove = nam… -
[프로그래머스 C++] 숫자 블록
[프로그래머스 C++] 숫자 블록
2024.03.23[프로그래머스 C++] 숫자 블록 https://school.programmers.co.kr/learn/courses/30/lessons/12923 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 해결전략 구현 처음 시도한 코드 #include using namespace std; long long Cal(long long k) { for(int i = 2; i * i < k; i++) { if(k % i == 0){ return k / i; } } return 1; } vector solution(long long begin, long long end) … -
[프로그래머스 C++] 가장 긴 팰린드롬
[프로그래머스 C++] 가장 긴 팰린드롬
2024.03.20 -
[프로그래머스 C++] 이모티콘 할인행사
[프로그래머스 C++] 이모티콘 할인행사
2024.03.18
댓글을 사용할 수 없습니다.