답안 #1104361

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1104361 2024-10-23T14:01:32 Z ALTAKEXE 로봇 (APIO13_robots) C++17
0 / 100
1 ms 4432 KB
#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define inf INT_MAX
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FAR(i, a, b) for (int i = (a); i >= (b); i--)
#define all(x) (x.begin(), x.end())
using namespace std;
const int MAXN = 505;
const int INF = 1e18;
pair<int, int> nx[MAXN][MAXN][4];
int dis[MAXN][MAXN][10];
int N, W, H, cnt;
char A[MAXN][MAXN];
pair<int, int> robots[10];
int main()
{
  cin >> N >> W >> H;
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j)
    {
      cin >> A[i][j];
      if ('0' < A[i][j] && A[i][j] <= '9')
        robots[A[i][j] - '1'] = make_pair(i, j), ++cnt;
    }

  for (int i = 1; i <= H; ++i)
  {
    nx[i][W + 1][0] = make_pair(i, W);
    for (int j = W; j > 0; --j)
    {
      if (A[i][j] == 'x')
        nx[i][j][0] = make_pair(i, j - 1);
      else
        nx[i][j][0] = nx[i][j + 1][0];
    }

    nx[i][0][1] = make_pair(i, 1);
    for (int j = 1; j <= W; ++j)
    {
      if (A[i][j] == 'x')
        nx[i][j][1] = make_pair(i, j + 1);
      else
        nx[i][j][1] = nx[i][j - 1][1];
    }
  }

  for (int j = 1; j <= W; ++j)
  {
    nx[H + 1][j][2] = make_pair(H, j);
    for (int i = H; i > 0; --i)
    {
      if (A[i][j] == 'x')
        nx[i][j][2] = make_pair(i - 1, j);
      else
        nx[i][j][2] = nx[i + 1][j][2];
    }

    nx[0][j][3] = make_pair(1, j);
    for (int i = 1; i <= H; ++i)
    {
      if (A[i][j] == 'x')
        nx[i][j][3] = make_pair(i + 1, j);
      else
        nx[i][j][3] = nx[i - 1][j][3];
    }
  }
  for (int k = 0; k < cnt; ++k)
    for (int i = 1; i <= H; ++i)
      for (int j = 1; j <= W; ++j)
        dis[i][j][k] = INF;

  for (int i = 0; i < cnt; ++i)
  {
    queue<pair<int, int>> q;
    q.push(robots[i]);
    dis[robots[i].first][robots[i].second][i] = 0;

    while (q.size())
    {
      pair<int, int> cur = q.front();
      q.pop();

      for (int k = 0; k < 4; ++k)
      {
        pair<int, int> p = nx[cur.first][cur.second][k];
        if (p.first == cur.first && p.second == cur.second)
          continue;
        if (dis[p.first][p.second][i] < dis[cur.first][cur.second][i] + 1)
          continue;
        dis[p.first][p.second][i] = dis[cur.first][cur.second][i] + 1;
        q.push(p);
      }
    }
  }

  int ans = INF;
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j)
    {
      ans = min(ans, dis[i][j][0] + dis[i][j][1]);
    }
  cout << (ans == INF ? -1 : ans) << '\n';
}

Compilation message

robots.cpp:11:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   11 | const int INF = 1e18;
      |                 ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Incorrect 1 ms 4432 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Incorrect 1 ms 4432 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Incorrect 1 ms 4432 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Incorrect 1 ms 4432 KB Output isn't correct
3 Halted 0 ms 0 KB -