zoukankan      html  css  js  c++  java
  • poj 2632 模拟

    模拟题,注意两点

    1.坐标

    2.L,R后边的数字是向左或向右转几次

      1 #include <iostream>
    2 #include <stdio.h>
    3 #include <string.h>
    4 using namespace std;
    5 const int maxx=102;
    6 int loc[maxx][maxx],a,b; //loc 地图第ij个位置是第几个机器人
    7 char dir[maxx][maxx];
    8 struct OPE //操作
    9 {
    10 int ind;
    11 char op;
    12 int step;
    13 };
    14 OPE ope[maxx];
    15 struct ROBOT //机器人
    16 {
    17 int x,y;
    18 char di;
    19 };
    20 ROBOT ro[maxx];
    21 void updateloc(int x,int y,int nx,int ny)//更新地图位置
    22 {
    23 loc[nx][ny]=loc[x][y];
    24 loc[x][y]=0;
    25 }
    26 void updatero(int index,int x,int y,char dir)//更新机器人位置、方向
    27 {
    28 ro[index].x=x;
    29 ro[index].y=y;
    30 ro[index].di=dir;
    31 }
    32 bool crashwall(int x,int y) //是否撞墙
    33 {
    34 if((x>=1 && x<=b)&&(y>=1 && y<=a))return 0;
    35 else return 1;
    36 }
    37 int crash(int x,int y,int step,char dir) //撞到第几个机器人
    38 {
    39 int i,j;
    40 switch(dir)
    41 {
    42 case 'W':
    43 {
    44 for(i=x-1;i>=x-step;i--)
    45 {
    46 if(crashwall(i,y)) return -1;
    47 else if(loc[i][y]) return loc[i][y];
    48 }
    49 updatero(loc[x][y],x-step,y,'W');
    50 updateloc(x,y,x-step,y);
    51 break;
    52 }
    53 case 'E':
    54 {
    55 for(i=x+1;i<=x+step;i++)
    56 {
    57 if(crashwall(i,y)) return -1;
    58 else if(loc[i][y]) return loc[i][y];
    59 }
    60 updatero(loc[x][y],x+step,y,'E');
    61 updateloc(x,y,x+step,y);
    62 break;
    63 }
    64 case 'N':
    65 {
    66 for(i=y+1;i<=y+step;i++)
    67 {
    68 if(crashwall(x,i)) return -1;
    69 else if(loc[x][i]) return loc[x][i];
    70 }
    71 updatero(loc[x][y],x,y+step,'N');
    72 updateloc(x,y,x,y+step);
    73 break;
    74 }
    75 default:
    76 {
    77 for(i=y-1;i>=y-step;i--)
    78 {
    79 if(crashwall(x,i)) return -1;
    80 else if(loc[x][i]) return loc[x][i];
    81 }
    82 updatero(loc[x][y],x,y-step,'S');
    83 updateloc(x,y,x,y-step);
    84 }
    85 }
    86 return 0;
    87 }
    88
    89 int opero(int index,char op,int step)//操作机器人
    90 {
    91 switch(op)
    92 {
    93 case 'F':return crash(ro[index].x,ro[index].y,step,ro[index].di);
    94 case 'L':
    95 switch(ro[index].di)
    96 {
    97
    98 case 'W': if(step%4==1) ro[index].di='S';
    99 else if(step%4==2) ro[index].di='E';
    100 else if(step%4==3) ro[index].di='N';
    101 else ro[index].di='W';
    102 break;
    103 case 'E': if(step%4==1) ro[index].di='N';
    104 else if(step%4==2) ro[index].di='W';
    105 else if(step%4==3) ro[index].di='S';
    106 else ro[index].di='E';
    107 break;
    108 case 'N': if(step%4==1) ro[index].di='W';
    109 else if(step%4==2) ro[index].di='S';
    110 else if(step%4==3) ro[index].di='E';
    111 else ro[index].di='N';
    112 break;
    113 default: if(step%4==1) ro[index].di='E';
    114 else if(step%4==2) ro[index].di='N';
    115 else if(step%4==3) ro[index].di='W';
    116 else ro[index].di='S';
    117 break;
    118 }
    119 break;
    120 default :
    121 switch(ro[index].di)
    122 {
    123 case 'W': if(step%4==1) ro[index].di='N';
    124 else if(step%4==2) ro[index].di='E';
    125 else if(step%4==3) ro[index].di='S';
    126 else ro[index].di='W';
    127 break;
    128 case 'E': if(step%4==1) ro[index].di='S';
    129 else if(step%4==2) ro[index].di='W';
    130 else if(step%4==3) ro[index].di='N';
    131 else ro[index].di='E';
    132 break;
    133 case 'N': if(step%4==1) ro[index].di='E';
    134 else if(step%4==2) ro[index].di='S';
    135 else if(step%4==3) ro[index].di='W';
    136 else ro[index].di='N';
    137 break;
    138 default: if(step%4==1) ro[index].di='W';
    139 else if(step%4==2) ro[index].di='N';
    140 else if(step%4==3) ro[index].di='E';
    141 else ro[index].di='S';
    142 break;
    143 }
    144
    145 }
    146 return 0;
    147 }
    148
    149 int main()
    150 {
    151 int n,m,x,y,op,ans,flag,t,tt,i,ans2;
    152 char d;
    153 //freopen("in.txt","r",stdin);
    154 scanf("%d",&t);
    155 for(tt=1;tt<=t;tt++)
    156 {
    157 scanf("%d%d",&b,&a);
    158 flag=0;
    159 memset(dir,0,sizeof(dir));
    160 memset(loc,0,sizeof(loc));
    161 scanf("%d%d",&n,&m);
    162 for(i=1;i<=n;i++)
    163 {
    164 scanf("%d %d %c",&x,&y,&d);
    165 ro[i].x=x;
    166 ro[i].y=y;
    167 ro[i].di=d;
    168 loc[x][y]=i;
    169 dir[x][y]=d;
    170 }
    171 for(i=1;i<=m;i++)
    172 scanf("%d %c %d",&ope[i].ind,&ope[i].op,&ope[i].step);
    173 for(i=1;i<=m;i++)
    174 {
    175 ans=opero(ope[i].ind,ope[i].op,ope[i].step);
    176 if(ans){flag=1;ans2=ope[i].ind;break;}
    177 }
    178 if(flag)
    179 {
    180 if(ans==-1)
    181 printf("Robot %d crashes into the wall\n",ans2);
    182 else
    183 printf("Robot %d crashes into robot %d\n",ans2,ans);
    184 }
    185 else printf("OK\n");
    186 }
    187 return 0;
    188 }



  • 相关阅读:
    ES6相关概念及新增语法
    正则表达式
    递归
    高阶函数和闭包
    严格模式
    this指向
    递归
    严格模式
    函数内部的this指向
    函数的定义和调用
  • 原文地址:https://www.cnblogs.com/inpeace7/p/2434750.html
Copyright © 2011-2022 走看看