poj 2632 模拟题

   题目不难。但是要做出来也要些耐心呀。考虑各种情况。

   这题是边输入边判断的,所以当能判断出结果的时候,要继续输入,把剩下的数据继续输入完,不然会影响下一次的输入。其他的就没什么要注意的了吧。该注意的我都在程序里注释了。感觉这题麻烦就麻烦再判断移动的move函数里面,都是重复的操作。应该可以优化吧。

   写完后我以为我的代码太长了。。。看了别人的,其实也差不多。呵呵。有空再想想怎么尽量优化吧。

/*
 * =====================================================================================
 *
 *       Filename:  main.cpp
 *
 *    Description:  poj 2632
 *
 *        Version:  1.0
 *        Created:  2012/5/7 13:10:38
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Jason Damon     
 *   Organization:  XD University
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <iostream>
#include <fstream>

using namespace std;
#define MAXN 101

int k,a,b,n,m,x,y;
int map[MAXN][MAXN];  //map[x][y]=i表示(x,y)存储的是第i个robot
char dire[]={'E','N','W','S'};//0->E 1->N
bool flag=false;  //用于判断是否已经成功判断
struct Robot{
    int x,y;
    int direct;  //定义方向,与数组dire[]对应
}robot[MAXN];

void turn(int pi,char dir,int times)  //转向函数
{
    int tem;
    tem=times%4;
    if(dir=='L')
    {
        robot[pi].direct=(robot[pi].direct+tem)%4;
    }
    else  //R
    {
        robot[pi].direct=(robot[pi].direct-tem+4)%4;  //注意这里有可能有负值,所以要加个4
    }
}

void move(int pi,char dir,int times)  //移动函数
{
    int di=robot[pi].direct;
    int x=robot[pi].x;
    int y=robot[pi].y;
    int i;
     if(di==0)
     {
         for(i=1; i<=times; i++)
         {
              if(x+1>a)
              {
                  printf("Robot %d crashes into the wall\n",pi);
                  flag=true;
                  break; 
              }
              else if(map[x+1][y])
              {
                  printf("Robot %d crashes into robot %d\n",pi,map[x+1][y]);
                  flag=true;
                  break; 
              }
              else  //移动
              {
                   map[x][y]=0;
                   x+=1;
                   map[x][y]=pi;
              }
         }
         //保存移动后的位置
         robot[pi].x=x;
         robot[pi].y=y;
     }
     else if(di==1)
     {
         for(i=1; i<=times; i++)
         {
              if(y+1>b)
              {
                  printf("Robot %d crashes into the wall\n",pi);
                   flag=true;
                  break; 
              }
              else if(map[x][y+1])
              {
                  printf("Robot %d crashes into robot %d\n",pi,map[x][y+1]);
                   flag=true;
                  break; 
              }
              else
              {
                   map[x][y]=0;
                   y+=1;
                   map[x][y]=pi;
              }
         }
         robot[pi].x=x;
         robot[pi].y=y;
     }
     else if(di==2)
     {
          for(i=1; i<=times; i++)
         {
              if(x-1<1)
              {
                  printf("Robot %d crashes into the wall\n",pi);
                   flag=true;
                  break; 
              }
              else if(map[x-1][y])
              {
                 printf("Robot %d crashes into robot %d\n",pi,map[x-1][y]);
                   flag=true;
                  break; 
              }
              else
              {
                   map[x][y]=0;
                   x-=1;
                   map[x][y]=pi;
              }
         }
         robot[pi].x=x;
         robot[pi].y=y;
     }
     else
     {
         for(i=1; i<=times; i++)
         {
              if(y-1<1)
              {
                  printf("Robot %d crashes into the wall\n",pi);
                   flag=true;
                  break; 
              }
              else if(map[x][y-1])
              {
                  printf("Robot %d crashes into robot %d\n",pi,map[x][y-1]);
                   flag=true;
                  break; 
              }
              else
              {
                   map[x][y]=0;
                   y-=1;
                   map[x][y]=pi;
              }
         }
         robot[pi].x=x;
         robot[pi].y=y;
     }
}

int main()
{
    int i,j,pi,times;
    char dir;
    freopen("in.txt","r",stdin);
    scanf("%d",&k);
    while(k--)
    {
        memset(map,0,sizeof(map));
        scanf("%d%d",&a,&b);
        scanf("%d%d",&n,&m);
        for(i=1; i<=n; i++)
        {
            scanf("%d%d %c",&robot[i].x,&robot[i].y,&dir);
            map[robot[i].x][robot[i].y]=i;
            switch(dir)
            {
            case 'E': robot[i].direct=0;break;
                case 'N': robot[i].direct=1;break;
                    case 'W': robot[i].direct=2;break;
                        case 'S': robot[i].direct=3;break;
            }
        }
        flag=false;
        for(i=1; i<=m; i++)
        {
            if(flag)  //成功判断后,要继续输入。
            {
                 scanf("%d %c%d",&pi,&dir,&times);
            }
            else
            {
                scanf("%d %c%d",&pi,&dir,&times);
                if(dir=='F')
                {
                    move(pi,dir,times);
                }
                else
                {
                    turn(pi,dir,times);
                }
            }    
        }
        if(!flag)
        {
            printf("OK\n");
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值