Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

终于AC了,没按它的图建坐标系的话方向都要反过来,即左转就右转,右转就左转

Posted by jackfong at 2016-04-21 22:13:12 on Problem 2632
#include <iostream>  
#include <cstring> 
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
struct node{
	int x,y;
	int dir;
	node(int x1,int y1,int d):x(x1),y(y1),dir(d){
	}
	node(){
	}
};
struct onode{
	int in,t;
	char ch;
	onode(int i,int ti,char c){
		in=i;
		t=ti;
		ch=c;
	}
	onode(){
	}
}; 
int col,row,n,m;
int map[105][105];
node robot[105];
onode order[105];
int dir_[4][2]={-1,0,0,1,1,0,0,-1};

void init(){
	memset(map,0,sizeof(map));
	scanf("%d%d",&col,&row);
	scanf("%d%d",&n,&m);
	int x,y;
	char dir;
	for(int i=0;i<n;i++){
		scanf("%d %d %c",&y,&x,&dir);
		if(dir=='N')
			robot[i]=node(x-1,y-1,2);
		else if(dir=='E')
			robot[i]=node(x-1,y-1,1);
		else if(dir=='S')
			robot[i]=node(x-1,y-1,0);
		else
			robot[i]=node(x-1,y-1,3);
		map[x-1][y-1]=i+1;
	}
	for(int i=0;i<m;i++){
		scanf("%d %c %d",&x,&dir,&y);
		order[i]=onode(x,y,dir);
	//	cout<<order[i].in<<" "<<order[i].ch<<" "<<order[i].t<<endl;
	} 
}

bool F(int i){
	int x,y,dir,in,time;
	in=order[i].in-1;
	time=order[i].t;
	x=robot[in].x;
	y=robot[in].y;
	dir=robot[in].dir;
	map[x][y]=0;
	for(int j=0;j<time;j++){
		x+=dir_[dir][0];
		y+=dir_[dir][1];	
	//	printf("%d %d\n",x,y);
		if(x<0 || x>row-1 || y<0 || y>col-1){
			printf("Robot %d crashes into the wall\n",in+1);return false;
		}
		else if(map[x][y]){
			printf("Robot %d crashes into robot %d\n",in+1,map[x][y]);return false;
		}	
	}
	map[x][y]=in+1;
	robot[in]=node(x,y,dir);
	return true;
} 
void L(int i){
	int x,y,dir,in,time;
	in=order[i].in-1;
	time=order[i].t;
	x=robot[in].x;
	y=robot[in].y;
	dir=robot[in].dir;
	dir=(dir+time)%4;
	robot[in]=node(x,y,dir);	
}
void R(int i){
	int x,y,dir,in,time;
	in=order[i].in-1;
	time=order[i].t;
	x=robot[in].x;
	y=robot[in].y;
	dir=robot[in].dir;
	dir=((dir%4+4)-time%4)%4;
	robot[in]=node(x,y,dir);
}
void work(){
	bool flag=true;
	for(int i=0;i<m && flag;i++){
		switch(order[i].ch){
			case 'F':
				if(!F(i)){
					flag=false;
				}break;
			case 'L':L(i);break;
			case 'R':R(i);break;
		}
	}
	if(flag){
		printf("OK\n");
	}
} 

int main()
{
//	freopen("read.txt","r",stdin);
//	freopen("write.txt","w",stdout); 
	int T;
	cin>>T;
	while(T--)
	{
		init();
		work();		  
	}
//	fclose(stdin);
//	fclose(stdout);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator