题意:给定你两个机器人,其中一个是在左上角,一个是在右下角,每个机器人有一个初始方向和速度,每隔一段时间他们就要向左转,如果一个机器人要碰到墙了,他就会向后转继续走,如果两个机器人在某一整点在相同的格子里面,他们就不会向左转且互换方向!
解题思路:把机器人的所有信息封装起来,让后简单模拟!(函数用指针传递!)
解题代码:
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<iostream> #define max(a , b) (a)>(b)?(a):(b) #define min(a , b) (a)<(b)?(a):(b) #define LL long long using namespace std; int n ; struct point { int x, y ,sp ,j; char d; }; void changd(char &c) { if(c == 'N') c = 'W'; else if(c == 'W') c = 'S'; else if(c == 'S') c = 'E'; else if(c == 'E') c = 'N'; } int is(int x) { if(x >= 1 && x <= n) return 1; return 0 ; } void changs(struct point &t) { if(t.d == 'N') { int temp = t.x - t.sp; if(!is(temp)) { t.x = 1+ (t.sp-(t.x -1)); t.d ='S'; } else t.x = temp; } else if(t.d == 'W') { int temp = t.y - t.sp; if(!is(temp)) { t.y = 1+ (t.sp-(t.y -1)); t.d = 'E'; } else t.y = temp; } else if(t.d == 'E') { int temp = t.y + t.sp; if(!is(temp)) { t.y = n - (t.sp-(n - t.y)); t.d = 'W'; } else t.y = temp; } else if(t.d == 'S') { int temp = t.x + t.sp; if(!is(temp)) { t.x = n- (t.sp-(n - t.x)); t.d = 'N'; } else t.x = temp; } } int main() { while(scanf("%d",&n)!= EOF) { getchar(); struct point tom,jerry; if(n == 0 ) break; int k ; int i ; tom.x = tom.y = 1; jerry.x = jerry.y = n; scanf("%c %d %d\n",&tom.d,&tom.sp,&tom.j); scanf("%c %d %d\n",&jerry.d,&jerry.sp,&jerry.j); scanf("%d",&k); for(i = 1; i<= k;i ++) { if(tom.x == jerry.x && tom.y == jerry.y) { char temp = tom.d; tom.d = jerry.d; jerry.d = temp; } changs(tom); changs(jerry); if(!(tom.x == jerry.x && tom.y == jerry.y)) { if(i% tom.j == 0) changd(tom.d); if(i % jerry.j == 0) changd(jerry.d); } //printf ("%d %d %c %d %d %c\n",tom.x,tom.y,tom.d,jerry.x,jerry.y,tom.d); } printf ("%d %d\n%d %d\n",tom.x,tom.y,jerry.x,jerry.y); } return 0; }