Cod sursa(job #2021687)

Utilizator OldpugAlex Ionescu Oldpug Data 14 septembrie 2017 12:03:47
Problema Generare de permutari Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <cstdio>
#include <queue>

struct Point
{
  int X;
  int Y;
};

Point dir[4];
int map[175][175];

int main()
{
  freopen("alee.in", "r", stdin);
  freopen("alee.out", "w", stdout);

  dir[0].X = -1;
  dir[1].Y = 1;
  dir[2].X = 1;
  dir[3].Y = -1;

  int mapSize;
  std::cin >> mapSize;
  for (int i = 0; i < mapSize * mapSize; ++i)
    *(map + i) = 2147483647;

  Point entry, exit;
  std::cin >> exit.Y;
  for (int i = 0; i < exit.Y; ++i)
  {
    std::cin >> temp.X >> temp.Y;
    map[temp.X][temp.Y] = -1;
  }

  std::cin >> entry.X >> entry.Y >> exit.X >> exit.Y;
  map[entry.X][entry.Y] = 0;

  queue<Point> que;
  que.push(entry);

  while (!que.empty())
  {
    auto crr = que.front();
    que.pop();

    for (int i = 0; i < 4; ++i)
    {
      auto crrVal = map[crr.X][crr.Y] + 1;
      auto temp = crr;
      temp.X += dir[i].X;
      temp.Y += dir[i].Y;

      if (temp.X >= 0 && temp.Y >= 0 && temp.X < mapSize && temp.Y < mapSize && map[temp.X][temp.Y] > map[temp.X][temp.Y] > crrVal)
      {
        map[temp.X][temp.Y] = crrVal;
        que.push(temp);
      }
    }
  }

  std::cout << map[exit.X][exit.Y];
}