POJ 2632 Crashing Robots (模拟 坐标调整)(fflush导致RE)

题目链接:http://poj.org/problem?id=2632

先话说昨天顺利1Y之后,直到今天下午才再出题 TAT,真是刷题计划深似海,从此AC是路人- -

本来2632是道稍微恶心点的模拟,但毕竟是模拟,一般模拟都是只要示例过了基本就AC了,但这个题很特殊

我开始的时候一直跑不出测试示例,才发现题目中的插图和我构想的坐标系是不一样的(看来还不能轻易忽视掉插图啊)

这个题给出的坐标系是纵轴为y,横轴为x,y从下到上依次递增,x轴是从左到右递增

而我用的二维数组记录的地图,也就是说我的纵向是x轴而且从上到下递增,横向是y轴,从左到右递增

在我调了程序近一个小时之后才发现了这个问题。

然后突然懵了,不知道怎么实现了。突然的全盘否定让我瞬间没了思路

过了一段时间我才缓过来,然后打算偷梁换柱

因为这个题目只有在定义机器人的时候才用到了坐标,在指令上没有用到坐标

然后我就这样想,在过程处理上依然使用数组那套坐标系,而在录入保存的时候做些转换

scanf ("%d%d%s",&x,&y,to);

MAP[B - y][x - 1] = i;
rob[i].x = B - y;
rob[i].y = x - 1;

然后直接不用再管这套坐标系了

后面进展顺利,很快代码成形,调了几组数据全过,然后一交:RE

然后赶紧看数据范围,地图范围,发现我开的远远大于范围最大值,因为是模拟,又没有用指针,所以检查了几遍代码,着重检查数组下标

没有发现问题,跑了discuss里给的测试数据,全过,一交还是RE,卡了好久

终于没有办法了,只能乱改,直到AC.  (= = 乱改居然改AC了)

我改的是把录入字符改为录入字符串,只取字符串第一个字符(s[0]),目的是不用控制空格(不过还是RE)

然后因为已经不用空格了,我把我原来控制空格回车符的fflush(stdin)----清空输入流函数去掉

然后提交AC,至于为什么fflush导致RE,实在不明白,百度直说了fflush并非库函数,只是库函数的补充,部分linux会导致不兼容

虽然不是CE,但不排除是这方面的原因。不过fflush我在处理输入流的时候经常用,这是第一次出现问题。

fflush导致了RE,也算给自己一次警告吧。

总的来说,这个题也就是一个模拟,虽然麻烦点,但毕竟是个模拟题。

代码如下:

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

struct n
{
    int x,y;
    int to;
}rob[10000];

int MAP[150][150];
int A,B;

int mov (int no,int turn)
{
    int x = rob[no].x,y = rob[no].y;
    int to = rob[no].to;

    //printf ("!1!%d %d\n",x,y);

    MAP[x][y] = -1;
    switch (to)
    {
    case 1:
        if (turn == 1)
        {
            rob[no].to = 4;
            //y--;
        }else if (turn == 2)
        {
            rob[no].to = 2;
            //y++;
        }else if (turn == 3)
        {
            x--;
        }
        break;
    case 2:
        if (turn == 1)
        {
            rob[no].to = 1;
            //x--;
        }else if (turn == 2)
        {
            rob[no].to = 3;
            //x++;
        }else if (turn == 3)
        {
            y++;
        }
        break;
    case 3:
        if (turn == 1)
        {
            rob[no].to = 2;
            //y++;
        }else if (turn == 2)
        {
            rob[no].to = 4;
            //y--;
        }else if (turn == 3)
        {
            x++;
        }
        break;
    case 4:
        if (turn == 1)
        {
            rob[no].to = 3;
            //x++;
        }else if (turn == 2)
        {
            rob[no].to = 1;
            //x--;
        }else if (turn == 3)
        {
            y--;
        }
        break;
    }

    //printf ("!2!%d %d\n",x,y);
    if (x < 0 || y < 0 || x >= B || y >= A)
    {
        printf ("Robot %d crashes into the wall\n",no + 1);
        return 0;
    }

    if (MAP[x][y] != -1)
    {
        printf ("Robot %d crashes into robot %d\n",no + 1,MAP[x][y] + 1);
        return 0;
    }

    rob[no].x = x;
    rob[no].y = y;
    MAP[x][y] = no;
    return 1;
}

int main()
{
    //freopen ("1.txt","w",stdout);
    int N;
    int i,k;
    scanf ("%d",&N);

    while (N--)
    {
        int n,m;
        memset (MAP,-1,sizeof (MAP));
        memset (rob,0,sizeof (rob));
        scanf ("%d%d",&A,&B);
        scanf ("%d%d",&n,&m);

        for (i = 0;i < n;i++)
        {
            int x,y;
            char to[10];

            //fflush (stdin);
            scanf ("%d%d%s",&x,&y,to);

            MAP[B - y][x - 1] = i;
            rob[i].x = B - y;
            rob[i].y = x - 1;

            if (to[0] == 'N')
            {
                rob[i].to = 1;
            }else if (to[0] == 'E')
            {
                rob[i].to = 2;
            }else if (to[0] == 'S')
            {
                rob[i].to = 3;
            }else if (to[0] == 'W')
            {
                rob[i].to = 4;
            }
        }

        int tf = 1;

        for (i = 0;i < m;i++)
        {
            int no,lop,iact;
            char act[10];

            //fflush (stdin);
            scanf ("%d%s%d",&no,act,&lop);

            if (act[0] == 'L')
                iact = 1;
            else if (act[0] == 'R')
                iact = 2;
            else if (act[0] == 'F')
                iact = 3;

                for (k = 0;k < lop;k++)
                    if (tf)
                        tf = mov (no - 1,iact);
        }

        if (tf)
            puts ("OK");
    }

    return 0;
}

POJ 2632 Crashing Robots (模拟 坐标调整)(fflush导致RE)

时间: 2024-08-11 01:12:45

POJ 2632 Crashing Robots (模拟 坐标调整)(fflush导致RE)的相关文章

poj 2632 Crashing Robots, 模拟

点击打开链接 简单 模拟机器人的移动,发生碰撞时输出相应的信息. code #include <cstdio> #include <cstring> using namespace std; struct node{ int k; int s; } mtx[200][200]; struct node1{ int x, y; } rob[200]; int n, m; int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}; //N, E, S, W

poj -2632 Crashing Robots

http://poj.org/problem?id=2632 Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7470   Accepted: 3265 Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the

poj 2632 Crashing Robots(模拟)

链接:poj 2632 题意:在n*m的房间有num个机器,它们的坐标和方向已知,现给定一些指令及机器k执行的次数, L代表机器方向向左旋转90°,R代表机器方向向右旋转90°,F表示前进,每次前进一米 若前进时机器i撞到机器j,输出"Robot i crashes into robot j " 若机器走出了n*m的房间,输出"Robot i crashes into the wall " 当出现上述情况,只需输出第一次出现上述的情况 若所有指令执行完,所有机器都没

POJ 2632 Crashing Robots(模拟题)

Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7880   Accepted: 3429 Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destination

POJ 2632 Crashing Robots(较为繁琐的模拟)

题目链接:http://poj.org/problem?id=2632 题目大意:题意简单,N个机器人在一个A*B的网格上运动,告诉你机器人的起始位置和对它的具体操作,输出结果: 1.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.) 撞墙 2.Robot i crashes

Poj OpenJudge 百练 2632 Crashing Robots

1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7912   Accepted: 3441 Description In a modernized warehouse, robots are used to

POJ 1573 &amp; POJ 2632(两道有趣的Robot)

题目链接:POJ 1573 Robot Motion & POJ 2632 Crashing Robots [题意]题意就不说了,有兴趣从链接点进去看吧,就是机器人各种打扫房间,行驶指令. [思路]2632是一道纯模拟题,只要把题意读懂,就可以用代码模拟过程,只是写起来有点蛋疼,代码力还是欠缺啊.而1573感觉挺新奇的,我用的DFS来模拟,好久没有写DFS,一开始又把dir数组写错了,结果总是得出0. 下面贴代码,先是2632的AC代码: 1 /* 2 ** POJ 2632 Crashing

POJ 2632:Crashing Robots

Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8424   Accepted: 3648 Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destination

Crashing Robots(水题,模拟)

1020: Crashing Robots 时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte 总提交: 207            测试通过:101 描述 In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations withou