POJ 2632

#include <stdio.h>
#include <string.h>


typedef struct
{
    int nX;
    int nY;
    char cDir;
}Robot_S;


int main()
{
    int K;
    Robot_S astRobots[100 + 1];
    int anMap[100 + 1][100 + 1];
    char cDirection[] = "NESW";
    int anXYOffset[sizeof(cDirection)][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};

    scanf("%d", &K);

    while (K--)
    {
        int A, B;
        scanf("%d %d", &A, &B);

        int N, M;
        scanf("%d %d", &N, &M);

        memset(anMap, 0, sizeof(anMap));
        
        // Start from 1
        for (int i = 1; i <= N; i++)
        {
            scanf("%d %d %c", &astRobots[i].nX, &astRobots[i].nY, &astRobots[i].cDir);

            // Record robots
            anMap[astRobots[i].nY][astRobots[i].nX] = i;
            
            // Change direction format
            for (int j = 0; j < (int)sizeof(cDirection); j++)
            {
                if (astRobots[i].cDir == cDirection[j])
                {
                    astRobots[i].cDir = j;
                    break;
                }
            }

        }

        bool bCrashed = false;
        for (int i = 0; i < M; i++)
        {
            int nIndex;
            char cInstruction;
            int nRepeat;

            scanf("%d %c %d", &nIndex, &cInstruction, &nRepeat);

            // continue to read instruction
            if (bCrashed == true)
            {
                continue;
            }

            switch (cInstruction)
            {
                case 'R':
                    astRobots[nIndex].cDir = (astRobots[nIndex].cDir - nRepeat) % 4 + 4;
                    break;

                case 'L':
                    astRobots[nIndex].cDir = (astRobots[nIndex].cDir + nRepeat) % 4;
                    break;

                case 'F':
                {
                    int nX = astRobots[nIndex].nX;
                    int nY = astRobots[nIndex].nY;

                    int nXOffset = anXYOffset[astRobots[nIndex].cDir][0];
                    int nYOffset = anXYOffset[astRobots[nIndex].cDir][1];

                    while (nRepeat--)
                    {
                        anMap[astRobots[nIndex].nY][astRobots[nIndex].nX] = 0;

                        astRobots[nIndex].nX += nXOffset;
                        astRobots[nIndex].nY += nYOffset;

                        if (astRobots[nIndex].nX < 1 || astRobots[nIndex].nX > A ||
                            astRobots[nIndex].nY < 1 || astRobots[nIndex].nY > B)
                        {
                            printf("Robot %d crashes into the wall\n", nIndex);
                            bCrashed = true;
                            break;
                        }

                        if (anMap[astRobots[nIndex].nY][astRobots[nIndex].nX] != 0)
                        {
                            printf("Robot %d crashes into robot %d\n", nIndex, anMap[astRobots[nIndex].nY][astRobots[nIndex].nX]);
                            bCrashed = true;
                            break;
                        }
                        
                        anMap[astRobots[nIndex].nY][astRobots[nIndex].nX] = nIndex;
                    }
                    astRobots[nIndex].nX = nX;
                    astRobots[nIndex].nY = nY;

                    break;
                }

                default:
                    break;
            }
        }

        if (false == bCrashed)
        {
            printf("OK\n");
        }
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值