• POJ2632Crashing Robots


     

    转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1299147655

     

    提示:简单的模拟而已。。。程序很长不是因为算法(根本就没算法= =)而是因为很多情况要考虑,要有耐心

    需要小心的是,当坐标系变换后,注意方向的改变规律

     

    注意事项:
    1、坐标系要改变为二维矩阵的形式,N、W、S、E的方向变化必须注意:改变坐标系后,N为南,S为北,WE不变,L转右,R转左,F不变;
    2、对于求余数处理是否注意出现负数的情况;
    3、robot移动过程中,crashes robot和crashes wall 同时判断,crashes robot放在前面。

    附加测试数据:
    Sample Input
    8
    5 4
    2 2
    1 1 E
    5 4 W
    1 F 7
    2 F 7
    5 4
    2 4
    1 1 E
    5 4 W
    1 F 3
    2 F 1
    1 L 1
    1 F 3
    5 4
    2 2
    1 1 E
    5 4 W
    1 L 96
    1 F 2
    5 4
    2 3
    1 1 E
    5 4 W
    1 F 4
    1 L 1
    1 F 20
    9 5
    5 19
    2 4 E
    4 3 N
    6 2 E
    9 5 S
    9 1 W
    4 F 1
    4 R 1
    4 F 6
    4 L 5
    4 F 3
    4 L 1
    5 R 1
    5 F 3
    5 L 1
    5 F 2
    5 L 1
    5 F 3
    5 R 5
    5 F 2
    5 R 1
    5 F 2
    4 F 2
    4 L 1
    4 F 3
    9 5
    2 6
    9 5 S
    9 1 W
    1 F 1
    1 R 1
    1 F 2
    2 F 2
    2 R 1
    2 F 3
    5 4
    2 2
    1 1 E
    5 4 W
    1 R 1
    1 F 2
    5 4
    2 2
    1 1 E
    5 4 W
    1 L 1
    1 F 2
    Sample Output
    Robot 1 crashes into the wall
    Robot 1 crashes into robot 2
    OK
    Robot 1 crashes into robot 2
    Robot 4 crashes into robot 5
    Robot 2 crashes into robot 1
    Robot 1 crashes into the wall
    OK
     
     
      1 //Memory Time 
    2 //348K 16MS
    3
    4 #include<iostream>
    5 using namespace std;
    6
    7 int main(void)
    8 {
    9 int cases;
    10 int a,b; //A列数B行数
    11 int n,m; //n : robot数, m : 指令数
    12 int x,y; //robots坐标
    13 char dire; //robots方向
    14 int rob[101],rep[101]; //rob:编号,rep:指令重复次数
    15 char act[101]; //act:行动指令
    16
    17 bool flag=false;
    18 int i,k;
    19
    20 int post[101][101]; //记录robot初始方向
    21 int num[101][101]; //记录robot的编号
    22 int xx[101],yy[101]; //记录robot的编号
    23
    24 cin>>cases;
    25 while(cases--)
    26 {
    27 memset(post,-1,sizeof(post));
    28 memset(num,0,sizeof(num));
    29 memset(xx,0,sizeof(xx));
    30 memset(yy,0,sizeof(yy));
    31
    32 cin>>a>>b;
    33 cin>>n>>m;
    34
    35 /*Input the postion and direction of each robot*/
    36 for(i=1;i<=n;i++)
    37 {
    38 cin>>y>>x>>dire; //使用cin函数时,空字符不会作为字符输入到char
    39 xx[i]=x;
    40 yy[i]=y; //编号记录(编号作为下标,通过编号反搜坐标)
    41 num[x][y]=i; //编号记录(坐标作为下标,通过坐标反搜编号)
    42 if(dire=='S') //方向记录(坐标作为下标,通过坐标反搜方向)
    43 post[x][y]=0; //0 or 360 【由于坐标系改变,注意上下颠倒,即N为南,S为北,但WE不变】
    44 else if(dire=='E')
    45 post[x][y]=1; //90
    46 else if(dire=='N')
    47 post[x][y]=2; //180
    48 else if(dire=='W')
    49 post[x][y]=3; //270
    50 } //用0~3代替是为了减少运算次数
    51
    52 /*Input < robot #> < action> < repeat>*/
    53 for(k=1;k<=m;k++)
    54 cin>>rob[k]>>act[k]>>rep[k];
    55
    56 bool flag=false;
    57 int row,col;
    58 for(k=1;k<=m;k++)
    59 {
    60 row=xx[rob[k]];
    61 col=yy[rob[k]];
    62
    63 if(act[k]=='L') //【由于坐标系改变,注意转向相反:L转右,R转左】
    64 {
    65 rep[k]%=4; //角度每转4次(360度)回到原位
    66 post[row][col] = ( post[row][col] + rep[k] ) % 4;
    67 }
    68 else if(act[k]=='R')
    69 {
    70 rep[k]%=4;
    71 post[row][col] = ( post[row][col] - rep[k] ) % 4;
    72 if(post[row][col]<0) //注意余数为负的情况
    73 post[row][col]+=4;
    74 }
    75 else if(act[k]=='F')
    76 {
    77 if(post[row][col]==0) //N方向移动判断
    78 for(i=1;i<=rep[k];i++)
    79 {
    80 if(num[row-i][col]) //如果该格存在编号(即存在另外的robot)
    81 {
    82 cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row-i][col]<<endl;
    83 flag=true;
    84 break;
    85 }
    86 if(row-i<1) //判断是否撞墙
    87 {
    88 cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;
    89 flag=true;
    90 break;
    91 }
    92 if(i==rep[k])
    93 { //移动中若无任何碰撞,则把robot原坐标记录的信息更新到新坐标
    94 post[row][col]=-1; //原坐标方向重置
    95 num[row][col]=0; //原坐标编号重置
    96 row-=rep[k];
    97 xx[rob[k]]-=rep[k];
    98 post[row][col]=0; //新坐标方向更新
    99 num[row][col]=rob[k]; //新坐标编号更新
    100 }
    101 }
    102 else if(post[row][col]==1) //E方向移动判断
    103 for(i=1;i<=rep[k];i++)
    104 {
    105 if(num[row][col+i])
    106 {
    107 cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row][col+i]<<endl;
    108 flag=true;
    109 break;
    110 }
    111 if(col+i>a)
    112 {
    113 cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;
    114 flag=true;
    115 break;
    116 }
    117 if(i==rep[k])
    118 {
    119 post[row][col]=-1;
    120 num[row][col]=0;
    121 col+=rep[k];
    122 yy[rob[k]]+=rep[k];
    123 post[row][col]=1;
    124 num[row][col]=rob[k];
    125 }
    126 }
    127 else if(post[row][col]==2) //S方向移动判断
    128 for(i=1;i<=rep[k];i++)
    129 {
    130 if(num[row+i][col])
    131 {
    132 cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row+i][col]<<endl;
    133 flag=true;
    134 break;
    135 }
    136 if(row+i>b)
    137 {
    138 cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;
    139 flag=true;
    140 break;
    141 }
    142 if(i==rep[k])
    143 {
    144 post[row][col]=-1;
    145 num[row][col]=0;
    146 row+=rep[k];
    147 xx[rob[k]]+=rep[k];
    148 post[row][col]=2;
    149 num[row][col]=rob[k];
    150 }
    151 }
    152 else if(post[row][col]==3) //W方向移动判断
    153 for(i=1;i<=rep[k];i++)
    154 {
    155 if(num[row][col-i])
    156 {
    157 cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row][col-i]<<endl;
    158 flag=true;
    159 break;
    160 }
    161 if(col-i<1)
    162 {
    163 cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;
    164 flag=true;
    165 break;
    166 }
    167 if(i==rep[k])
    168 {
    169 post[row][col]=-1;
    170 num[row][col]=0;
    171 col-=rep[k];
    172 yy[rob[k]]-=rep[k];
    173 post[row][col]=3;
    174 num[row][col]=rob[k];
    175 }
    176 }
    177 }
    178 if(flag)break;
    179 }
    180 if(!flag)
    181 cout<<"OK"<<endl;
    182 }
    183 return 0;
    184 }

  • 相关阅读:
    敏捷开发读后感
    软工第一次作业总结报告
    个人项目作业week5——敏捷开发方法读后感
    结对项目——电梯调度
    个人项目作业
    个人阅读作业3
    个人阅读作业2
    软件工程基础作业-个人项目代码复审
    电梯调度项目总结
    《移山之道》读后感
  • 原文地址:https://www.cnblogs.com/lyy289065406/p/2121383.html
Copyright © 2020-2023  润新知