Question link: http://poj.org/problem? Id = 2632
Let's start by saying that things went smoothly after 1Y yesterday and things were not published again until this afternoon. It's really like a sea of questions. From now on, the AC is a passer-by --
Originally, 2632 was a slightly disgusting simulation, but after all it was a simulation. Generally, the simulation is based on the AC as long as the example has passed, but this question is very special.
At the beginning, I couldn't run the test example until I found that the illustrations in the question were different from the coordinate system I conceived (it seems that the illustrations cannot be easily ignored)
The coordinate system given in this question is y for the vertical axis, x for the horizontal axis, and y for ascending order from bottom to top, x for ascending order from left to right
The two-dimensional array record map I used, that is to say, my portrait is the X axis and increases from top to bottom, the horizontal is the Y axis, and the left to right increments
I found this problem after I tuned the program for nearly an hour.
Then I suddenly got disconnected and didn't know how to implement it. A sudden total of negative results left me with no idea.
After a while, I waited, and plannedColumn stealing
This is because coordinates are used only when robots are defined, and coordinates are not used in commands.
Then I thought like this, in terms of process processing, I still use the array coordinate system, while doing some conversions during input and storage.
scanf ("%d%d%s",&x,&y,to);MAP[B - y][x - 1] = i;rob[i].x = B - y;rob[i].y = x - 1;
Then you don't have to worry about this coordinate system anymore.
Later, the code was quickly formed and several groups of data were fully transferred.
Then I checked the data range and MAP range, and found that I opened a far greater value than the maximum range. Because it was a simulation and no pointer, I checked the code several times and checked the array subscript.
No problem found. I ran all the test data from discuss. I got stuck for a long time.
Finally, there is no way to change it. You can only change it until the AC is changed. (=)
I changed itChange the input character to the input string. Only the first character of the string (s [0]) is used. The purpose is not to control spaces (but RE)
Then, because no space is needed, I removed the fflush (stdin) function that originally controls the space carriage return.
Then submit the AC. As for why fflush causes RE, I really don't understand. Baidu directly said that fflush is not a library function, but a supplement to the library function. Some linux will cause incompatibility.
Although it is not CE, it is not excluded from this. However, I often use fflush when processing Input streams. This is the first time that a problem occurs.
If fflush causes RE, you should give yourself a warning.
In general, this question is also a simulation. Although it is troublesome, It is a simulation question after all.
The Code is as follows:
#include
#include
#include
#include
struct n{ int x,y; int to;}rob[10000];int MAP[150][150];int A,B;int mov (int no,int turn){ int x = rob[no].x,y = rob[no].y; int to = rob[no].to; //printf ("!1!%d %d\n",x,y); MAP[x][y] = -1; switch (to) { case 1: if (turn == 1) { rob[no].to = 4; //y--; }else if (turn == 2) { rob[no].to = 2; //y++; }else if (turn == 3) { x--; } break; case 2: if (turn == 1) { rob[no].to = 1; //x--; }else if (turn == 2) { rob[no].to = 3; //x++; }else if (turn == 3) { y++; } break; case 3: if (turn == 1) { rob[no].to = 2; //y++; }else if (turn == 2) { rob[no].to = 4; //y--; }else if (turn == 3) { x++; } break; case 4: if (turn == 1) { rob[no].to = 3; //x++; }else if (turn == 2) { rob[no].to = 1; //x--; }else if (turn == 3) { y--; } break; } //printf ("!2!%d %d\n",x,y); if (x < 0 || y < 0 || x >= B || y >= A) { printf ("Robot %d crashes into the wall\n",no + 1); return 0; } if (MAP[x][y] != -1) { printf ("Robot %d crashes into robot %d\n",no + 1,MAP[x][y] + 1); return 0; } rob[no].x = x; rob[no].y = y; MAP[x][y] = no; return 1;}int main(){ //freopen ("1.txt","w",stdout); int N; int i,k; scanf ("%d",&N); while (N--) { int n,m; memset (MAP,-1,sizeof (MAP)); memset (rob,0,sizeof (rob)); scanf ("%d%d",&A,&B); scanf ("%d%d",&n,&m); for (i = 0;i < n;i++) { int x,y; char to[10]; //fflush (stdin); scanf ("%d%d%s",&x,&y,to); MAP[B - y][x - 1] = i; rob[i].x = B - y; rob[i].y = x - 1; if (to[0] == 'N') { rob[i].to = 1; }else if (to[0] == 'E') { rob[i].to = 2; }else if (to[0] == 'S') { rob[i].to = 3; }else if (to[0] == 'W') { rob[i].to = 4; } } int tf = 1; for (i = 0;i < m;i++) { int no,lop,iact; char act[10]; //fflush (stdin); scanf ("%d%s%d",&no,act,&lop); if (act[0] == 'L') iact = 1; else if (act[0] == 'R') iact = 2; else if (act[0] == 'F') iact = 3; for (k = 0;k < lop;k++) if (tf) tf = mov (no - 1,iact); } if (tf) puts ("OK"); } return 0;}