본문 바로가기

백준 문제풀이/Bruteforcing

[C++] 백준 문제풀이 (Bruteforcing) 10157번 자리배정

 

 

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

 

10157번: 자리배정

첫 줄에는 공연장의 격자 크기를 나타내는 정수 C와 R이 하나의 공백을 사이에 두고 차례대로 주어진다. 두 값의 범위는 5 ≤ C, R ≤ 1,000이다. 그 다음 줄에는 어떤 관객의 대기번호 K가 주어진다.

www.acmicpc.net

 

 

//[C++] 백준 문제풀이 (Bruteforcing)

#include <bits/stdc++.h>


int r, c, k;

int seats[1001][1001];


int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin >> c >> r >> k;

    if (k > r * c) {
        std::cout << 0 << "\n";
        return 0;
    }

    int y = 1, x = 1;
    int number = 1;
    seats[y][x] = number;

    while(number <= k){
        if (number == k){
            std::cout << x << " " << y << "\n";
            break;
        }
        while(y < r && number < k && seats[y + 1][x] == 0){
            ++y;
            ++number;
            seats[y][x] = number;
        }
        while(x < c && number < k && seats[y][x + 1] == 0){
            ++x;
            ++number;
            seats[y][x] = number;
        }
        while(y > 1 && number < k && seats[y - 1][x] == 0){
            --y;
            ++number;
            seats[y][x] = number;
        }
        while(x > 1 && number < k && seats[y][x - 1] == 0){
            --x;
            ++number;
            seats[y][x] = number;
        }
    }
    return 0;
}