답안 #580945

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
580945 2022-06-22T06:41:45 Z 박상훈(#8359) 로봇 (APIO13_robots) C++17
0 / 100
1 ms 560 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
struct Vertex{
    int x, y, d;
    Vertex(){}
    Vertex(int _x, int _y, int _d): x(_x), y(_y), d(_d) {}
}to[505][505][4];
char a[505][505];
bool visited[505][505][4];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}, n, w, h;

void dfs(int x, int y, int d){
    visited[x][y][d] = 1;
    int nx, ny, nd = d;
    if (a[x][y]=='A') nd = (d+3)%4;
    if (a[x][y]=='C') nd = (d+1)%4;
    nx = x + dx[nd], ny = y + dy[nd];

    if (a[nx][ny]=='x') to[x][y][d] = Vertex(x, y, d);
    else if (visited[nx][ny][nd]) to[x][y][d] = to[nx][ny][nd];
    else{
        dfs(nx, ny, nd);
        to[x][y][d] = to[nx][ny][nd];
    }
}

void init(){
    for (int i=0;i<=h+1;i++) fill(a[i], a[i]+w+2, 'x');
    for (int i=1;i<=h;i++){
        scanf("%s", a[i]+1);
    }

    for (int i=1;i<=h;i++){
        for (int j=1;j<=w;j++){
            for (int d=0;d<4;d++) if (!visited[i][j][d]) dfs(i, j, d);
        }
    }

    //printf("%d %d %d\n", to[2][1][0].x, to[2][1][0].y, to[2][1][0].d);
}

int dist[45][505][505];

void bfs(int t){
    int sx = -1, sy = -1;
    for (int i=1;i<=h;i++){
        for (int j=1;j<=w;j++){
            if (a[i][j]=='1'+t) sx = i, sy = j;
            dist[t][i][j] = 1e9;
        }
    }

    dist[t][sx][sy] = 0;
    queue<pair<int, int>> q;
    q.emplace(sx, sy);
    while(!q.empty()){
        auto [x, y] = q.front(); q.pop();
        for (int d=0;d<4;d++){
            int nx = to[x][y][d].x, ny = to[x][y][d].y;
            //if (!nx) printf("%d %d %d\n", x, y, d);
            assert(nx);

            if (dist[t][nx][ny]!=1e9) continue;
            dist[t][nx][ny] = dist[t][x][y] + 1;
            q.emplace(nx, ny);
        }
    }
}

int main(){

    scanf("%d %d %d", &n, &w, &h);
    init();

    bfs(0);
    bfs(1);
    int ans = 1e9;
    for (int i=1;i<=h;i++){
        for (int j=1;j<=w;j++){
            //if (dist[0][i][j]==0 && dist[1][i][j]==0) printf("%d %d\n", i, j);
            ans = min(ans, dist[0][i][j] + dist[1][i][j]);
        }
    }
    printf("%d\n", ans==1e9?-1:ans);
    return 0;
}

Compilation message

robots.cpp: In function 'void init()':
robots.cpp:32:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         scanf("%s", a[i]+1);
      |         ~~~~~^~~~~~~~~~~~~~
robots.cpp: In function 'int main()':
robots.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |     scanf("%d %d %d", &n, &w, &h);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 560 KB Output is correct
5 Incorrect 1 ms 560 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 560 KB Output is correct
5 Incorrect 1 ms 560 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 560 KB Output is correct
5 Incorrect 1 ms 560 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 560 KB Output is correct
5 Incorrect 1 ms 560 KB Output isn't correct
6 Halted 0 ms 0 KB -