Skip to content

Instantly share code, notes, and snippets.

@honux77
Created May 8, 2019 14:34
Show Gist options
  • Save honux77/f5073790b0aae686e114121344d810f9 to your computer and use it in GitHub Desktop.
Save honux77/f5073790b0aae686e114121344d810f9 to your computer and use it in GitHub Desktop.
BOJ 1913
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int dr[] = { -1, 0, 1, 0 };
int dc[] = { 0, 1, 0, -1 };
int main() {
int n;
scanf("%d", &n);
vector <vector <int>> m(n);
map<int, pair<int, int>> nmap;
for (int i = 0; i < n; i++) {
m[i].resize(n);
}
int num = 1;
int row = n / 2;
int col = row;
double w = 1;
m[row][col] = num;
int vec = 0;
while (true) {
for (int i = 0; i < (int) w; i++) {
num++;
if (num > n * n) goto answer;
row += dr[vec];
col += dc[vec];
m[row][col] = num;
nmap[num] = make_pair(row + 1, col + 1);
}
vec = (vec + 1) % 4;
w += 0.5;
}
answer:
for (auto v : m) {
for (auto n : v) {
printf("%d ", n);
}
printf("\n");
}
int x;
scanf("%d", &x);
printf("%d %d\n", nmap[x].first, nmap[x].second);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment