Submission #1279367

#TimeUsernameProblemLanguageResultExecution timeMemory
1279367shidou26Nautilus (BOI19_nautilus)C++20
66 / 100
26 ms1340 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef KURUMI
    #include "algo/debug.h"
#endif

#define endl '\n'
#define fi first
#define se second
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
#define filter(v) v.resize(unique(all(v)) - v.begin())
#define dbg(x) "[" #x << " = " << x << "]"

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T1, typename T2> T2 rand(T1 l, T2 r) {
    return uniform_int_distribution<T2>(l, r)(rng);
}
template<typename T1, typename T2> T2 wrand(T1 l, T2 r, int seed) {
    if(seed == 0) return rand(l, r);
    else return (seed > 0 ? max(rand(l, r), wrand(l, r, seed - 1)) : min(rand(l, r), wrand(l, r, seed + 1)));
}

template<typename T> bool maximize(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }else return false;
}
template<typename T> bool minimize(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }else return false;
}

typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tp3;

const int N = 5e2 + 3;
const char BLOCKED = '#';

int n, m, k;
char d[N];
char a[N][N];

void input() {
    cin >> n >> m >> k;

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }

    for(int i = 1; i <= k; i++) {
        cin >> d[i];
    }
}

namespace subtask_2 {
    bool approve() {
        return max({n, m, k}) <= 1e2;
    }

    const int N = 1e2 + 3;
    const int dx[] = {1, 0, -1, 0},
              dy[] = {0, 1, 0, -1};

    void execute() {
        map<char, int> direction;
        direction['S'] = 0;
        direction['E'] = 1;
        direction['N'] = 2;
        direction['W'] = 3;

        vector<vector<vector<bool>>> vis(k + 1, vector<vector<bool>>(n + 1, vector<bool>(m + 1)));
        queue<tp3> q;

        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j++) {
                if(a[i][j] == BLOCKED) continue;
                vis[0][i][j] = true;
                q.emplace(0, i, j);
            }
        }

        int counter = 0;
        while(!q.empty()) {
            int t, u, v; tie(t, u, v) = q.front(); q.pop();
//            debug("State", t, u, v);

            if(t == k) {
                counter++;
//                cout << u << " " << v << endl;
                continue;
            }

            if(d[t + 1] != '?') {
                int i = direction[d[t + 1]];
                int x = u + dx[i], y = v + dy[i];
//                debug("Move", d[t + 1], pii(u, v), pii(x, y));

                if(1 <= x && x <= n && 1 <= y && y <= m && a[x][y] != BLOCKED) {
                    if(!vis[t + 1][x][y]) {
                        vis[t + 1][x][y] = true;
                        q.emplace(t + 1, x, y);
                    }
                }
            }else {
                for(int i = 0; i < 4; i++) {
                    int x = u + dx[i], y = v + dy[i];

                    if(1 <= x && x <= n && 1 <= y && y <= m && a[x][y] != BLOCKED) {
                        if(!vis[t + 1][x][y]) {
                            vis[t + 1][x][y] = true;
                            q.emplace(t + 1, x, y);
                        }
                    }
                }
            }
        }

        cout << counter << endl;

//        debug("After board");
//        for(int i = 1; i <= n; i++) {
//            for(int j = 1; j <= m; j++)
//                cout << vis[k][i][j];
//            cout << endl;
//        }
//        debug("Ending");
    }
}

namespace subtask_3 {
    bool approve() {
        return true;
    }

    bitset<N> row[2][N], passable[N];

    void move_south() {
        for(int i = n; i >= 1; i--) row[1][i] = row[0][i - 1] & passable[i];
        for(int i = 1; i <= n; i++) row[0][i] = row[1][i];
    }

    void move_north() {
        for(int i = 1; i <= n; i++) row[1][i] = row[0][i + 1] & passable[i];
        for(int i = 1; i <= n; i++) row[0][i] = row[1][i];
    }

    void move_east() {
        for(int i = 1; i <= n; i++) row[1][i] = (row[0][i] << 1) & passable[i];
        for(int i = 1; i <= n; i++) row[0][i] = row[1][i];
    }

    void move_west() {
        for(int i = 1; i <= n; i++) row[1][i] = (row[0][i] >> 1) & passable[i];
        for(int i = 1; i <= n; i++) row[0][i] = row[1][i];
    }

    void move_arbitrary() {
        for(int i = 1; i <= n; i++) {
            row[1][i] = (row[0][i - 1] | row[0][i + 1] | (row[0][i] << 1) | (row[0][i] >> 1));
            row[1][i] &= passable[i];
        }

        for(int i = 1; i <= n; i++) {
            row[0][i] = row[1][i];
        }
    }

    void execute() {
        for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) {
            if(a[i][j] != BLOCKED) {
                row[0][i].set(j);
                passable[i].set(j);
            }
        }

        for(int i = 1; i <= k; i++) {
            if(d[i] == 'S') move_south();
            else if(d[i] == 'N') move_north();
            else if(d[i] == 'E') move_east();
            else if(d[i] == 'W') move_west();
            else move_arbitrary();
        }

        int counter = 0;
        for(int i = 1; i <= n; i++) {
            counter += row[0][i].count();
        }

        cout << counter << endl;
    }
}

void process() {
    if(subtask_2::approve()) return subtask_2::execute();
    if(subtask_3::approve()) return subtask_3::execute();
    assert(false);
}

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

    #define task "Deeto"
    if(fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }

    int testcase = 1; // cin >> testcase;
    for(int i = 1; i <= testcase; i++) {
        input();
        process();
    }

    cerr << "Saa, watashtachi no deeto hajimemashou" << endl;
    cerr << "Atarashii kiseki wo koko kara hajimeru shining place nee mou ichido kimi to..." << endl;

    return 0;
}

Compilation message (stderr)

nautilus.cpp: In function 'int main()':
nautilus.cpp:216:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  216 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
nautilus.cpp:217:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  217 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...