您的位置:首页 > 其它

走格子收获

2016-03-16 18:49 162 查看
eg:poj2632

code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a,b;
int queue[101][101];
int xx[4]= {-1,0,1,0};   //!!!!!!!注意这里方向的顺序必须和0、1、2、3保持一致!!!!WA了好多遍都不知道哪错了
int yy[4]= {0,1,0,-1};
struct spot
{
int x,y,d;
} robot[101];

bool forward(int s,int t)
{
int x=robot[s].x;
int y=robot[s].y;
int d=robot[s].d;
queue[x][y]=0;
for(int i=0; i<t; i++)
{
x+=xx[d];
y+=yy[d];
if(queue[x][y])
{
cout<<"Robot "<<s<<" crashes into robot "<<queue[x][y]<<endl;
return true;
}
if(x<1||x>a||y>b||y<1)
{
cout<<"Robot "<<s<<" crashes into the wall"<<endl;
return true;
}

}
robot[s].x=x;
robot[s].y=y;
queue[x][y]=s;
return false;
}

bool action(int s,char dir,int t)
{
switch (dir)
{
case 'F':
return forward(s,t);
case 'L':
robot[s].d=(robot[s].d-t%4+4)%4;    //是编号始终为0、1、2、3 对于绕桌子一圈编号问题一样
break;
case 'R':
robot[s].d=(robot[s].d+t%4)%4;
break;
}
return false;
}

int main()
{
int k;
int n,m,t,s;
int xi,yi;
char dir;
cin>>k;
bool f=false;
while(k--)
{
memset(queue,0,sizeof(queue));
memset(robot,0,sizeof(struct spot)*101);
cin>>a>>b>>n>>m;
for(int i=1; i<=n; i++)
{
cin>>xi>>yi>>dir;
queue[xi][yi]=i;             //这里注意,通过编号找位置,通过位置找编号
robot[i].x=xi;
robot[i].y=yi;
switch(dir)          //可以用0、1、2、3来表示方向
{                                                    N : 1
case 'N':                                W:0    +    E :2
robot[i].d=1;                            S : 3
break;
case 'S':
robot[i].d=3;
break;
case 'W':
robot[i].d=0;
break;
case 'E':
robot[i].d=2;
break;
}

}
f=false;
for(int i=0; i<m; i++)
{
cin>>s>>dir>>t;
if(!f) f=action(s,dir,t);
}
if(!f) cout<<"OK"<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: