50123. Magic Square
難度:3.5/5 Used Time: 15:131
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#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int arr[1024][1024] = {0};
int n, k, x, y;
int cnt = 1;
int next_x(int s){
if(s == 0) return n - 1;
else return s - 1;
}
int next_y(int s){
if(s == n - 1) return 0;
else return s + 1;
}
int main(){
scanf("%d %d %d %d", &n, &k, &x, &y);
for(int i = 0; i < k; i++) x = next_y(x);
for(int i = 0; i < k; i++) y = next_x(y);
while(cnt <= n * n){
if(arr[next_x(x)][next_y(y)] != 0){
while(arr[x][y] != 0){
x = next_y(x);
}
}
else{
x = next_x(x), y = next_y(y);
}
arr[x][y] = cnt;
cnt++;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d%c", arr[i][j], (j == n - 1) ? '\n' : ' ');
}
}
}