알고리즘
백준 1913번 달팽이 C++
영춘권의달인
2023. 2. 17. 11:58
https://www.acmicpc.net/problem/1913
1913번: 달팽이
N개의 줄에 걸쳐 표를 출력한다. 각 줄에 N개의 자연수를 한 칸씩 띄어서 출력하면 되며, 자릿수를 맞출 필요가 없다. N+1번째 줄에는 입력받은 자연수의 좌표를 나타내는 두 정수를 한 칸 띄어서
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,0 };
int dCol[4] = { 0,1,0,-1 };
int n, target;
int dir = 3;
vector<vector<int>> board;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> target;
board = vector<vector<int>>(n, vector<int>(n, -1));
int currRow = n / 2;
int currCol = n / 2;
int targetRow = -1;
int targetCol = -1;
int num = 0;
while (true) {
board[currRow][currCol] = ++num;
if (board[currRow][currCol] == target) {
targetRow = currRow;
targetCol = currCol;
}
if (currRow == 0 && currCol == 0) break;
int rightDir = (dir + 1) % 4;
int nextRow = currRow + dRow[rightDir];
int nextCol = currCol + dCol[rightDir];
if (board[nextRow][nextCol] == -1) {
dir = rightDir;
}
else {
nextRow = currRow + dRow[dir];
nextCol = currCol + dCol[dir];
}
currRow = nextRow;
currCol = nextCol;
}
for (auto a : board) {
for (auto b : a) {
cout << b << " ";
}
cout << "\n";
}
cout << targetRow + 1 << " " << targetCol + 1;
};