답안 #581464

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
581464 2022-06-22T16:40:43 Z Soul234 Portal (COCI17_portal) C++14
120 / 120
49 ms 6860 KB
#include<bits/stdc++.h>
using namespace std;

void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h; if(sizeof...(t)) cerr << ", ";
    DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL

#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

void setIO(string NAME = "") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(NAME)) {
        freopen((NAME + ".inp").c_str(),"r",stdin);
        freopen((NAME + ".out").c_str(),"w",stdout);
    }
}

tcT> bool ckmin(T&a, const T&b) {
    return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
    return b > a ? a=b,1 : 0; }

const int MOD = 1e9 + 7;
const int dx[] = {-1, 0 ,1, 0};
const int dy[] = {0, -1, 0, 1};

const int MX = 505;
int N, M;
char A[MX][MX];
int dtnw[MX][MX], lef[MX][MX], rig[MX][MX], up[MX][MX], down[MX][MX], dis[MX][MX];
pi st, en;

bool valid(int x, int y) {
    return min(x,y) >= 1 && x <= N && y <= M && A[x][y] != '#';
}

#define d(x) dis[x.first][x.second]

void solve() {
    cin>>N>>M;
    FOR(i,1,N+1) FOR(j,1,M+1) {
        cin>>A[i][j];
        if(A[i][j] == 'C') st = {i,j};
        else if(A[i][j] == 'F') en = {i,j};
    }
    FOR(i,1,N+1) {
        lef[i][0] = 0;
        FOR(j,1,M+1) {
            if(A[i][j] == '#') lef[i][j] = j;
            else lef[i][j] = lef[i][j-1];
        }
        rig[i][M+1] = M+1;
        ROF(j,1,M+1) {
            if(A[i][j] == '#') rig[i][j] = j;
            else rig[i][j] = rig[i][j+1];
        }
    }
    FOR(j,1,M+1) {
        up[0][j] = 0;
        FOR(i,1,N+1) {
            if(A[i][j] == '#') up[i][j] = i;
            else up[i][j] = up[i-1][j];
        }
        down[N+1][j] = N+1;
        ROF(i,1,N+1) {
            if(A[i][j] == '#') down[i][j] = i;
            else down[i][j] = down[i+1][j];
        }
    }
    memset(dtnw, 0x3f, sizeof dtnw);
    memset(dis, 0x3f, sizeof dis);
    dis[st.first][st.second] = 0;
    FOR(i,1,N+1) FOR(j,1,M+1) if(A[i][j] != '#') {
        int L = j - lef[i][j], R = rig[i][j] - j, U = i - up[i][j], D = down[i][j] - i;
        ckmin(dtnw[i][j], min({L, R, U, D})-1);
    }
    pqg<pair<int,pi>> pq;
    pq.push({0, st});
    while(sz(pq)) {
        int du = pq.top().first;
        pi cur = pq.top().second;
        pq.pop();
        if(du > d(cur)) continue;
        F0R(i,4) {
            pi ncur = {cur.first + dx[i], cur.second + dy[i]};
            if(valid(ncur.first, ncur.second) && ckmin(d(ncur), du + 1)) pq.push({d(ncur), ncur});
        }
        int dv = du + dtnw[cur.first][cur.second] + 1;
        {
            pi ncur = {cur.first, lef[cur.first][cur.second] + 1};
            if(ckmin(d(ncur), dv)) pq.push({d(ncur), ncur});
        }
        {
            pi ncur = {cur.first, rig[cur.first][cur.second] - 1};
            if(ckmin(d(ncur), dv)) pq.push({d(ncur), ncur});
        }
        {
            pi ncur = {up[cur.first][cur.second]+1, cur.second};
            if(ckmin(d(ncur), dv)) pq.push({d(ncur), ncur});
        }
        {
            pi ncur = {down[cur.first][cur.second]-1, cur.second};
            if(ckmin(d(ncur), dv)) pq.push({d(ncur), ncur});
        }
    }
    if(d(en) >= MOD) cout << "nemoguce\n";
    else cout << d(en) << nl;
}

int main() {
    setIO();

    int t=1;
    //cin>>t;
    while(t-->0) {
        solve();
    }

    return 0;
}

Compilation message

portal.cpp: In function 'void setIO(std::string)':
portal.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen((NAME + ".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
portal.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen((NAME + ".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2388 KB Output is correct
2 Correct 1 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2376 KB Output is correct
2 Correct 1 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2388 KB Output is correct
2 Correct 1 ms 2388 KB Output is correct
3 Correct 1 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3988 KB Output is correct
2 Correct 2 ms 3196 KB Output is correct
3 Correct 5 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 3668 KB Output is correct
2 Correct 4 ms 4052 KB Output is correct
3 Correct 5 ms 3296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 5700 KB Output is correct
2 Correct 18 ms 4564 KB Output is correct
3 Correct 13 ms 4928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 6312 KB Output is correct
2 Correct 12 ms 6672 KB Output is correct
3 Correct 17 ms 4936 KB Output is correct
4 Correct 16 ms 5720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 6860 KB Output is correct
2 Correct 13 ms 6780 KB Output is correct
3 Correct 26 ms 6740 KB Output is correct
4 Correct 31 ms 6716 KB Output is correct