Link: poj 2632
There are num machines in N * m rooms. Their coordinates and directions are known, and some commands and the number of K executions are given,
L indicates that the machine is rotated 90 ° to the left, r indicates that the machine is rotated 90 ° to the right, and F indicates that the machine is advancing one meter each time.
If machine I hits machine J when moving forward, the output is "robot I crashes into robot J"
If the machine goes out of the N * m room, the output is "robot I crashes into the wall"
When the above problem occurs, you only need to output the first case.
If all the commands have been executed, all the machines have not encountered the above situation. The output is "OK"
Train of Thought: After understanding the meaning of the question, you can simply simulate it.
Note: The rows and columns must be distinguished. Columns must be input first in the question and machine direction must be noted.
# Include <stdio. h> # include <string. h> int X [4] = {,-}, Y [4] = {, 0,-1}; // int num, m, n, vis [101] [101]; struct Stu {int R, C, Dir;} Rob [105]; int rob_go (int K, char C, int time) {int I, ri, CI; If (C = 'l') {time % = 4; rob [K]. dir = (Rob [K]. dir + 4-Time) % 4;} else if (C = 'R') {time % = 4; rob [K]. dir = (Rob [K]. dir + time) % 4;} else if (C = 'F') {rI = Rob [K]. r; CI = Rob [K]. c; vis [ri] [CI] = 0; while (Time --) {RI + = x [Rob [K]. dir]; Ci + = Y [Rob [K]. dir]; If (vis [ri] [CI]) {// if a machine is hit, the system returns the machine ID for (I = 1; I <= num; I ++) if (Ri = Rob [I]. R & amp; CI = Rob [I]. c) return I ;}}if (RI <1 | RI> N | CI <1 | Ci> m) Return-1; // use-1 to mark the machine going to the wall Rob [K]. R = Ri; rob [K]. C = CI; vis [ri] [CI] = 1;} return 0;} int main () {int T, I, INS, flag, K, time; char C; scanf ("% d", & T); While (t --) {scanf ("% d", & M, & N, & num, & INS); memset (VIS, 0, sizeof (VIS); // indicates whether the machine exists. 0 indicates that the machine does not exist for (I = 1; I <= num; I ++) {scanf ("% D % C", & Rob [I]. c, & Rob [I]. r, & C); vis [Rob [I]. r] [Rob [I]. c] = 1; // 1 indicates the machine if (C = 'n') ROB [I]. dir = 0; else if (C = 'E') ROB [I]. dir = 1; else if (C ='s ') ROB [I]. dir = 2; else if (C = 'W') ROB [I]. dir = 3;} flag = 0; I = 0; while (ins --) {scanf ("% d % C % d", & K, & C, & time ); if (! Flag) {flag = rob_go (K, C, time); If (flag &&! I) // if you hit another machine or go to the wall, you do not have to judge the other machine I = K; // record the machine for which the command was not safely executed for the first time} If (flag = 0) printf ("OK \ n"); else if (flag =-1) printf ("Robot % d crashes into the wall \ n", I); else printf ("Robot % d crashes into robot % d \ n", I, flag );} return 0 ;}
Poj 2632 crashing robots (simulation)