답안 #42523

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
42523 2018-02-28T03:26:35 Z funcsr 로봇 (APIO13_robots) C++14
60 / 100
1198 ms 134364 KB
//#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstdio>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define pb push_back
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define INF 1000001919
#define _1 first
#define _2 second
const int DX[4] = {-1, 0, 1, 0};
const int DY[4] = {0, -1, 0, 1};

int N, W, H;
char A[500][500];
P dst[500][500][4];
int start;
inline void tle_check() {
  double passed = (double)(clock()-start) / (double)CLOCKS_PER_SEC;
  if (W > 300 && H > 300 && passed>=0.5) {
    //cout<<passed<<" passed\n";
    abort();
  }
}
P f(int x, int y, int d) {
  if (dst[x][y][d] != P(-2, -2)) return dst[x][y][d];

  tle_check();
  dst[x][y][d] = P(-1, -1);
  int nx = x+DX[d], ny = y+DY[d], nd = d;
  if (nx<0||nx>=W||ny<0||ny>=H||A[nx][ny]=='x') return dst[x][y][d] = P(x, y);
  if (A[nx][ny] == 'A') nd = (d+3)%4;
  if (A[nx][ny] == 'C') nd = (d+1)%4;
  return dst[x][y][d] = f(nx, ny, nd);
}

//const int MAX_D = 10*500*500;
//const int MAX_D = 10*1000;
int X[9], Y[9];
int dp[9][9][500][500];
vector<P> Q[10*500*500];
bool used[500][500];
void bfs(int D[500][500]) {
  rep(i, W) rep(j, H) used[i][j] = false;
  int MAX_D = 0;
  rep(x, W) rep(y, H) if (D[x][y] != INF) MAX_D = max(MAX_D, D[x][y]);
  assert(MAX_D <= 500*500);
  tle_check();
  rep(x, W) rep(y, H) if (D[x][y] != INF) Q[D[x][y]].pb(P(x, y));
  tle_check();
  //rep(x, W) rep(y, H) if (D[x][y] != INF)cout<<"("<<x<<","<<y<<")->"<<D[x][y]<<"\n";
  vector<P> q;
  int r = 0;
  while (true) {
    if (r > MAX_D && q.empty()) break;
    tle_check();
    vector<P> nq;
    if (r <= MAX_D) {
      for (P p : Q[r]) {
        int x = p._1, y = p._2;
        if (!used[x][y]) {
          used[x][y] = true;
          q.pb(P(x, y));
        }
      }
    }
    for (P p : q) {
      int x = p._1, y = p._2;
      rep(k, 4) {
        P np = dst[x][y][k];
        if (np._1 != -1 && !used[np._1][np._2]) {
          used[np._1][np._2] = true;
          D[np._1][np._2] = r+1;
          nq.pb(np);
        }
      }
    }
    q.clear();
    swap(q, nq);
    r++;
  }
  rep(i, MAX_D+1) Q[i].clear();
}
int main() {
  start = clock();
  scanf("%d %d %d", &N, &W, &H);
  getchar();
  rep(y, H) {
    rep(x, W) {
      A[x][y] = getchar();
      if ('1' <= A[x][y] && A[x][y] <= '9') X[A[x][y]-'1'] = x, Y[A[x][y]-'1'] = y;
    }
    getchar();
  }
  rep(x, W) rep(y, H) rep(d, 4) dst[x][y][d] = P(-2, -2);
  rep(x, W) rep(y, H) if (A[x][y] != 'x') rep(d, 4) f(x, y, d);
  //assert(W<500&&H<500);
  rep(i, N) rep(j, N) rep(x, W) rep(y, H) dp[i][j][x][y] = INF;
  rep(i, N) dp[i][i][X[i]][Y[i]] = 0;
  for (int len=1; len<=N; len++) {
    rep(l, N) {
      int r = l+len-1;
      if (r >= N) break;
      tle_check();
      rep(x, W) rep(y, H) for (int k=l; k<r; k++) {
        dp[l][r][x][y] = min(dp[l][r][x][y], dp[l][k][x][y]+dp[k+1][r][x][y]);
      }
      bfs(dp[l][r]);
    }
  }
  int m = INF;
  rep(x, W) rep(y, H) m = min(m, dp[0][N-1][x][y]);
  if (m == INF) m = -1;
  printf("%d\n", m);
  return 0;
}

Compilation message

robots.cpp: In function 'int main()':
robots.cpp:90:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &N, &W, &H);
                                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 59000 KB Output is correct
2 Correct 47 ms 59104 KB Output is correct
3 Correct 49 ms 59420 KB Output is correct
4 Correct 48 ms 59420 KB Output is correct
5 Correct 48 ms 59420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 59000 KB Output is correct
2 Correct 47 ms 59104 KB Output is correct
3 Correct 49 ms 59420 KB Output is correct
4 Correct 48 ms 59420 KB Output is correct
5 Correct 48 ms 59420 KB Output is correct
6 Correct 47 ms 59420 KB Output is correct
7 Correct 47 ms 59420 KB Output is correct
8 Correct 47 ms 59420 KB Output is correct
9 Correct 47 ms 59452 KB Output is correct
10 Correct 48 ms 59452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 59000 KB Output is correct
2 Correct 47 ms 59104 KB Output is correct
3 Correct 49 ms 59420 KB Output is correct
4 Correct 48 ms 59420 KB Output is correct
5 Correct 48 ms 59420 KB Output is correct
6 Correct 47 ms 59420 KB Output is correct
7 Correct 47 ms 59420 KB Output is correct
8 Correct 47 ms 59420 KB Output is correct
9 Correct 47 ms 59452 KB Output is correct
10 Correct 48 ms 59452 KB Output is correct
11 Correct 927 ms 114308 KB Output is correct
12 Correct 151 ms 114308 KB Output is correct
13 Correct 186 ms 114308 KB Output is correct
14 Correct 1198 ms 116712 KB Output is correct
15 Correct 421 ms 116712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 59000 KB Output is correct
2 Correct 47 ms 59104 KB Output is correct
3 Correct 49 ms 59420 KB Output is correct
4 Correct 48 ms 59420 KB Output is correct
5 Correct 48 ms 59420 KB Output is correct
6 Correct 47 ms 59420 KB Output is correct
7 Correct 47 ms 59420 KB Output is correct
8 Correct 47 ms 59420 KB Output is correct
9 Correct 47 ms 59452 KB Output is correct
10 Correct 48 ms 59452 KB Output is correct
11 Correct 927 ms 114308 KB Output is correct
12 Correct 151 ms 114308 KB Output is correct
13 Correct 186 ms 114308 KB Output is correct
14 Correct 1198 ms 116712 KB Output is correct
15 Correct 421 ms 116712 KB Output is correct
16 Runtime error 609 ms 134364 KB Execution killed with signal 11 (could be triggered by violating memory limits)
17 Halted 0 ms 0 KB -