N×N 棋盤,兩兔子初始位於 (1,1)、(N,N)。各給定初始方向(N/W/S/E)、每秒跳躍格數 s、每 t 秒左轉 90°。撞牆反彈(剩餘步數反方向完成)。第 k 秒兩兔同格 → 交換方向,且該秒不再執行左轉。模擬 T 秒後輸出雙方座標。
time % t == 0,方向左轉(順時針:N→W→S→E→N)。// 0:N(-1,0) 1:W(0,-1) 2:S(1,0) 3:E(0,1)
const int dir[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};
#include <bits/stdc++.h>
using namespace std;
struct Rabbit { int d, s, t, x, y; } r[2];
int N, T;
int dir_id(char c) { // N/W/S/E → 0/1/2/3
return c=='N'?0 : c=='W'?1 : c=='S'?2 : 3;
}
void jump(int i) {
int nx = r[i].x + r[i].s * dir[r[i].d][0];
int ny = r[i].y + r[i].s * dir[r[i].d][1];
if (1 <= nx && nx <= N && 1 <= ny && ny <= N) { // 未撞牆
r[i].x = nx; r[i].y = ny;
} else { // 撞牆反彈
if (r[i].d == 0) r[i].x += r[i].s - 2*(r[i].x - 1);
else if (r[i].d == 1) r[i].y += r[i].s - 2*(r[i].y - 1);
else if (r[i].d == 2) r[i].x -= r[i].s - 2*(N - r[i].x);
else r[i].y -= r[i].s - 2*(N - r[i].y);
r[i].d = (r[i].d + 2) % 4; // 反向
}
}
int main() {
while (cin >> N, N) {
char ch; cin >> ch >> r[0].s >> r[0].t; r[0].d = dir_id(ch);
cin >> ch >> r[1].s >> r[1].t; r[1].d = dir_id(ch);
r[0].x = r[0].y = 1;
r[1].x = r[1].y = N;
cin >> T;
for (int sec=1; sec<=T; ++sec) {
jump(0); jump(1);
if (r[0].x == r[1].x && r[0].y == r[1].y) {
swap(r[0].d, r[1].d); // 碰撞交換方向,不再左轉
} else {
if (sec % r[0].t == 0) r[0].d = (r[0].d + 1) % 4;
if (sec % r[1].t == 0) r[1].d = (r[1].d + 1) % 4;
}
}
cout << r[0].x << ' ' << r[0].y << '\n';
cout << r[1].x << ' ' << r[1].y << '\n';
}
return 0;
}
N=0 結束)。