Submission #1290524

#TimeUsernameProblemLanguageResultExecution timeMemory
1290524baotoan655Robots (APIO13_robots)C++20
0 / 100
38 ms80180 KiB
#include <bits/stdc++.h>
#define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int N = 502;
int n, w, h;
string s[N];
pair<int, int> nxt[N][N][4];
int dp[9][9][N][N];
vector<pair<int, int>> q[N * N];
bool vis[9][9][N][N];

bool inBoard(int x, int y) {
    return 1 <= x && x <= h && 1 <= y && y <= w;
}

pair<int, int> go(int x, int y, int d) {
    while(true) {
        if(s[x][y] == 'A') d = (d + 3) % 4;
        if(s[x][y] == 'C') d = (d + 1) % 4;
        int nx = x + dx[d];
        int ny = y + dy[d];
        if(!inBoard(nx, ny) || s[nx][ny] == 'x') break;
        x = nx;
        y = ny;
    }
    return make_pair(x, y);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    file("A") else file("task");

    cin >> n >> w >> h;
    for(int i = 1; i <= h; ++i) {
        cin >> s[i];
        s[i] = ' ' + s[i];
    }
    memset(dp, 0x3f, sizeof dp);
    for(int i = 1; i <= h; ++i) {
        for(int j = 1; j <= w; ++j) if(s[i][j] != 'x') {
                for(int d = 0; d < 4; ++d) {
                    nxt[i][j][d] = go(i, j, d);
                }
            }
    }
    for(int i = 1; i <= h; ++i) {
        for(int j = 1; j <= w; ++j) {
            if('1' <= s[i][j] && s[i][j] <= '9') {
                dp[s[i][j] - '1'][s[i][j] - '1'][i][j] = 0;
            }
        }
    }
    for(int len = 1; len <= n; ++len) {
        for(int i = 0; ; ++i) {
            int j = i + len - 1;
            if(j >= n) break;
            for(int x = 1; x <= h; ++x) {
                for(int y = 1; y <= w; ++y) {
                    for(int k = i; k < j; ++k) {
                        dp[i][j][x][y] = min(dp[i][j][x][y], dp[i][k][x][y] + dp[k + 1][j][x][y]);
                    }
                    if(dp[i][j][x][y] < N * N) q[dp[i][j][x][y]].emplace_back(x, y);
                }
            }
            for(int _ = 0; _ < N * N; ++_) {
                while(!q[_].empty()) {
                    auto [x, y] = q[_].back();
                    q[_].pop_back();
                    if(vis[i][j][x][y]) continue;
                    vis[i][j][x][y] = true;
                    for(int d = 0; d < 4; ++d) {
                        auto [nx, ny] = nxt[x][y][d];
                        if(dp[i][j][x][y] + 1 < dp[i][j][nx][ny]) {
                            dp[i][j][nx][ny] = dp[i][j][x][y] + 1;
                            if(dp[i][j][nx][ny] < N * N) q[dp[i][j][nx][ny]].emplace_back(nx, ny);
                        }
                    }
                }
            }
        }
    }
//    cout << dp[2][2][1][7] << '\n';
    int ans = 1e9;
    for(int i = 1; i <= h; ++i) {
        for(int j = 1; j <= w; ++j) {
            ans = min(ans, dp[0][n - 1][i][j]);
        }
    }
    cout << ans << '\n';
    return 0;
}

Compilation message (stderr)

robots.cpp: In function 'int main()':
robots.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:36:5: note: in expansion of macro 'file'
   36 |     file("A") else file("task");
      |     ^~~~
robots.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:36:5: note: in expansion of macro 'file'
   36 |     file("A") else file("task");
      |     ^~~~
robots.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:36:20: note: in expansion of macro 'file'
   36 |     file("A") else file("task");
      |                    ^~~~
robots.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:36:20: note: in expansion of macro 'file'
   36 |     file("A") else file("task");
      |                    ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...