HDU 4452 Running Rabbits [模拟]

#Description
一个N*N的grid,兔子1在(1,1),兔子2在(n, n)然后他们走啊走
三个参数
c s t
c表示方向 N E W S
s表示速度 每小时走几格
t表示每小时 向左转
然后问第k小时 两个兔子在哪?
规则有:
如果在某一个时刻,两个兔子同时在一个格子时,他们会互相会交换方向
如果他们撞墙了,回往回走,比如说他现在在1,1,速度是2,往N走,那么下一小时就会在3,1
如果他们相遇了,那么就不转了
注意是都不转,我就是这个WA好几次

if a rabbit is just about to turn left when two rabbit meet, he will forget to turn this time.
所以要先判断是否相遇,再判断转
#Algorithm
模拟
把题意理解清楚了就是模拟
#Code

#include <cstdio>
#include <iostream>
using namespace std;
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, 1, -1, 0};
int turn_around(const int &h)
{
  return (3 - h);
}
int turn_left(const int &h)
{
  if (h == 0) return 2;
  if (h == 1) return 0;
  if (h == 2) return 3;
  if (h == 3) return 1;
}
struct V
{
  int h, x, y, s, t;
  // N E W S h = 0 1 2 3
};
V a[2];
int main()
{
  //freopen("input.txt", "r", stdin);
  int n;
  for (;;)
  {
    cin >> n;
    if (n == 0) break;
    for (int i = 0; i < 2; i++)
    {
      char ch;
      cin >> ch;
      if (ch == 'N') a[i].h = 0; else
        if (ch == 'E') a[i].h = 1; else
          if (ch == 'W') a[i].h = 2; else
            a[i].h = 3;
      cin >> a[i].s >> a[i].t;
    }
    a[0].x = 1;
    a[0].y = 1;
    a[1].x = n;
    a[1].y = n;
    int kk;
    cin >> kk;
    for (int i = 0; i < kk; i++)
    {
      for (int j = 0; j < 2; j++)
      {
        for (int k = 0; k < a[j].s; k++)
        {
          a[j].x += dx[a[j].h];
          a[j].y += dy[a[j].h];
          if (a[j].x == 0)
          {
            a[j].x = 2;
            a[j].h = turn_around(a[j].h);
          }else if (a[j].x == n + 1)
          {
            a[j].x = n - 1;
            a[j].h = turn_around(a[j].h);
          }else if (a[j].y == 0)
          {
            a[j].y = 2;
            a[j].h = turn_around(a[j].h);
          }else if (a[j].y == n + 1)
          {
            a[j].y = n - 1;
            a[j].h = turn_around(a[j].h);
          }
        }
      }
      if (a[0].x == a[1].x && a[0].y == a[1].y)
      {
        int t = a[0].h;
        a[0].h = a[1].h;
        a[1].h = t;
      }else
      for (int j = 0; j < 2; j++)
        {
          if ((i + 1) % a[j].t == 0) a[j].h = turn_left(a[j].h);
        }
    }
    cout << a[0].x << ' ' << a[0].y << endl;
    cout << a[1].x << ' ' << a[1].y << endl;
  }
}

猜你喜欢

转载自blog.csdn.net/YYecust/article/details/50930308