| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
这么恶心的模拟题竟然一次ac了,弄了2个多小时。。。真无语啊(附ac代码)#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int out[2];
int row,col,flag;
typedef struct Instruction
{
int robot;
char action;
int repeat;
}instruction;
typedef struct Machine
{
int x;
int y;
int face; //N-0,E-1,S-2,W-3
int back;
int left;
int right;
}machine;
machine machines[105];
instruction instructions[105];
bool judge(machine a,machine b){
if((a.x != b.x )|| (a.y !=b.y)) //不相交返回false
return false;
else
return true;
}
bool judgeM(machine a){
if(a.x<1 || a.x >col || a.y<1 || a.y > row)
return true;
return false;
}
int process(int m,int n){
flag=0;
int i,j,k,robot,repeat;
char action;
machine tmp;
for(i=1;i<=m;i++){
robot=instructions[i].robot;
action=instructions[i].action;
repeat=instructions[i].repeat;
// printf("face:%d\n",machines[robot].face );
switch(action){
case'F':{
switch(machines[robot].face){
case 0:
for(j=1;j<=repeat;j++){
machines[robot].y++;
for(k=1;k<=n;k++){
if(k==robot) continue;
if(judge(machines[k],machines[robot]) ){
out[0]=robot,out[1]=k;
flag=2; return flag;
}
}
}
break;
case 1:
for(j=1;j<=repeat;j++){
machines[robot].x++;
for(k=1;k<=n;k++){
if(k==robot) continue;
if(judge(machines[k],machines[robot]) ){
out[0]=robot,out[1]=k;
flag=2; return flag;
}
}
}
//printf("x:%d\n",machines[robot].x);
break;
case 2:
for(j=1;j<=repeat;j++){
machines[robot].y--;
for(k=1;k<=n;k++){
if(k==robot) continue;
if(judge(machines[k],machines[robot]) ){
out[0]=robot,out[1]=k;
flag=2; return flag;
}
}
}
break;
case 3:
for(j=1;j<=repeat;j++){
machines[robot].x--;
for(k=1;k<=n;k++){
if(k==robot) continue;
if(judge(machines[k],machines[robot]) ){
out[0]=robot,out[1]=k;
flag=2; return flag;
}
}
}
break;
}
if(judgeM(machines[robot])){
flag=1;out[0]=robot;
return flag;
}
break;
}
case'L':
for(j=1;j<=repeat%4;j++){
tmp=machines[robot];
machines[robot].face=tmp.left;
machines[robot].back=tmp.right;
machines[robot].left=tmp.back;
machines[robot].right=tmp.face;
}
break;
case'R':
for(j=1;j<=repeat%4;j++){
tmp=machines[robot];
machines[robot].face=tmp.right;
machines[robot].back =tmp.left;
machines[robot].left =tmp.face;
machines[robot].right =tmp.back ;
}
break;
}
//printf("robot:%d action:%c face:%d x:%d y:%d\n",robot,action,machines[robot].face,machines[robot].x,machines[robot].y);
}
return flag;
}
int main()
{
int k,n,m,face,i;
char c;
scanf("%d",&k);
while(k--){
scanf("%d%d",&col,&row);
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
scanf("%d%d",&(machines[i].x),&(machines[i].y));
getchar();
c=getchar();
switch(c){
case'N': face=0; break;
case'E': face=1; break;
case'S': face=2; break;
case'W': face=3; break;
}
machines[i].face=face;
machines[i].back=(face+2)%4;
machines[i].left=(face+3)%4;
machines[i].right=(face+1)%4;
}
for(i=1;i<=m;i++){
scanf("%d",&(instructions[i].robot));
getchar();
c=getchar();
instructions[i].action=c;
scanf("%d",&(instructions[i].repeat));
}
process(m,n);
//printf("flag:%d\n",flag);
if(flag==1){
printf("Robot %d crashes into the wall\n",out[0]);
}else if(flag==2){
printf("Robot %d crashes into robot %d\n",out[0],out[1]);
}else{
printf("OK\n");
}
}
system("pause");
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator