-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1022.cpp
More file actions
50 lines (40 loc) · 920 Bytes
/
1022.cpp
File metadata and controls
50 lines (40 loc) · 920 Bytes
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 <iostream>
using namespace std;
const int dx[4] = { 0, -1, 0, 1 };
const int dy[4] = { -1, 0, 1, 0 };
int main(){
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
int arr[50][5];
for (int i = 0; i <= (r2 - r1); i++){
for (int j = 0; j <= (c2 - c1); j++) {
arr[i][j] = 0;
}
}
int x = 0, y = 0, dir = 3;
int cnt = 0, n = 1, temp = 1;
while (!arr[r2 - r1][0] || !arr[0][0] || !arr[0][c2 - c1] || !arr[r2 - r1][c2 - c1]) {
if (y - r1 >= 0 && y - r1 <= (r2 - r1) && x - c1 >= 0 && x - c1 <= (c2 - c1)) {
arr[y - r1][x - c1] = n;
}
cnt++; n++;
x = x + dx[dir]; y = y + dy[dir];
if (cnt == temp) {
dir = (dir + 1) % 4;
if (dir == 1 || dir == 3) temp++;
cnt = 0;
}
}
cnt = 0;
while(n) {
n /= 10;
cnt += 1;
}
for (int i = 0; i <= (r2 - r1); i++) {
for (int j = 0; j <= (c2 - c1); j++) {
printf("%*d ", cnt, arr[i][j]);
}
cout << endl;
}
return 0;
}