알고리즘
백준 2615번 오목 C++
영춘권의달인
2023. 5. 6. 13:49
https://www.acmicpc.net/problem/2615
2615번: 오목
오목은 바둑판에 검은 바둑알과 흰 바둑알을 교대로 놓아서 겨루는 게임이다. 바둑판에는 19개의 가로줄과 19개의 세로줄이 그려져 있는데 가로줄은 위에서부터 아래로 1번, 2번, ... ,19번의 번호
www.acmicpc.net
육목 이상이 되는 부분을 조심해야한다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int dRow[4] = { -1,0,1,1 };
int dCol[4] = { 1,1,1,0 };
vector<vector<int>> board;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
board = vector<vector<int>>(19, vector<int>(19));
for (int i = 0; i < 19; i++)
for (int j = 0; j < 19; j++)
cin >> board[i][j];
bool finish = false;
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (board[i][j] == 0) continue;
for (int k = 0; k < 4; k++) {
int nextRow = i;
int nextCol = j;
int prevCnt = 0;
int nextCnt = 1;
while (true) {
nextRow -= dRow[k];
nextCol -= dCol[k];
if (nextRow < 0 || nextRow >= 19) break;
if (nextCol < 0 || nextCol >= 19) break;
if (board[nextRow][nextCol] != board[i][j]) break;
prevCnt++;
}
nextRow = i;
nextCol = j;
while (true) {
nextRow += dRow[k];
nextCol += dCol[k];
if (nextRow < 0 || nextRow >= 19) break;
if (nextCol < 0 || nextCol >= 19) break;
if (board[nextRow][nextCol] != board[i][j]) break;
nextCnt++;
}
int cnt = prevCnt + nextCnt;
if (cnt == 5 && prevCnt == 0) {
finish = true;
cout << board[i][j] << "\n";
cout << i + 1 << " " << j + 1;
}
}
}
}
if (finish == false) cout << 0;
}