#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int board[50][50];
int dist[50][50];
queue<pair<int, int>> q;
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
vector<int> solution(int n, int kk) {
vector<int> answer;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
board[i][j] = 0;
}
}
board[0][0] = 1;
q.push(make_pair(0, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j] = 2100000000;
}
}
dist[0][0] = 0;
//총 n*n번 숫자 집어넣기
for (int i = 2; i <= n * n; i++) {
//거리계산 후
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n) {
continue;
}
if (dist[nx][ny] == 0)
continue;
if (dist[nx][ny] > dist[x][y] + 1) {
dist[nx][ny] = dist[x][y] + 1;
q.push(make_pair(nx, ny));
}
}
}
//가장 먼 곳을 찾아
int farx = 0; int fary = 0; int maxfar = 0;
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (dist[k][j] > maxfar) {
maxfar = dist[k][j];
farx = k; fary = j;
}
}
}
//그곳에 다음 숫자 대입 후
board[farx][fary] = i;
//dist를 다시 초기화 및 숫자 적힌 곳을 큐로 넣음
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
//이미 관객이 들어왔다면, q에 넣고, 해당 거리는 0
if (board[j][k] != 0) {
q.push(make_pair(j, k));
dist[j][k] = 0;
}
else
dist[j][k] = 2100000000;
}
}
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (board[j][k] == kk) {
answer.push_back(j + 1);
answer.push_back(k + 1);
}
}
}
return answer;
}
int main() {
vector<int> ans = solution(5, 12);
for (auto a : ans)
cout << a << " ";
cout << "\n";
ans = solution(5, 16);
for (auto a : ans)
cout << a<< " ";
cout << "\n";
ans = solution(6, 13);
for (auto a : ans)
cout << a << " ";
cout << "\n";
}
'성우리뷰' 카테고리의 다른 글
파이썬 다단계칫솔 판매 (0) | 2021.12.04 |
---|---|
공 튕기기 (0) | 2021.11.28 |
구현) 달팽이 배열의 심화 >> 빈 구간을 점프하면서 채우기 (0) | 2021.11.01 |
오른쪽, 아래로 갈 수 있는 배열에서 최대 점수 획득 (0) | 2021.10.30 |
C++ 중첩 및 조합 이용, 덱에 숫자 넣어 최소한의 합 만들기 (1) | 2021.10.28 |