Baekjoon

Baekjoon 2615번 오목

ppwag 2021. 5. 16. 02:17

문제

https://www.acmicpc.net/problem/2615

걸린 시간

-

풀이

C++

#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0), cin.tie(0)
#define all(c) c.begin(), c.end()
#define INF 1e9
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

bool compare(pii a, pii b){
    if(a.second < b.second) return true;
    else if(a.second == b.second) return a.first < b.first;
    else return false;
}

void print(int winner, vector<pii>& tmp){
    sort(all(tmp), compare);
    cout << winner << "\n";
    cout << tmp[0].first+1 << " " << tmp[0].second+1 << "\n";
}

// horizontal, vertical, slash, backSlash
int dy[8] = {0, 0, 1, -1, -1, 1, 1, -1};
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};

int main(){
    fastio;
    int n = 19;
    vector<vector<int>> b(n, vector<int>(n));
    for(int y = 0; y < n; y++){
        for(int x = 0; x < n; x++){
            cin >> b[y][x];
        }
    }
    for(int y = 0; y < n; y++){
        for(int x = 0; x < n; x++){
            if(b[y][x]){
                vector<pii> tmp;
                for(int i = 0; i < 8; i++){
                    int ny = y;
                    int nx = x;
                    while(true){
                        ny += dy[i];
                        nx += dx[i];
                        if(ny < 0 || ny >= n || nx < 0 || nx >= n) break;
                        if(b[ny][nx] != b[y][x]) break;
                        tmp.push_back({ny, nx});
                    }
                    if(i%2 == 1){
                        tmp.push_back({y, x});
                        if(tmp.size() == 5){
                            print(b[y][x], tmp);
                            return 0;
                        }
                        else{
                            tmp.clear();
                        }
                    }
                }
            }
        }
    }
    cout << 0 << "\n";
    return 0;
}

연속된 5개의 바둑알 중 가장 왼쪽의 바둑알을, 세로로 연속될 경우 제일 위쪽의 바둑돌을 출력하여야 한다. 다시 말하면 제일 작은 y, x 좌표 값을 출력하라는 뜻인데 compare 함수를 대충 작성하는 바람에 애를 먹었다. 오름차순으로 정렬할 때, 1순위로 가로 좌표를 먼저 비교하고 같을 경우에만 2순위로 세로 좌표를 비교해주어야 하지만 클 경우도 세로 좌표 값을 비교해주어서 정렬이 재대로 되지 않았다.

'Baekjoon' 카테고리의 다른 글

Baekjoon 13549번 숨바꼭질 3  (0) 2021.09.12
Baekjoon 1411번 비슷한 단어  (0) 2021.07.26
Baekjoon 1914번 하노이 탑  (0) 2021.05.15
Baekjoon 1052번 물병  (0) 2021.05.15
Baekjoon 2457번 공주님의 정원  (0) 2021.05.04

댓글