Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- --watch
- Unity IAP
- MySQL
- nodejs
- Camera Movement
- Git
- Digital Ocean
- linux
- react
- express
- Google Refund
- Spring Boot
- OverTheWire
- java
- Google Developer API
- Camera Zoom
- mongoDB
- docker
- Unity Editor
- css framework
- critical rendering path
- springboot
- screencapture
- spread 연산자
- server
- rpg server
- Packet Network
- draganddrop
- SDK upgrade
- unity
Archives
- Today
- Total
우당탕탕 개발일지
[프로그래머스] Level 2. 혼자서 하는 틱택토 본문
https://school.programmers.co.kr/learn/courses/30/lessons/160585
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
해결 방법
- O의 개수가 X 개수보다 한 개 많거나, O의 개수가 X의 개수와 동일해야한다.
- O 가 승리했는데 X 또한 승리하는 경우는 있을 수 없다.
- 만약 O만 승리했다면, O의 개수는 X보다 한 개 많아야 한다. O가 놓으면서 게임이 바로 종료되기 때문이다.
- 마찬가지로, 만약 X만 승리했다면, O의 개수는 X와 동일해야 한다. X가 놓으면서 게임이 바로 종료되기 때문이다. (O가 선공)
- 위 4가지 경우를 찾기 위해 보드 위 O의 개수 Ocnt, X의 개수 Xcnt, O가 이겼는지 여부 Owin, X가 이겼는지 여부 Xwin을 각각 구한다.
#include <string>
#include <vector>
using namespace std;
vector<vector<char>> b(3,vector<char>(3));
bool iswin(char a){
if(b[0][0] == a && b[1][1] == a && b[2][2] == a) return true;
if(b[0][2] == a && b[1][1] ==a && b[2][0] == a) return true;
for(int i =0; i<3; i++){
bool row = true; bool col = true;
for(int j=0; j<3; j++){
if(b[i][j] != a) row= false;
if(b[j][i] != a) col = false;
}
if(row == true || col == true) return true;
}
return false;
}
int solution(vector<string> board) {
int Ocnt = 0; int Xcnt = 0;
for(int i=0; i<board.size(); i++){
for(int j=0; j<board[i].size(); j++){
b[i][j] = board[i][j];
if(b[i][j] == 'O') Ocnt++;
else if(b[i][j] == 'X') Xcnt++;
}
}
if(Ocnt!= Xcnt && Ocnt != Xcnt+1) return 0;
bool Owin = iswin('O'); bool Xwin = iswin('X');
if(Owin && Xwin) return 0;
if(Owin && Ocnt != Xcnt+1) return 0;
if(Xwin && Ocnt != Xcnt) return 0;
return 1;
}
코드 작성은 어렵지 않았다. 어떤 경우가 실수한 경우인지를 찾아내는 것이 핵심이었던 문제이다.
'Algorithm(c++) > Level 2' 카테고리의 다른 글
[프로그래머스] Level 2. 리코쳇 로봇 (1) | 2025.02.17 |
---|---|
[프로그래머스] Level 2. 연속된 부분 수열 (0) | 2025.02.16 |
[프로그래머스] Level 2. 두 원 사이의 정수쌍 (0) | 2025.02.13 |
[프로그래머스] Level 2. 아날로그 시계 (0) | 2025.02.12 |
[프로그래머스] Level 2. 당구 연습 (1) | 2025.02.11 |