POJ 2632 Crashing Robots(大模拟)

题目链接

总是敲错,死调,还好打之前想的如何实现想的挺清楚,可是敲的时候老出错,外加x轴和Y轴有点混淆了,调试了40+比打代码的时间都长。。还好1Y。

  1 #include <stdio.h>

  2 #include <string.h>

  3 int p[101][101],o[101][101];

  4 int x[4] = {-1,0,1,0};

  5 int y[4] = {0,1,0,-1};

  6 int main()

  7 {

  8     int ti,n,m,a,b,i,j,k,sv,ev,nu,t,r,c,z,rr;

  9     int kei,kej;

 10     char la,di;

 11     scanf("%d",&t);

 12     while(t--)

 13     {

 14         memset(p,0,sizeof(p));

 15         memset(o,0,sizeof(o));

 16         scanf("%d%d",&a,&b);

 17         scanf("%d%d",&n,&m);

 18         for(i = 1; i <= n; i ++)

 19         {

 20             scanf("%d%*c%d%*c%c%*c",&sv,&ev,&di);

 21             p[sv][ev] = i;

 22             if(di == 'W')

 23                 o[sv][ev] = 0;

 24             else if(di == 'N')

 25                 o[sv][ev] = 1;

 26             else if(di == 'E')

 27                 o[sv][ev] = 2;

 28             else if(di == 'S')

 29                 o[sv][ev] = 3;

 30         }

 31         z = 0;

 32         for(i = 1; i <= m; i ++)

 33         {

 34             scanf("%d%*c%c%d%*c",&nu,&la,&ti);

 35             if(!z)

 36             {

 37                 for(j = 1; j <= a; j ++)

 38                     for(k = 1; k <= b; k ++)

 39                     {

 40                         if(p[j][k] == nu)

 41                         {

 42                             r = j;

 43                             c = k;

 44                         }

 45                     }

 46                 if(la == 'L')

 47                 {

 48                     for(k = 1; k <= ti; k ++)

 49                     {

 50                         if(o[r][c] == 0)

 51                             o[r][c] = 3;

 52                         else

 53                             o[r][c] --;

 54                     }

 55                 }

 56                 else if(la == 'R')

 57                 {

 58                     for(k = 1; k <= ti; k ++)

 59                     {

 60                         if(o[r][c] == 3)

 61                             o[r][c] = 0;

 62                         else

 63                             o[r][c] ++;

 64                     }

 65                 }

 66                 else if(la == 'F')

 67                 {

 68                     p[r][c] = 0;

 69                     rr = o[r][c];

 70                     for(k = 1; k <= ti&&!z; k ++)

 71                     {

 72                         if(r+x[rr] <= a&&r+x[rr]>=1&&c+y[rr]<=b&&c+y[rr]>=1)

 73                         {

 74                             if(p[r+x[rr]][c+y[rr]] == 0)

 75                             p[r+x[rr]][c+y[rr]] = nu;

 76                             else

 77                             {

 78                                 if(!z)

 79                                 {

 80                                     z = 2;

 81                                     kei = nu;

 82                                     kej = p[r+x[rr]][c+y[rr]];

 83                                 }

 84                             }

 85                             o[r+x[rr]][c+y[rr]] = o[r][c];

 86                             p[r][c] = 0;

 87                             r = r+x[rr];

 88                             c = c+y[rr];

 89                         }

 90                         else

 91                         {

 92                             if(!z)  z = 1;

 93                             kei = nu;

 94                             break;

 95                         }

 96                     }

 97                 }

 98             }

 99         }

100         if(!z)

101         printf("OK\n");

102         else if(z == 1)

103         printf("Robot %d crashes into the wall\n",kei);

104         else if(z == 2)

105         printf("Robot %d crashes into robot %d\n",kei,kej);

106     }

107     return 0;

108 }

你可能感兴趣的:(Crash)