# 【題解】ZeroJudge c292: APCS2017-0304-3數字龍捲風

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c292
【解題想法】觀察：一個方向重複使用幾次之後，就該變換前進方向呢？

```#include <iostream>
using namespace std;

int main(){
int N, nxt;
while (cin >> N) {
cin >> nxt;
int a[N][N];
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
cin >> a[i][j];
}
}
// 0代表左 、1代表上 、2代表右 、3代表下
int direction[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
int total_steps = N * N, steps = 1;
int repeat = 1, repeat_cnt = 0;
int r = N / 2, c = N / 2; // 從中心點出發

cout << a[r][ c];
while (steps < total_steps){
for (int i=0; i<repeat; i++){
r += direction[nxt][0];
c += direction[nxt][1];
cout << a[r][ c];
steps++;
if (steps == total_steps) break;
}

repeat_cnt++;
if (repeat_cnt % 2 == 0){
repeat++;
}
nxt = (nxt + 1) % 4;
}
cout << '\n';
}
return 0;
}
```

Python code

```while True:
try:
side = int(input())
nextmove = int(input())
lst = []
for a in range(side):
lst.append(list(map(int, input().split())))
mid = side // 2
i = mid
j = mid
step = 1
ansstr = str(lst[i][j])
for b in range((side-1)*2):
if nextmove == 0:
for k in range(step):
j -= 1
ansstr += str(lst[i][j])
elif nextmove == 1:
for k in range(step):
i -= 1
ansstr += str(lst[i][j])
elif nextmove == 2:
for k in range(step):
j += 1
ansstr += str(lst[i][j])
elif nextmove == 3:
for k in range(step):
i += 1
ansstr += str(lst[i][j])
if b % 2 == 1:
step += 1
nextmove = (nextmove+1) % 4

if nextmove == 0:
for k in range(step-1):
j -= 1
ansstr += str(lst[i][j])
elif nextmove == 1:
for k in range(step-1):
i -= 1
ansstr += str(lst[i][j])
elif nextmove == 2:
for k in range(step-1):
j += 1
ansstr += str(lst[i][j])
elif nextmove == 3:
for k in range(step-1):
i += 1
ansstr += str(lst[i][j])
print(ansstr)
except:
break
```