-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP5731.cpp
More file actions
82 lines (65 loc) · 1.36 KB
/
Copy pathP5731.cpp
File metadata and controls
82 lines (65 loc) · 1.36 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
# P5731 【深基5.习6】蛇形方阵
## 题目描述
给出一个不大于 $9$ 的正整数 $n$,输出 $n\times n$
的蛇形方阵。
从左上角填上 $1$ 开始,顺时针方向依次填入数字,如同样例所示。注意每个数字有都会占用 $3$ 个字符,前面使用空格补齐。
## 输入格式
输入一个正整数 $n$,含义如题所述。
## 输出格式
输出符合题目要求的蛇形矩阵。
## 输入输出样例 #1
### 输入 #1
```
4
```
### 输出 #1
```
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
```
## 说明/提示
数据保证,$1 \leq n \leq 9$。
*/
#include <iostream>
#include <cstring>
using namespace std;
int n, A[9][9], d = 0;
int Di[] = {0, 1, 0, -1};
int Dj[] = {1, 0, -1, 0};
bool iswall(int i, int j)
{
if (i < 0 || j < 0 || i >= n || j >= n || A[i][j] != 0)
return 1;
else
return 0;
}
int main()
{
cin >> n;
memset(A, 0, sizeof(A));
int i = 0, j = 0, x, y;
for (int k = 1; k <= n * n; k++)
{
A[i][j] = k;
x = i + Di[d];
y = j + Dj[d];
if (iswall(x, y))
{
d = (d + 1) % 4;
}
i = i + Di[d];
j = j + Dj[d];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%3d", A[i][j]);
}
printf("\n");
}
return 0;
}