走格子收获

来源:互联网 发布:思创医惠 人工智能 编辑:程序博客网 时间:2024/04/29 18:07
 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;    }}
0 0
原创粉丝点击