250116 16:24 ~ 17:25
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <filesystem>
using namespace std;
#include <stack>
#include <limits.h>
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// 250116 16:24 ~ 17:25
int n;
int m;
cin >> n >> m;
// 마지막 지점으로 향할때 는 증감하면 안된다.
// n이 3라고 하면
// 가운데 지점은 [1][1] = arr[0]
// 위 -> 오른 -> 아래 -> 왼쪽으로 반복되는 방향변화
vector<pair<int, int>> dir
= { {-1,0},{0,1},{1,0},{0,-1} };
int ssize = pow(n, 2);
vector<vector<int>> v(ssize, vector<int>(ssize));
int startY = n / 2;
int startX = n / 2;
v[startY][startX] = 1;
int dy = startY;
int dx = startX;
int cnt2 = 0;
int cnt = 0;
int a = 1;
int b = 2;
//for (int i = 2; i <= ssize; ++i)
while(b < ssize)
{
//dy += dir[cnt].first;
//dx += dir[cnt].second;
cnt2++;
if (cnt2 % 2 == 0)
a++;
for (int i = 0; i < a; ++i)
{
dy += dir[cnt].first;
dx += dir[cnt].second;
v[dy][dx] = b++;
//cout << v[dy][dx] << endl;
}
cnt = (++cnt) % 4;
//++ccnt;
//if (ccnt % 2 == 0)
// a++;
}
for (auto& iter : v)
{
for (auto& iter2 : iter)
{
cout << iter2 << " ";
}
cout << endl;
}
}
초기값 설정을 잘 해야 한다.
1 일 때 의 위치는 2,2 이다. 그렇다고 해서 초기값을 1,1로 설정하는 것이 아니라, (내가 단정 지으면 안돼)
1이 들어갈 위치는 중앙 pos 이므로 이렇게 설정해야 한다.
이렇게 해야 맞음.
반례를 찾으려고 하지 말고, 나의 코드에서 뭐가 잘못되었을까?
판단 하는 것이 정답으로 가는 길이다.