poj 2632 Crashing Robots

 

只是一道模拟题:

题意:题目就是给定你N个机器人在规定的范围内进行操作,问你是否走出范围或者是否相撞;(如果有多个这样的情况输出最开始的那种)

 

View Code
View Code 
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
class Node
{
public:
     int x ,y;
     char direct;    
};
char Direct[2][4]={{'N','E','S','W'},{'N','W','S','E'}};//定义左右旋转 
int N,M,A,B;
void Opt_Direct( Node robot[] , int num , char  direct , int n,int t )// 左右旋转 
{
    int i;
    for(  i = 0 ; i < 4 ; i ++ )
         if( robot[num].direct == Direct[t][i] )
             break;
    i += n;i %= 4;
    robot[num].direct = Direct[t][i];
}
bool judge_limit( Node robot[] ,int num )//判断是否撞墙 
{
    if( robot[num].x <=A && robot[num].x >=1  )
    {
       if( robot[num].y <=B &&robot[num].y >=1 )
          return true;
        else return false;
    }
    else
       return false;    
}
bool judge( Node robot[] ,int num )//判断走的是否合法 
{
    if( judge_limit( robot ,num ) )
    {
        for( int i = 1; i <= N ; i ++ )//判断是否相撞 
        {
           if( i != num )
           {
              if( robot[num].x == robot[i].x && robot[num].y == robot[i].y )
              {    
                  printf( "Robot %d crashes into robot %d\n",num , i );
                  return false;
              }
           }
        }
    }
    else
    {
        printf( "Robot %d crashes into the wall\n",num );
        return false;
    }
    return true;    
}
bool Opt_F( Node robot[] , int num ,  int n )//走的操作 
{
    switch( robot[num].direct )
    {
       case 'N':while( n-- )
               {
                  robot[num].y += 1;
                  if( judge( robot ,num )==false ) return false;    
               }
               break;
      case 'S':while( n-- )
               {
                  robot[num].y -=1;
                  if( judge( robot ,num ) ==false ) return false;    
               }
               break;
      case 'E':while( n-- )
               {
                  robot[num].x +=1;
                  if( judge( robot ,num )==false  ) return false;    
               }
               break;
      case 'W':while( n-- )
               {
                  robot[num].x -=1;
                  if( judge( robot ,num )==false  ) return false;    
               }
               break;           
    } 
    return true;    
}
bool Opt( Node robot[] , int num , char  direct , int &n )
{
   switch( direct )
   {
      case 'L':Opt_Direct( robot , num ,direct , n ,1);break;
      case 'R':Opt_Direct( robot , num ,direct , n ,0);break;
      case 'F':return Opt_F( robot , num , n );         
   }
   return true;    
}
int main(  )
{
    int Case,num[124],direct[124],n[124];
    Node  robot[124];
    while( scanf( "%d",&Case )==1 )
    {
        while( Case-- )
        {
            bool flag = true;
           scanf( "%d%d",&A,&B);
           scanf("%d %d",&N, &M);
           for( int i = 1; i <= N ; i ++ )
           {
               scanf( "%d %d %c",&robot[i].x,&robot[i].y,&robot[i].direct );        
           }
           for( int i = 0 ; i < M ; i ++ )
           {
                scanf( "%d %c %d",&num[i] ,&direct[i], &n[i] );        
           }
           for( int i = 0 ; i < M ; i++ )
           {
                flag = Opt( robot, num[i] ,direct[i] , n[i] );
                if( flag == false) break;
           }
           if( flag ) printf( "OK\n" );
        }     
    }
    //system( "pause" );
    return 0;
}

 

posted @ 2012-06-29 20:23  wutaoKeen  阅读(149)  评论(0编辑  收藏  举报