当前位置: 代码迷 >> 综合 >> hdu - 4452 - Running Rabbits
  详细解决方案

hdu - 4452 - Running Rabbits

热度:90   发布时间:2024-01-10 13:50:30.0

题意:一个N*N的正方形田野分成1*1的格子,有两只兔子,Tom在左上角(1, 1),Jerry在右下角(N, N),它们可以沿东南西北4个方向走,但不可出格,各有各的速度,相遇时,两只兔子交换移动方向,碰边界时反向,各自还有自己的转左周期,周期一到即转左(但此时两只兔子相遇就不执行此周期转左),问K小时后两只兔子的坐标。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4452

——>>照着走就是了。

边界处理:若N = 5,

右或下出界:

x(或y)直接取模2*N-2

1234543212345432123……周期为2*5-2,存入数组a;

左或上出界:

x(或y) 变成1 - x(或1 - y)再取模2*N-2

……123454321234543212345周期为2*5-2,存入数组b;

开始没注意到speed( 1≤s<N),以为会来回改变方向多次,写多写错……其实挺水,这题……

#include <cstdio>
#include <algorithm>using namespace std;int N, a[50], b[50];void turn(char& c)      //转左
{switch(c){case 'E': c = 'N'; break;case 'S': c = 'E'; break;case 'W': c = 'S'; break;case 'N': c = 'W'; break;}
}
void moveto(char& c, int s, int& x, int& y)     //移步
{switch(c){case 'E':{y += s;if(y > N){y = a[y%(2*N-2)];c = 'W';}break;}case 'S':{x += s;if(x > N){x = a[x%(2*N-2)];c = 'N';}break;}case 'W':{y -= s;if(y < 1){y = b[(1-y)%(2*N-2)];c = 'E';}break;}case 'N':{x -= s;if(x < 1){x = b[(1-x)%(2*N-2)];c = 'S';}break;}}
}
int main()
{int K, T_s, T_t, J_s, J_t, i;char T_c, J_c;while(~scanf("%d", &N)){if(!N) return 0;scanf("\n%c%d%d", &T_c, &T_s, &T_t);scanf("\n%c%d%d", &J_c, &J_s, &J_t);scanf("%d", &K);int T_x = 1, T_y = 1, J_x = N, J_y = N;a[0] = 2;for(i = 1; i <= N; i++) a[i] = i;for(i = N+1; i <= 2*N-3; i++) a[i] = 2*N-i;b[0] = 1;for(i = 1; i <= N-1; i++) b[i] = i+1;for(i = N; i <= 2*N-3; i++) b[i] = 2*N-i-1;for(i = 1; i <= K; i++){moveto(T_c, T_s, T_x, T_y);moveto(J_c, J_s, J_x, J_y);if(T_x == J_x && T_y == J_y) swap(T_c, J_c);else{if(i % T_t == 0) turn(T_c);if(i % J_t == 0) turn(J_c);}}printf("%d %d\n", T_x, T_y);printf("%d %d\n", J_x, J_y);}return 0;
}


  相关解决方案