poj 2632 Crashing Robots_模拟

做的差点想吐,调来调去,编译器都犯毛病,wlgq,幸好1a。

题意:给你机器人怎走的路线,碰撞就输出

#include <cstdlib>
#include <iostream>
#include<cstdio>
#include<cstring>
#define N 110
using namespace std;
struct Rob{
	int x,y,dire;//dire 1为e,2为s,3为w,4为n
}rob[N];
int n,m,a,b,map[N][N];

int setdire(char s){
	switch(s){
		case 'E':return 1;
		case 'S':return 2;
		case 'W':return 3;
		case 'N':return 4;
	}
}
void Turn(Rob &rb,char s){
	if(s=='R'){
		if(rb.dire==4)
		    rb.dire=1;
		else
			rb.dire+=1;
	}
	else{
		if(rb.dire==1)
		    rb.dire=4;
		else
			rb.dire-=1;
	}
}
void init(){
	int i,x,y;
	char tmp[3];
	memset(map,0,sizeof(map));
	for(i=1;i<=n;i++)
	{
		scanf("%d%d%s",&x,&y,tmp);
		map[x][y]=i;
		rob[i].x=x;
		rob[i].y=y;
		rob[i].dire=setdire(tmp[0]);
	}
}
void solve(){
	int i,j,t,num,x,y,tag=1,flag=1;
	char tmp;
	for(i=0;i<m;i++)
	{
		scanf("%d %c %d",&t,&tmp,&num);
		if(!flag)
			continue;
		if(tmp!='F')
		{
			for(j=0;j<num%4;j++)
				Turn(rob[t],tmp);
		}
		else{
			x=rob[t].x;
			y=rob[t].y;
			if(rob[t].dire==1){
				for(j=1;j<=num;j++){
					x=rob[t].x+j;
					if(x>a){
						printf("Robot %d crashes into the wall\n",t);
						tag=0;
						flag=0;
						break;
					}
					if(map[x][y]){
						printf("Robot %d crashes into robot %d\n",t,map[x][y]);
						tag=0;
						flag=0;
						break;
					}
				}
				map[rob[t].x][rob[t].y]=0;
				map[rob[t].x+num][rob[t].y]=t;
				rob[t].x+=num;
			}
			else if(rob[t].dire==2){
				for(j=1;j<=num;j++){
					y=rob[t].y-j;
					if(y<1){
						printf("Robot %d crashes into the wall\n",t);
						tag=0;
						flag=0;
						break;
					}
					if(map[x][y]){
						printf("Robot %d crashes into robot %d\n",t,map[x][y]);
						tag=0;
						flag=0;
						break;
					}
				}
				map[rob[t].x][rob[t].y]=0;
				map[rob[t].x][rob[t].y-num]=t;
				rob[t].y-=num;
			}
			else if(rob[t].dire==3){
				for(j=1;j<=num;j++){
					x=rob[t].x-j;
					if(x<1){
						printf("Robot %d crashes into the wall\n",t);
						tag=0;
						flag=0;
						break;
					}
					if(map[x][y]){
						printf("Robot %d crashes into robot %d\n",t,map[x][y]);
						tag=0;
						flag=0;
						break;
					}
				}
				map[rob[t].x][rob[t].y]=0;
				map[rob[t].x-num][rob[t].y]=t;
				rob[t].x-=num;
			}
			else if(rob[t].dire==4){
				for(j=1;j<=num;j++){
					y=rob[t].y+j;
					if(y>b){
						printf("Robot %d crashes into the wall\n",t);
						tag=0;
						flag=0;
						break;
					}
					if(map[rob[t].x][y]){
						printf("Robot %d crashes into robot %d\n",t,map[x][y]);
						tag=0;
						flag=0;
						break;
					}
				}
				map[rob[t].x][rob[t].y]=0;
				map[rob[t].x][rob[t].y+num]=t;
				rob[t].y+=num;
			}
		}
	}
	
	if(tag)
	    printf("OK\n");
}
int main(int argc, char *argv[])
{
	int t,i,j;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&a,&b);
		scanf("%d%d",&n,&m);
		init();
		solve();
	}
    system("PAUSE");
    return EXIT_SUCCESS;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值