QOJ.ac

QOJ

Type: General Discussion

Status: Open

Posted by: fire_and_sweets

Posted at: 2026-06-17 16:05:36

Last updated: 2026-06-17 16:08:54

Back to Problem

Discussion

I wrote a code to solve this problem, which can pass in C++23 but not in C++26:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i, a, b) for (int i = (a); i <= (b); i ++ )
#define DF(i, a, b) for (int i = (a); i >= (b); i -- )
inline void chkmin(int& a, int b) { if (a > b) a = b; }
inline void chkmax(int& a, int b) { if (a < b) a = b; }

typedef pair<int, int> PII;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
const string dirs = "SENW";

const int N = 210;
int n, pre[N][N];
char mp[N][N];
string a[2 * N];

bool check(int x, int y, int d)
{
    return a[2 * x + 1 + dx[d]][2 * y + 1 + dy[d]] == '.';
}

signed main()
{
    string mode;
    cin >> mode;
    if (mode[0] == 'v')
    {
        cin >> n;
        F(i, 0, 2 * n) cin >> a[i];
        memset(pre, -1, sizeof pre);
        queue<PII> q;
        int sx = n - 1, sy = 0;
        pre[sx][sy] = 4;
        q.emplace(sx, sy);
        while (q.size())
        {
            auto [x, y] = q.front(); q.pop();
            F(d, 0, 3)
            {
                if (!check(x, y, d)) continue;
                int nx = x + dx[d], ny = y + dy[d];
                if (pre[nx][ny] != -1) continue;
                q.emplace(nx, ny);
                pre[nx][ny] = d;
            }
        }
        vector<int> path;
        int x = 0, y = n - 1;
        while (x != sx || y != sy)
        {
            int d = pre[x][y];
            path.push_back(d);
            x -= dx[d], y -= dy[d];
        }
        reverse(path.begin(), path.end());
        string hint;
        int lst = -1;
        for (auto d : path)
        {
            vector<int> v;
            F(j, 0, 3) if (j != lst && check(x, y, j)) v.push_back(j);
            int id = 0;
            F(j, 0, (int)v.size() - 1) if (v[j] == d) id = j;
            if (v.size() == 2) hint.push_back(id + '0');
            else if (v.size() == 3) hint.push_back((id >> 1) + '0'), hint.push_back((id & 1) + '0');
            x += dx[d], y += dy[d], lst = d ^ 2;
        }
        cout << hint << endl;
    }
    else
    {
        int lst = -1;
        getline(cin, mode), getline(cin, mode);
        while (mode.size() && mode.back() == '\r') mode.pop_back();
        cin >> n;
        int x = n - 1, y = 0, p = 0;
        while (x != 0 || y != n - 1)
        {
            F(i, 0, 2) F(j, 0, 2) cin >> mp[i][j];
            vector<int> v;
            F(j, 0, 3) if (j != lst && mp[1 + dx[j]][1 + dy[j]] == '.' && j != lst) v.push_back(j);
            int id = v[0];
            if (v.size() == 2) id = v[mode[p] - '0'], p ++ ;
            else if (v.size() == 3) id = v[(mode[p] - '0') * 2 + (mode[p + 1] - '0')], p += 2;
            lst = id ^ 2;
            cout << dirs[id] << endl;
            x += dx[id], y += dy[id];
        }
    }
    return 0;
}

Note that all submissions in C++26 get 0. (Though almost all of them are mine)

Comments

No comments yet.