Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define fi first
- #define se second
- #define pb push_back
- #define pf push_front
- #define popb pop_back
- #define popf pop_front
- #define ins insert
- #define pq priority_queue
- #define minele min_element
- #define maxele max_element
- #define lb lower_bound //first pos >= val
- #define ub upper_bound // first pos > val
- #define cnt_bit __builtin_popcount
- #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
- //#pragma GCC optimize("Ofast")
- //#pragma GCC target("avx,avx2,fma")
- using namespace std;
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- typedef long long ll;
- typedef pair<ll, ll> pll;
- typedef pair<int, int> pii;
- int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
- char go[4] = {'D', 'R', 'U', 'L'};
- int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
- int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
- const ll oo = 1e18;
- const ll maxN = 20;
- /* Author : Le Ngoc Bao Anh, 10A5, LQD High School for Gifted Student */
- void maximize(int &a, int b) {
- a = max(a, b);
- }
- void minimize(int &a, int b) {
- a = min(a, b);
- }
- int p[maxN][maxN];
- int pre[maxN][maxN];
- bool vs[maxN][maxN][maxN][maxN], dp[maxN][maxN];
- struct G {
- int id, a, b, c, d;
- } f[maxN][maxN][maxN][maxN];
- void solve() {
- int n, m;
- int a, b, c, d;
- cin >> n >> m >> a >> b >> c >> d;
- int blocks = 0;
- a++; b++; c++; d++;
- for(int i = 1; i <= n; i++) {
- for(int j = 1; j <= m; j++) {
- cin >> p[i][j];
- blocks += p[i][j];
- }
- }
- auto is = [&](int x, int y) -> bool {
- return (x >= 1 && y >= 1 && x <= n && y <= m && !p[x][y]);
- };
- auto sub1 = [&]() -> void {
- cout << 4 << endl;
- cout << "ULUL";
- return;
- };
- auto sub2 = [&]() -> void {
- queue<pii> q;
- q.push(make_pair(a, b));
- dp[a][b] = 1;
- while(!q.empty()) {
- int x = q.front().fi, y = q.front().se; q.pop();
- for(int r = 0; r < 4; r++) {
- int nx = x + d4x[r], ny = y + d4y[r];
- if(!is(nx, ny)) continue;
- if(dp[nx][ny]) continue;
- dp[nx][ny] = true;
- pre[nx][ny] = r + 1;
- q.push(make_pair(nx, ny));
- }
- }
- vector<char> ans;
- int x = 1, y = 1;
- while(true) {
- if(!pre[x][y]) break;
- int k = pre[x][y] - 1;
- ans.pb(go[k]);
- x = x - d4x[k], y = y - d4y[k];
- }
- int len = ans.size();
- cout << len << endl;
- reverse(ans.begin(), ans.end());
- for(int i = 0; i < len; i++) {
- cout << ans[i];
- }
- };
- auto sub3 = [&]() -> void {
- cout << 20 << endl;
- for(int i = 1; i <= 10; i++) cout << "U";
- for(int i = 1; i <= 10; i++) cout << "L";
- };
- auto sub4 = [&]() -> void {
- struct P {
- int a, b, c, d;
- };
- queue<P> q;
- q.push({a, b, c, d});
- vs[a][b][c][d] = true;
- while(!q.empty()) {
- int a = q.front().a, b = q.front().b, c = q.front().c, d = q.front().d; q.pop();
- for(int r = 0; r < 4; r++) {
- int na = a + d4x[r], nb = b + d4y[r];
- int nc = c + d4x[r], nd = d + d4y[r];
- if(!is(na, nb)) na = a, nb = b;
- if(!is(nc, nd)) nc = c, nd = d;
- if(vs[na][nb][nc][nd]) continue;
- vs[na][nb][nc][nd] = true;
- f[na][nb][nc][nd] = {r + 1, a, b, c, d};
- q.push({na, nb, nc, nd});
- }
- }
- int a = 1, b = 1, c = 1, d = 1;
- vector<char> ans;
- while(true) {
- if(!f[a][b][c][d].id) break;
- G k = f[a][b][c][d];
- ans.pb(go[k.id - 1]);
- a = k.a, b = k.b;
- c = k.c, d = k.d;
- }
- int len = ans.size();
- cout << len << endl;
- reverse(ans.begin(), ans.end());
- for(int i = 0; i < len; i++) cout << ans[i];
- };
- auto sub5 = [&]() -> void {
- };
- if(n <= 2 && m <= 2) sub1();
- else if(a == c && b == d) sub2();
- else if(n <= 10 && m <= 10 && blocks == 0) sub3();
- else if(n <= 10 && m <= 10) sub4();
- else sub5();
- }
- int main()
- {
- ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- //online
- #endif
- int tc = 1, ddd = 0;
- // cin >> tc;
- while(tc--) {
- //ddd++;
- //cout << "Case #" << ddd << ": ";
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement