Crashing Robots--POJ 2632

1、题目类型:模拟题。

2、解题思路:(1)将输入转换为Map[][],Loc[], Mov[]数组,分别表示在矩形中有哪些robot、robot的位置及当前运动方向、所需要完成的行动;(2)模拟输出各种情况。

3、注意事项:N、W、S、E这种顺时针的旋转顺序;对于求余数处理是否注意出现负数的情况;robot移动过程中,crashes robot和crashes wall 同时判断,crashes robot放在前面。

4、实现方法:

  
    
#include < iostream >
using namespace std;

struct TPoint{
int x,y;
int letter;
};
struct TMove{
int robot;
char action;
int repeat;
};

int Map[ 101 ][ 101 ];
int A,B,m,n,flag;
TPoint Loc[
101 ];
TMove Mov[
101 ];

void Do()
{
int i,j;
TMove tmp;
for (i = 1 ;i <= m;i ++ )
{
tmp.robot
= Mov[i].robot;
tmp.action
= Mov[i].action;
tmp.repeat
= Mov[i].repeat;
switch (tmp.action)
{
case ' F ' :
{
switch (Loc[tmp.robot].letter)
{
case 1 : // 'W'
{
for (j = Loc[tmp.robot].x - 1 ;j >= Loc[tmp.robot].x - tmp.repeat;j -- )
{
if (Map[j][Loc[tmp.robot].y] != 0 )
{
flag
= 0 ;
cout
<< " Robot " << tmp.robot << " crashes into robot " << Map[j][Loc[tmp.robot].y] << endl;
return ;
}

if (j < 1 )
{
flag
= 0 ;
cout
<< " Robot " << tmp.robot << " crashes into the wall " << endl;
return ;
}
}
Map[Loc[tmp.robot].x][Loc[tmp.robot].y]
= 0 ;
Loc[tmp.robot].x
= Loc[tmp.robot].x - tmp.repeat;
Map[Loc[tmp.robot].x][Loc[tmp.robot].y]
= tmp.robot;
break ;
}
case 3 : // 'E'
{
for (j = Loc[tmp.robot].x + 1 ;j <= Loc[tmp.robot].x + tmp.repeat;j ++ )
{
if (Map[j][Loc[tmp.robot].y] != 0 )
{
flag
= 0 ;
cout
<< " Robot " << tmp.robot << " crashes into robot " << Map[j][Loc[tmp.robot].y] << endl;
return ;
}
if (j > A)
{
flag
= 0 ;
cout
<< " Robot " << tmp.robot << " crashes into the wall " << endl;
return ;
}
}
Map[Loc[tmp.robot].x][Loc[tmp.robot].y]
= 0 ;
Loc[tmp.robot].x
= Loc[tmp.robot].x + tmp.repeat;
Map[Loc[tmp.robot].x][Loc[tmp.robot].y]
= tmp.robot;
break ;
}
case 0 : // 'N'
{
for (j = Loc[tmp.robot].y + 1 ;j <= Loc[tmp.robot].y + tmp.repeat;j ++ )
{
if (Map[Loc[tmp.robot].x][j] != 0 )
{
flag
= 0 ;
cout
<< " Robot " << tmp.robot << " crashes into robot " << Map[Loc[tmp.robot].x][j] << endl;
return ;
}
if (j > B)
{
flag
= 0 ;
cout
<< " Robot " << tmp.robot << " crashes into the wall " << endl;
return ;
}
}
Map[Loc[tmp.robot].x][Loc[tmp.robot].y]
= 0 ;
Loc[tmp.robot].y
= Loc[tmp.robot].y + tmp.repeat;
Map[Loc[tmp.robot].x][Loc[tmp.robot].y]
= tmp.robot;
break ;
}
case 2 : // 'S'
{
for (j = Loc[tmp.robot].y - 1 ;j >= Loc[tmp.robot].y - tmp.repeat;j -- )
{
if (Map[Loc[tmp.robot].x][j] != 0 )
{
flag
= 0 ;
cout
<< " Robot " << tmp.robot << " crashes into robot " << Map[Loc[tmp.robot].x][j] << endl;
return ;
}
if (j < 1 )
{
flag
= 0 ;
cout
<< " Robot " << tmp.robot << " crashes into the wall " << endl;
return ;
}
}
Map[Loc[tmp.robot].x][Loc[tmp.robot].y]
= 0 ;
Loc[tmp.robot].y
= Loc[tmp.robot].y - tmp.repeat;
Map[Loc[tmp.robot].x][Loc[tmp.robot].y]
= tmp.robot;
break ;
}
}
break ;
}
case ' L ' :
{
Loc[tmp.robot].letter
= (Loc[tmp.robot].letter + tmp.repeat) % 4 ;
break ;
}
case ' R ' :
{
Loc[tmp.robot].letter
= (Loc[tmp.robot].letter - tmp.repeat + 200 ) % 4 ;
break ;
}
}
}
}
int main()
{
int i,k;
char tmpchar;
cin
>> k;
while (k -- )
{
flag
= 1 ;
cin
>> A >> B;
cin
>> n >> m;
memset(Map,
0 , sizeof (Map));
memset(Loc,
0 , sizeof (Loc));
memset(Mov,
0 , sizeof (Mov));
for (i = 1 ;i <= n;i ++ )
{
cin
>> Loc[i].x >> Loc[i].y >> tmpchar;
if (tmpchar == ' N ' )
Loc[i].letter
= 0 ;
if (tmpchar == ' W ' )
Loc[i].letter
= 1 ;
if (tmpchar == ' S ' )
Loc[i].letter
= 2 ;
if (tmpchar == ' E ' )
Loc[i].letter
= 3 ;
Map[Loc[i].x][Loc[i].y]
= i;
}
for (i = 1 ;i <= m;i ++ )
{
cin
>> Mov[i].robot >> Mov[i].action >> Mov[i].repeat;
}
Do();
if (flag)
cout
<< " OK " << endl;
}
return 0 ;
}

 

你可能感兴趣的:(Crash)