POJ2632-Crashing Robots

全解题报告索引目录 -> 【北大ACM – POJ试题分类

转载请注明出处:http://exp-blog.com

-------------------------------------------------------------------------

 

 

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

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

 

注意事项:

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
 
 
//Memory Time 
//348K   16MS 

#include<iostream>
using namespace std;

int main(void)
{
	int cases;
	int a,b;  //A列数B行数
	int n,m;  //n : robot数, m :  指令数
	int x,y;  //robots坐标
	char dire;  //robots方向
	int rob[101],rep[101];   //rob:编号,rep:指令重复次数
	char act[101];           //act:行动指令

	bool flag=false;
	int i,k;

	int post[101][101];      //记录robot初始方向
	int num[101][101];       //记录robot的编号
	int xx[101],yy[101];     //记录robot的编号

	cin>>cases;
	while(cases--)
	{
		memset(post,-1,sizeof(post));
		memset(num,0,sizeof(num));
		memset(xx,0,sizeof(xx));
		memset(yy,0,sizeof(yy));

		cin>>a>>b;
		cin>>n>>m;

		/*Input the postion and direction of each robot*/
		for(i=1;i<=n;i++)
		{
			cin>>y>>x>>dire;   //使用cin函数时,空字符不会作为字符输入到char
			xx[i]=x;
			yy[i]=y;                  //编号记录(编号作为下标,通过编号反搜坐标)
			num[x][y]=i;              //编号记录(坐标作为下标,通过坐标反搜编号)
			if(dire=='S')             //方向记录(坐标作为下标,通过坐标反搜方向)
			    post[x][y]=0;        //0 or 360     【由于坐标系改变,注意上下颠倒,即N为南,S为北,但WE不变】
			else if(dire=='E')
				post[x][y]=1;        //90
			else if(dire=='N')
				post[x][y]=2;        //180
			else if(dire=='W')
				post[x][y]=3;        //270
		}                            //用0~3代替是为了减少运算次数

		/*Input < robot #> < action> < repeat>*/
		for(k=1;k<=m;k++)
			cin>>rob[k]>>act[k]>>rep[k];

		bool flag=false;
		int row,col;
		for(k=1;k<=m;k++)
		{
			row=xx[rob[k]];
			col=yy[rob[k]];

			if(act[k]=='L')       //【由于坐标系改变,注意转向相反:L转右,R转左】
			{
				rep[k]%=4;   //角度每转4次(360度)回到原位
				post[row][col] = ( post[row][col] + rep[k] ) % 4;
			}
			else if(act[k]=='R')
			{
				rep[k]%=4;
				post[row][col] = ( post[row][col] - rep[k] ) % 4;
				if(post[row][col]<0)   //注意余数为负的情况
					post[row][col]+=4;
			}
			else if(act[k]=='F')
			{
				if(post[row][col]==0)      //N方向移动判断
					for(i=1;i<=rep[k];i++)
					{
						if(num[row-i][col]) //如果该格存在编号(即存在另外的robot)
						{
							cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row-i][col]<<endl;
							flag=true;
							break;
						}
						if(row-i<1)  //判断是否撞墙
						{
							cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;
							flag=true;
							break;
						}
						if(i==rep[k])
						{   //移动中若无任何碰撞,则把robot原坐标记录的信息更新到新坐标
							post[row][col]=-1;   //原坐标方向重置
							num[row][col]=0;      //原坐标编号重置
							row-=rep[k];
							xx[rob[k]]-=rep[k];
							post[row][col]=0;    //新坐标方向更新
							num[row][col]=rob[k];   //新坐标编号更新
						}
					}
				else if(post[row][col]==1)  //E方向移动判断
					for(i=1;i<=rep[k];i++)
					{
						if(num[row][col+i])
						{
							cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row][col+i]<<endl;
							flag=true;
							break;
						}
						if(col+i>a)
						{
							cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;
							flag=true;
							break;
						}
						if(i==rep[k])
						{
							post[row][col]=-1;
							num[row][col]=0;
							col+=rep[k];
							yy[rob[k]]+=rep[k];
							post[row][col]=1;
							num[row][col]=rob[k];
						}
					}
				else if(post[row][col]==2)  //S方向移动判断
					for(i=1;i<=rep[k];i++)
					{
						if(num[row+i][col])
						{
							cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row+i][col]<<endl;
							flag=true;
							break;
						}
						if(row+i>b)
						{
							cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;
							flag=true;
							break;
						}
						if(i==rep[k])
						{
							post[row][col]=-1;
							num[row][col]=0;
							row+=rep[k];
							xx[rob[k]]+=rep[k];
							post[row][col]=2;
							num[row][col]=rob[k];
						}
					}
				else if(post[row][col]==3)  //W方向移动判断
					for(i=1;i<=rep[k];i++)
					{
						if(num[row][col-i])
						{
							cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row][col-i]<<endl;
							flag=true;
							break;
						}
						if(col-i<1)
						{
							cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;
							flag=true;
							break;
						}
						if(i==rep[k])
						{
							post[row][col]=-1;
							num[row][col]=0;
							col-=rep[k];
							yy[rob[k]]-=rep[k];
							post[row][col]=3;
							num[row][col]=rob[k];
						}
					}
			}
			if(flag)break;
		}
		if(!flag)
			cout<<"OK"<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值