跳到正文

个人记录

马蹄集

liangcc约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年8月12日
更新
2026年8月12日
篇幅
约 2 分钟

版本

  1. v1当前

Passw0rd Passw0rd Passw0rd

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 15;
const int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int n, x = 1, y = 1, a[N][N], cur = 0;
int main(){
    scanf("%d", &n);
    memset(a, 0, sizeof a);
    a[x][y] = 1;
    for (int i = 2; i <= n * n; i ++ ) {
        int nx = x + d[cur][0], ny = y + d[cur][1];
        if (nx < 1 || nx > n || ny < 1 || ny > n || a[nx][ny]) {
            cur = (cur + 1) % 4;
            nx = x + d[cur][0], ny = y + d[cur][1];
        }
        x = nx, y = ny;
        a[x][y] = i;
    }
    for (int i = 1; i <= n; i ++ ) {
        for (int j = 1; j <= n; j ++ )
            printf(" %2d", a[i][j]);
        puts("");
    }
    return 0;
}

https://oj.since2018.top/d/contest_problems

原文