題意: 有兩只兔子,一只在左上角,一只在右上角,兩只兔子有自己的移動速度(每小時),和初始移動方向。
現在有3種可能讓他們轉向:撞牆:移動過程中撞牆,掉頭走未完成的路。 相碰: 兩只兔子在K點整(即處理完一小時走的路後),在同一點,兔子A和兔子B的方向互相交換一下。 向左轉 : 兩只兔子有自己的轉向時間T,每隔T小時,它就會向左轉, 但是相碰的優先級高於它,相碰之後就不處理左轉問題了。
#include <iostream> #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <vector> #include <set> #include <queue> #include <stack> #include <climits>//形如INT_MAX一類的 #define MAX 100005 #define INF 0x7FFFFFFF #define REP(i,s,t) for(int i=(s);i<=(t);++i) #define ll long long #define mem(a,b) memset(a,b,sizeof(a)) #define mp(a,b) make_pair(a,b) #define L(x) x<<1 #define R(x) x<<1|1 # define eps 1e-5 //#pragma comment(linker, "/STACK:36777216") ///傳說中的外掛 using namespace std; int n; struct node { int x,y; int sp,turn; int dir; // 0,1,2,3 } tom,jer; int dirx[] = {-1,0,1,0}; int diry[] = {0,1,0,-1}; int judge(char c) { if(c == 'N') return 0; if(c == 'E') return 1; if(c == 'S') return 2; if(c == 'W') return 3; } void go(node &a,int d) { for(int i=0; i<d; i++) { if(a.x == 1 && a.dir == 0) { a.dir = 2; } if(a.x == n && a.dir == 2) { a.dir = 0; } if(a.y == 1 && a.dir == 3) { a.dir = 1; } if(a.y == n && a.dir == 1) { a.dir = 3; } a.x = a.x + dirx[a.dir]; a.y = a.y + diry[a.dir]; } } bool meet() { if(tom.x == jer.x && tom.y == jer.y) { swap(tom.dir,jer.dir); return true; } return false; } void move(node &a,int step,bool flag) { if(flag == 0) { if(step % a.turn == 0 && step != 0) a.dir --; if(a.dir < 0) a.dir = 3; } int dir = a.dir; if(dirx[dir] != 0) { int dx = abs(a.sp * dirx[dir]); go(a,dx); } if(diry[dir] != 0) { int dy = abs(a.sp * diry[dir]); go(a,dy); } } void solve(int h) { int step = 0; while(step < h) { bool flag = 0; if(meet()) flag = 1; //cout << step << endl; move(tom,step,flag); //cout << "tom: " << tom.x << ' ' << tom.y << endl; move(jer,step,flag); //cout << "jer: " << jer.x << ' ' << jer.y << endl; step ++; } } int main() { char c; int h; while(scanf("%d",&n) && n) { tom.x = 1; tom.y = 1; jer.x = n; jer.y = n; getchar(); cin >> c; scanf("%d%d",&tom.sp,&tom.turn); tom.dir = judge(c); cin >> c; scanf("%d%d",&jer.sp,&jer.turn); jer.dir = judge(c); scanf("%d",&h); solve(h); printf("%d %d\n",tom.x,tom.y); printf("%d %d\n",jer.x,jer.y); } return 0; } #include <iostream> #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <vector> #include <set> #include <queue> #include <stack> #include <climits>//形如INT_MAX一類的 #define MAX 100005 #define INF 0x7FFFFFFF #define REP(i,s,t) for(int i=(s);i<=(t);++i) #define ll long long #define mem(a,b) memset(a,b,sizeof(a)) #define mp(a,b) make_pair(a,b) #define L(x) x<<1 #define R(x) x<<1|1 # define eps 1e-5 //#pragma comment(linker, "/STACK:36777216") ///傳說中的外掛 using namespace std; int n; struct node { int x,y; int sp,turn; int dir; // 0,1,2,3 } tom,jer; int dirx[] = {-1,0,1,0}; int diry[] = {0,1,0,-1}; int judge(char c) { if(c == 'N') return 0; if(c == 'E') return 1; if(c == 'S') return 2; if(c == 'W') return 3; } void go(node &a,int d) { for(int i=0; i<d; i++) { if(a.x == 1 && a.dir == 0) { a.dir = 2; } if(a.x == n && a.dir == 2) { a.dir = 0; } if(a.y == 1 && a.dir == 3) { a.dir = 1; } if(a.y == n && a.dir == 1) { a.dir = 3; } a.x = a.x + dirx[a.dir]; a.y = a.y + diry[a.dir]; } } bool meet() { if(tom.x == jer.x && tom.y == jer.y) { swap(tom.dir,jer.dir); return true; } return false; } void move(node &a,int step,bool flag) { if(flag == 0) { if(step % a.turn == 0 && step != 0) a.dir --; if(a.dir < 0) a.dir = 3; } int dir = a.dir; if(dirx[dir] != 0) { int dx = abs(a.sp * dirx[dir]); go(a,dx); } if(diry[dir] != 0) { int dy = abs(a.sp * diry[dir]); go(a,dy); } } void solve(int h) { int step = 0; while(step < h) { bool flag = 0; if(meet()) flag = 1; //cout << step << endl; move(tom,step,flag); //cout << "tom: " << tom.x << ' ' << tom.y << endl; move(jer,step,flag); //cout << "jer: " << jer.x << ' ' << jer.y << endl; step ++; } } int main() { char c; int h; while(scanf("%d",&n) && n) { tom.x = 1; tom.y = 1; jer.x = n; jer.y = n; getchar(); cin >> c; scanf("%d%d",&tom.sp,&tom.turn); tom.dir = judge(c); cin >> c; scanf("%d%d",&jer.sp,&jer.turn); jer.dir = judge(c); scanf("%d",&h); solve(h); printf("%d %d\n",tom.x,tom.y); printf("%d %d\n",jer.x,jer.y); } return 0; }