很恶心的一道模拟题,最近状态不好,很烦,静不下心来做,留着吧,以后有机会再做吧

Crashing Robots

Description

In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occupy a circular floor space with a diameter of 1 meter. Assume there are N robots, numbered from 1 through N. You will get to know the position and orientation of each robot, and all the instructions, which are carefully (and mindlessly) followed by the robots. Instructions are processed in the order they come. No two robots move simultaneously; a robot always completes its move before the next one starts moving. 
A robot crashes with a wall if it attempts to move outside the area of the warehouse, and two robots crash with each other if they ever try to occupy the same spot.

Input

The first line of input is K, the number of test cases. Each test case starts with one line consisting of two integers, 1 <= A, B <= 100, giving the size of the warehouse in meters. A is the length in the EW-direction, and B in the NS-direction. 
The second line contains two integers, 1 <= N, M <= 100, denoting the numbers of robots and instructions respectively. 
Then follow N lines with two integers, 1 <= Xi <= A, 1 <= Yi <= B and one letter (N, S, E or W), giving the starting position and direction of each robot, in order from 1 through N. No two robots start at the same position. 
 
Figure 1: The starting positions of the robots in the sample warehouse

Finally there are M lines, giving the instructions in sequential order. 
An instruction has the following format: 
< robot #> < action> < repeat> 
Where is one of 
  • L: turn left 90 degrees, 
  • R: turn right 90 degrees, or 
  • F: move forward one meter,

and 1 <= < repeat> <= 100 is the number of times the robot should perform this single move.

Output

Output one line for each test case: 
  • Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.) 
  • Robot i crashes into robot j, if robots i and j crash, and i is the moving robot. 
  • OK, if no crashing occurs.

Only the first crash is to be reported.

Sample Input

4
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

Sample Output

Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2

代码如下:

/*#include<stdio.h>
int inf=999999;
struct node
{
    int x,y;
    int p;
} ro[111];
int dir(char s)
{
    switch(s)
    {
    case 'N':
        return 0;
    case 'W':
        return 1;
    case 'S':
        return 2;
    case 'E':
        return 3;
    }
}
int main()
{
    int k,n,m,i,j,a,b,Map[111][111];
    int f1,f3;
    char f2;
    scanf("%d",&k);
    while(k--)
    {
        scanf("%d%d",&a,&b);
        for(i=0; i<=b+1; i++)
            for(j=0; j<=a+1; j++)
                if(i==0||j==0||i==b+1||j==a+1)
                    Map[i][j]=inf;
                else  Map[i][j]=0;
        scanf("%d%d",&n,&m);
        for(i=1; i<=n; i++)
        {
            scanf("%d%d%*c%c",&ro[i].y,&ro[i].x,&f2);
            ro[i].p=dir(f2);
            Map[ro[i].x][ro[i].y]=i;
        }
        int flag=1;
        while(m--)
        {
            scanf("%d%*c%c%d",&f1,&f2,&f3);
            if(flag==1)
            {
                if(f2=='L')
                {
                    ro[f1].p=(ro[f1].p+4-f3%4)%4;
                }
                if(f2=='R')
                {
                    ro[f1].p=(ro[f1].p+f3)%4;
                }
                if(f2=='F'&&f3)
                {
                    Map[ro[f1].x][ro[f1].y]=0;
                    if(ro[f1].p==0)
                    {
                        for(i=ro[f1].x; i<=ro[f1].x+f3; i++)
                        {
                            if(Map[i][ro[f1].y]==inf)
                            {
                                printf("Robot %d crashes into the wall\n",f1);
                                flag=0;
                                break;
                            }
                            else if(Map[i][ro[f1].y]>0)
                            {
                                printf("Robot %d crashes into robot %d\n",f1,Map[i][ro[f1].y]);
                                flag=0;
                                break;
                            }
                        }
                    }
                    if(ro[f1].p==1)
                    {
                        for(i=ro[f1].y; i>=ro[f1].y-f3; i--)
                        {
                            if(Map[ro[f1].x][i]==inf)
                            {
                                printf("Robot %d crashes into the wall\n",f1);
                                flag=0;
                                break;
                            }
                            else if(Map[ro[f1].x][i]>0)
                            {
                                printf("Robot %d crashes into robot %d\n",f1,Map[i][ro[f1].y]);
                                flag=0;
                                break;
                            }
                        }
                    }
                    if(ro[f1].p==2)
                    {
                        for(i=ro[f1].x; i>=ro[f1].x-f3; i--)
                        {
                            if(Map[i][ro[f1].y]==inf)
                            {
                                printf("Robot %d crashes into the wall\n",f1);
                                flag=0;
                                break;
                            }
                            else if(Map[i][ro[f1].y]>0)
                            {
                                printf("Robot %d crashes into robot %d\n",f1,Map[i][ro[f1].y]);
                                flag=0;
                                break;
                            }
                        }
                    }
                    if(ro[f1].p==3)
                    {
                        for(i=ro[f1].y; i<=ro[f1].y+f3; i++)
                        {
                            if(Map[ro[f1].y][i]==inf)
                            {
                                printf("Robot %d crashes into the wall\n",f1);
                                flag=0;
                                break;
                            }
                            else if(Map[ro[f1].y][i]>0)
                            {
                                printf("Robot %d crashes into robot %d\n",f1,Map[i][ro[f1].y]);
                                flag=0;
                                break;
                            }
                        }
                    }
                }
                if(flag) printf("OK\n");
            }

        }
    }
}*/
#include <cstdio>

using namespace std;

const int N = 105;

struct rob
{
	int x, y, d;
}rob[N];

struct move
{
	int id, tap, times;
}ms[N];

int on[N][N];
int t, m, n, nums, moves;

void input()
{
	scanf("%d %d", &m, &n);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			on[i][j] = 0;
	char c;
	scanf("%d %d", &nums, &moves);
	for (int i = 1; i <= nums; ++i)
	{
		scanf("%d %d %c", &rob[i].y, &rob[i].x, &c);
		on[rob[i].x][rob[i].y] = i;
		switch(c)
		{
			case 'S':rob[i].d = 0;break;
			case 'E':rob[i].d = 1;break;
			case 'N':rob[i].d = 2;break;
			case 'W':rob[i].d = 3;break;
		}
	}
	for (int i = 1; i <= moves; ++i)
	{
		scanf("%d %c %d", &ms[i].id, &c, &ms[i].times);
		switch(c)
		{
			case 'L':ms[i].tap = 1;break;
			case 'R':ms[i].tap = 2;break;
			case 'F':ms[i].tap = 3;break;
		}
	}
}

void output(int one, int two, int w)
{
	if (w == 1)
		printf("Robot %d crashes into the wall\n", one);
	else
		printf("Robot %d crashes into robot %d\n", one, two);
}

bool move(int id, int inx, int iny, int times)
{
	int x = rob[id].x;
	int y = rob[id].y;
	on[x][y] = 0;
	for (int i = 0; i < times; ++i)
	{
		x += inx;
		y += iny;
		if (x < 1 || x > n || y < 1 || y > m)
		{
			output(id, 0, 1);
			return false;
		}
		if (on[x][y] != 0)
		{
			output(id, on[x][y], 2);
			return false;
		}
	}
	on[x][y] = id;
	rob[id].x = x;
	rob[id].y = y;
	return true;
}

bool change(int id, int tap, int times)
{
	switch(tap)
	{
		case 1:rob[id].d = (rob[id].d + times) % 4;break;
		case 2:rob[id].d = (rob[id].d + 3 * times) % 4;break;
		case 3:
			switch(rob[id].d)
			{
				case 0:
					if (!move(id, -1,  0, times))
						return false;
					break;
				case 1:
					if (!move(id,  0,  1, times))
						return false;
					break;
				case 2:
					if (!move(id,  1,  0, times))
						return false;
					break;
				case 3:
					if (!move(id,  0, -1, times))
						return false;
					break;
			}
			break;
	}
	return true;
}

void solve()
{
	for (int i = 1; i <= moves; ++i)
		if (!change(ms[i].id, ms[i].tap, ms[i].times))
			return;
	puts("OK");
}

int main()
{
	scanf("%d", &t);
	while (t--)
	{
		input();
		solve();
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值