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

题目中测试数据过来,但是一直WA,哪位大神麻烦帮忙看看哪里出问题了

Posted by wangtong at 2013-03-25 10:52:44 on Problem 2632
#include <cstdio>
#include <cstring>
const int MAX = 110;
int wh[MAX][MAX];//dr[i][j]表示在位置i,j上的机器人编号
int A,B;
char dr[MAX];//dr[i]为i号机器人的当前方向
char dd[] = {'N','E','S','W'};
struct location{
	int x;
	int y;
}robloc[MAX];//记录每个机器人当前位置

//rob机器人编号,face朝向,times次数
//go函数用来计算机器人是否可以朝前走times步
//成功返回0,撞墙-1,撞到其他机器人则放回走的步数(走了多少步后撞到其他人)
int go(int rob,char face,int times){
	int orgx = robloc[rob].x;
	int orgy = robloc[rob].y;
	int curx = orgx;
	int cury = orgy;
	for(int i=1;i<=times;i++){
		switch(face){
			case 'N':
				if(cury==B){
					return -1;
				}
				cury++;
				if(wh[curx][cury])
					return i;
				break;
			case 'S':
				if(cury==1)
					return -1;
				cury--;
				if(wh[curx][cury])
					return i;
				break;
			case 'W':
				if(curx==1)
					return -1;
				curx--;
				if(wh[curx][cury])
					return i;
				break;
			case 'E':
				if(curx==A)
					return -1;
				curx++;
				if(wh[curx][cury])
					return i;
				break;
		}
	}
	wh[curx][cury] = rob;
	robloc[rob].x = curx;
	robloc[rob].y = cury;
	wh[orgx][orgy] = 0;
	return 0;
}

//处理单条指令
//返回,与go函数相同
int process(int rob,char action,int times){
	switch(action){
		case 'F':   //朝前走,调用go()
			return go(rob,dr[rob],times);
		case 'L':   //左转,改变机器人方向
			if(times%4==0)
				return 0;
			switch(dr[rob]){
				case 'N':
					dr[rob] = dd[4-times%4];
					break;
				case 'E':
					dr[rob] = dd[(5-times%4)%4];
					break;
				case 'S':
					dr[rob] = dd[(6-times%4)%4];
					break;
				case 'W':
					dr[rob] = dd[(7-times%4)%4];
					break;
			}
			return 0;
		case 'R':
			switch(dr[rob]){
				case 'N':
					dr[rob] = dd[times%4];
					break;
				case 'E':
					dr[rob] = dd[(times+1)%4];
					break;
				case 'S':
					dr[rob] = dd[(times+2)%4];
					break;
				case 'W':
					dr[rob] = dd[(times+3)%4];
					break;
			}
			return 0;
	}
}
int main(void){
	int cs;
	scanf("%d",&cs);
	while(cs--){
		memset(wh,0,sizeof(wh));
		memset(dr,0,sizeof(dr));
		scanf("%d%d",&A,&B);
		int N,M;
		scanf("%d%d",&N,&M);
		int x,y;
		for(int i=1;i<=N;i++){
			scanf("%d%d",&x,&y);
			wh[x][y] = i;
			robloc[i].x = x;
			robloc[i].y = y;
			getchar();
			scanf("%c",&dr[i]);
		}
		int i;
		int rob;
		char action;
		int times;
		int flag=1;
		for(i=1;i<=M;i++){
			scanf("%d",&rob);
			getchar();
			scanf("%c",&action);
			scanf("%d",&times);
			int result = process(rob,action,times);
			if(result == 0)
				continue ;
			else if(result==-1){
				flag = 0;
				printf("Robot %d crashes into the wall\n",rob);
				break;
			}
			else{
				flag = 0;
				int x = robloc[rob].x;
				int y = robloc[rob].y;
				switch(dr[rob]){
					case 'N':
						y+=result;
						break;
					case 'S':
						y-=result;
						break;
					case 'W':
						x-=result;
						break;
					case 'E':
						x+=result;
						break;
				}
				printf("Robot %d crashes into robot %d\n",rob,wh[x][y]);
			}
		}
		if(flag)
			printf("OK\n");
		else{
			while(i<M){
				scanf("%d",&rob);
				getchar();
				scanf("%c",&action);
				scanf("%d",&times);
				i++;
			}
		}
	}
	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