QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1141#718270#8981. Kangaroo PuzzleKobicGendKobicGendFailed.2024-11-06 20:18:432024-11-06 20:18:44

Details

Extra Test:

Invalid Input

input:

2 3
110
111

output:


result:

FAIL cycle

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#718270#8981. Kangaroo PuzzleKobicGendAC ✓2ms3972kbC++232.2kb2024-11-06 20:08:202024-11-06 20:08:20

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
using i64 = long long;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define multi int _;cin>>_;while(_--)
#define int long long
#define pb push_back
#define eb emplace_back
mt19937_64 mrand(chrono::steady_clock().now().time_since_epoch().count());
int rnd(int l,int r){ return mrand() % (r - l + 1) + l;}

signed main()
{  
#ifdef localfreopen
    // freopen("1.in","r",stdin);
#endif
    fastio
    std::cout << std::fixed << std::setprecision(10);
    int n, m;
    std::cin >> n >> m;

    std::vector is(n + 1, std::vector<int> (m + 2));
    for (int i = 1; i <= n; i++) {
        std::string s;
        std::cin >> s;
        for (int j = 0; j < m; j++) {
            is[i][j + 1] = s[j] - '0';
        }
    }

    std::string ans;

    std::vector<std::pair<int, int>> D = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    std::vector<char> target = {'R', 'L', 'D', 'U'};
    auto dfs = [&](auto self, int x, int y, int fx, int fy, int rx, int ry, std::string s)->void{
        int cnt = 0;
        for (int k = 0; k < 4; k++) {
            auto [dx, dy] = D[k];
            int nx = x + dx, ny = y + dy;
            if (nx < 1 || nx > n || ny < 1 || ny > m || is[nx][ny] == 0 || (nx == fx && ny == fy)) continue;
            cnt++;
            self(self, nx, ny, x, y, rx, ry, s + target[k]);
        }
        if (cnt == 0) {
            ans += s;
        }
    };


    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (is[i][j] == 1) {
                int cnt = 0;
                for (auto [dx, dy] : D) {
                    int nx = i + dx, ny = j + dy;
                    if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && is[nx][ny] == 1) {
                        cnt++;
                    }
                }
                if (cnt == 1) {
                    dfs(dfs, i, j, 0, 0, i, j, "");                    
                }
            }
        }
    }
    while (ans.size() > 50000) ans.pop_back();
    std::cout << ans << "\n";


    return 0;
}