[NYOJ] [v1-33] Snake Matrix Fill

Problem Description

蛇形填数
Time limit: 3000 msMemory limit: 64 MB

Description

Fill 1,2.,n*n into an n*n square, and the filling must be snake-shaped. For example, when n=4 the square is: 10 11 12 1 9 16 13 2 815143 7654

Input

Directly input the dimension of the square, i.e. the value of n. (n<=100)

Output

The output is the snake-shaped square.

Sample Input

1
3

Sample Output

1
2
3
7 8 1
6 9 2
5 4 3

Problem Analysis

At first glance it seemed quite interesting
At a quick look it seemed pretty easy
On second thought it was a bit hard
After thinking it over carefully, it was actually fine

I used boundary checks plus a check for whether a cell had already been filled
Then I used a flag to mark the current filling direction

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<stdio.h>
#include<string.h>
int main() {
    int n;
    int map[120][120];
    while(scanf("%d",&n)!=EOF) {
        memset(map,0,sizeof(map));
        int y=1,x=n,cnt=0;
        int flag=1;
        while(cnt<n*n) {
            map[y][x]=++cnt;
            if(flag==1)
                y++;
            else if(flag==2)
                x--;
            else if(flag==3)
                y--;
            else if(flag==4)
                x++;
            if(map[y][x]||x<1||x>n||y<1||y>n) {
                if(flag==1) {
                    y--;
                    x--;
                    flag=2;
                } else if(flag==2) {
                    x++;
                    y--;
                    flag=3;
                } else if(flag==3) {
                    y++;
                    x++;
                    flag=4;
                } else if(flag==4) {
                    x--;
                    y++;
                    flag=1;
                }
            }
        }
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=n; j++) {
                printf("%d",map[i][j]);
                if(j!=n)
                    printf(" ");
            }
            printf("\n");
        }
    }
    return 0;
}

The reference solution’s idea is to loop over several rings
Then, through mathematical relationships, directly output
in the repeating order down, left, up, right
It is also very clever

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
int main() {
    int a,b,c,d,n,sum=1;
    int yi[101][101];
    scanf("%d",&n);
    for(a=0; a<=(n-1)/2; a++) {
        for(b=a; b<=n-a-1; b++)
            yi[b][n-a-1]=sum++;
        for(b=n-2-a; b>=a; b--)
            yi[n-a-1][b]=sum++;
        for(b=n-a-2; b>=a; b--)
            yi[b][a]=sum++;
        for(b=a+1; b<n-a-1; b++)
            yi[a][b]=sum++;
    }
    for(c=0; c<n; c++) {
        for(d=0; d<n; d++)
            printf("%d ",yi[c][d]);
        printf("\n");
    }
}

Problem link: NYOJ v1-33 Snake Filling

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...