-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathntoj_426.cpp
More file actions
55 lines (47 loc) · 1.13 KB
/
ntoj_426.cpp
File metadata and controls
55 lines (47 loc) · 1.13 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
#include <bits/stdc++.h>
using namespace std;
typedef struct {
int x;
int y;
} position;
int main() {
vector<int> ns;
for (int n;cin>>n&&n!=0;) {
ns.push_back(n);
}
for (int i=0;i<ns.size();i++) {
const int n=ns[i];
int map[10][10];
position pos={0,0};
for (int step=1;step<=n*n;step++) {
map[pos.y][pos.x]=step;
if ((pos.x+pos.y)%2==0) {
if (pos.x==n-1) {
pos.y++;
} else if (pos.y==0) {
pos.x++;
} else {
pos.x++;
pos.y--;
}
} else {
if (pos.y==n-1) {
pos.x++;
} else if (pos.x==0) {
pos.y++;
} else {
pos.x--;
pos.y++;
}
}
}
cout<<"Case "<<i+1<<":"<<"\n";
for (int y=0;y<n;y++) {
for (int x=0;x<n;x++) {
printf("%3d",map[y][x]);
}
cout<<"\n";
}
}
return 0;
}