Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pb(x) push_back(x)
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- vector <int> dx, dy;
- int n;
- vector <vector <int> > m, id;
- bool ok1(int i, int j) {
- return i >= 0 && j >= 0 && i + j < n;
- }
- bool ok2(int i, int j) {
- return i < n && j < n && i + j > n;
- }
- bool sh = 1;
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin >> n;
- dx = { 1, -1, 0, 1,};
- dy = { 0, 1, 1, -1,};
- int i = 0, j = 0;
- m.assign(n, vector <int> (n, 0));
- id.assign(n, vector <int> (n, 0));
- int cnt = 1;
- id[0][0] = 1;
- int tk = 1;
- while (tk != (n - 1) * n / 2) {
- if (ok1(i + dx[0], j + dy[0])) {
- cnt+=2;
- tk++;
- i = i + dx[0];
- j = j + dy[0];
- id[i][j] = cnt;
- }
- while (ok1(i + dx[1], j + dy[1])) {
- cnt+=2;
- tk++;
- i += dx[1];
- j += dy[1];
- id[i][j] = cnt;
- }
- if (ok1(i + dx[2], j + dy[2])) {
- cnt+=2;
- tk++;
- i += dx[2];
- j += dy[2];
- id[i][j] = cnt;
- }
- while (ok1(i + dx[3], j + dy[3])) {
- cnt+=2;
- tk++;
- i += dx[3];
- j += dy[3];
- id[i][j] = cnt;
- }
- }
- dx = {0, -1, -1, 1};
- dy = {-1, 1, 0, -1};
- cnt = 2;
- tk++;
- id[n-1][n-1] = 2;
- i = n - 1;
- j = n - 1;
- while (tk != (n - 1) * n) {
- if (ok2(i + dx[0], j + dy[0])) {
- cnt+=2;
- tk++;
- i = i + dx[0];
- j = j + dy[0];
- id[i][j] = cnt;
- }
- while (ok2(i + dx[1], j + dy[1])) {
- cnt+=2;
- tk++;
- i += dx[1];
- j += dy[1];
- id[i][j] = cnt;
- }
- if (ok2(i + dx[2], j + dy[2])) {
- cnt+=2;
- tk++;
- i += dx[2];
- j += dy[2];
- id[i][j] = cnt;
- }
- while (ok2(i + dx[3], j + dy[3])) {
- cnt+=2;
- tk++;
- i += dx[3];
- j += dy[3];
- id[i][j] = cnt;
- }
- }
- cnt++;
- tk++;
- i = 0;
- j = n - 1;
- id[i][j] = cnt;
- while (tk != n * n) {
- cnt++;
- tk++;
- i++;
- j--;
- id[i][j] = cnt;
- }
- if (sh) {
- cvv(id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement