Submission #414597

# Submission time Handle Problem Language Result Execution time Memory
414597 2021-05-30T17:41:38 Z souvenir_vayne Patkice II (COCI21_patkice2) C++14
110 / 110
1487 ms 97024 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <chrono>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define fin cin
#define fout cout
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define all(x) x.begin(), x.end()
#define left vadia
#define lb lower_bound
#define right puta
using namespace std;
using namespace __gnu_pbds;
void setIO(string s) {
  ios_base::sync_with_stdio(0); cin.tie(0);
  freopen((s+".in").c_str(),"r",stdin);
  freopen((s+".out").c_str( ),"w",stdout);
}
typedef pair<ll, ll> pii;
typedef vector<vector<char>> mat;
typedef pair<int, string> pis;
const ll mod = 1e9+7, MAXN = 1e5;
typedef vector<int> vi;
typedef pair<int, pair<int, int>> piii;

char m[2005][2005];
int dist[2005][2005];
pii pai[2005][2005];

priority_queue<piii, vector<piii>, greater<piii>> fila;
pii start, e;

int x[] = {1, -1, 0, 0}, l, c;
int y[] = {0, 0, 1, -1};

int dijkstra() {
    for(int i = 0; i <= 2000; i++)
        for(int j = 0; j <= 2000; j++)
            dist[i][j] = INF;

    dist[start.f][start.s] = 0;
    fila.push({0, start});

    while(!fila.empty()) {

        start = fila.top().s;
        int aux = fila.top().f;
        fila.pop();

        //debug2(start.f, start.s);
        if(aux != dist[start.f][start.s])
            continue;

        for(int i = 0; i < 4; i++) {

            int ni = start.f + x[i], nj = start.s + y[i];
            if(ni < 0 or nj < 0 or ni >= l or nj >= c) continue;
           // debug2(ni, nj);
            if(!i && dist[ni][nj] > aux + (m[start.f][start.s] != 'v')) {
                dist[ni][nj] = aux + (m[start.f][start.s] != 'v');
                fila.push({aux + (m[start.f][start.s] != 'v'), {ni, nj}});
                pai[ni][nj] = {start.f, start.s};
            }
            if(i == 1 && dist[ni][nj] > aux + (m[start.f][start.s] != '^')) {
                dist[ni][nj] = aux + (m[start.f][start.s] != '^');
                fila.push({aux + (m[start.f][start.s] != '^'), {ni, nj}});
                pai[ni][nj] = {start.f, start.s};
            }
            if(i == 2 && dist[ni][nj] > aux + (m[start.f][start.s] != '>')) {
                dist[ni][nj] = aux + (m[start.f][start.s] != '>');
                fila.push({aux + (m[start.f][start.s] != '>'), {ni, nj}});
                pai[ni][nj] = {start.f, start.s};
            }
            if(i == 3 && dist[ni][nj] > aux + (m[start.f][start.s] != '<')) {
                dist[ni][nj] = aux + (m[start.f][start.s] != '<');
                fila.push({aux + (m[start.f][start.s] != '<'), {ni, nj}});
                pai[ni][nj] = {start.f, start.s};
            }
        }

    }
    return dist[e.f][e.s];


}

int main(){

    cin >> l >> c;
    for(int i = 0; i < l; i++)
        for(int j = 0; j < c; j++) {
            cin >> m[i][j];
            if(m[i][j] == 'o') start = {i, j};
            if(m[i][j] == 'x') e = {i, j};
        }

    pii kek = start;
    cout << dijkstra()-1 << endl;

    while(pai[e.f][e.s] != kek) {
        for(int i = 0; i < 4; i++) {
            int ni = e.f + x[i], nj = e.s + y[i];
            if(ni < 0 or nj < 0 or ni >= l or nj >= c or ni != pai[e.f][e.s].f or nj != pai[e.f][e.s].s) continue;
            if(!i)
                m[pai[e.f][e.s].f][pai[e.f][e.s].s] = '^';
            if(!i)
                m[pai[e.f][e.s].f][pai[e.f][e.s].s] = '^';
            if(i == 1)
                m[pai[e.f][e.s].f][pai[e.f][e.s].s] = 'v';
            if(i == 2)
                m[pai[e.f][e.s].f][pai[e.f][e.s].s] = '<';
            if(i == 3)
                m[pai[e.f][e.s].f][pai[e.f][e.s].s] = '>';
        }
        e = pai[e.f][e.s];
    }


    for(int i = 0; i < l; i++, cout << endl)
        for(int j = 0; j < c; j++)
            cout << m[i][j];
}

Compilation message

Main.cpp: In function 'void setIO(std::string)':
Main.cpp:26:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |   freopen((s+".in").c_str(),"r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:27:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |   freopen((s+".out").c_str( ),"w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15948 KB Output is correct
2 Correct 8 ms 16044 KB Output is correct
3 Correct 8 ms 16076 KB Output is correct
4 Correct 8 ms 16076 KB Output is correct
5 Correct 8 ms 15964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15948 KB Output is correct
2 Correct 8 ms 16044 KB Output is correct
3 Correct 8 ms 16076 KB Output is correct
4 Correct 8 ms 16076 KB Output is correct
5 Correct 8 ms 15964 KB Output is correct
6 Correct 170 ms 28332 KB Output is correct
7 Correct 231 ms 31952 KB Output is correct
8 Correct 424 ms 45212 KB Output is correct
9 Correct 867 ms 65480 KB Output is correct
10 Correct 1034 ms 80620 KB Output is correct
11 Correct 1487 ms 97024 KB Output is correct