QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270399#7751. Palindrome Pathucup-team859TL 231ms16436kbC++175.0kb2023-11-30 20:17:022023-11-30 20:17:02

Judging History

你现在查看的是最新测评结果

  • [2023-11-30 20:17:02]
  • 评测
  • 测评结果:TL
  • 用时:231ms
  • 内存:16436kb
  • [2023-11-30 20:17:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;

using ll = long long;
using ull = unsigned long long;

string to_string(const string &s) {
  return '"' + s + '"';
}

string to_string(bool b) {
  return b ? "true" : "false";
}

template <typename A, typename B>
string to_string(const pair<A, B> &p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename T>
string to_string(const T &v) {
  string s = "{";
  bool first = true;
  for (const auto &it : v) {
    if (!first)
      s += ", ";
    else
      first = false;
    s += to_string(it);
  }
  return s += "}";
}

void debug_out() {
  cerr << endl;
}

template <typename T, typename... Args>
void debug_out(const T &first, const Args&... rest) {
  cerr << to_string(first) << " ";
  debug_out(rest...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

auto startTime = high_resolution_clock::now();
int get_time() {
  auto stopTime = chrono::high_resolution_clock::now();
  auto duration = duration_cast<milliseconds>(stopTime - startTime);
  return duration.count(); // in ms
}

struct usu {
  int x, y, x2, y2;
};

int n, m;
usu tt[31][31][31][31];
int marked[31][31];
int viz[31][31][31][31];
int a[31][31];

const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

const int dx2[] = {1, -1, 0, 0};
const int dy2[] = {0, 0, 1, -1};

const int rk[] = {1, 0, 3, 2};

const char ch[] = {'U', 'D', 'L', 'R'};

inline bool in_bounds(int i, int j) {
  return i >= 1 && i <= n && j >= 1 && j <= m && a[i][j];
}

vector<int> ordk;
vector<int> dfp;
int nr = 0;
int nrv = 0;
void dfs(int x, int y) {
  ++nrv;
  marked[x][y] = nr;
  shuffle(ordk.begin(), ordk.end(), rng);
  for (auto k : ordk) {
    int i = dx[k] + x;
    int j = dy[k] + y;

    if (in_bounds(i, j) && marked[i][j] != nr) {
      dfp.push_back(k);
      dfs(i, j);
      dfp.push_back(rk[k]);
    }
  }
}

void print_path(int x, int y, int x2, int y2) {
  string ans;

  while (viz[x][y][x2][y2] != -1) {
    int k = viz[x][y][x2][y2] - 1;
    ans.push_back(ch[k]);

    auto tmp = tt[x][y][x2][y2];
    x = tmp.x;
    y = tmp.y;
    x2 = tmp.x2;
    y2 = tmp.y2;
  }

  reverse(ans.begin(), ans.end());

  string str;
  for (auto it : dfp)
    str.push_back(ch[it]);

  for (auto it = dfp.rbegin(); it != dfp.rend(); ++it)
    str.push_back(ch[*it]);

  string sol = ans + str;
  reverse(ans.begin(), ans.end());
  sol += ans;

  for (auto it : sol)
    cout << it;
  cout << '\n';
}

void solve() {
  ordk = {0, 1, 2, 3};
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; ++j) {
      a[i][j] = s[j - 1] - '0';
    }
  }

  int x, y, x2, y2;
  cin >> x >> y >> x2 >> y2;
  queue<usu> q;
  viz[x][y][x2][y2] = -1;
  q.push({x, y, x2, y2});

  int sx = -1, sy = -1;
  while (!q.empty()) {
    x = q.front().x;
    y = q.front().y;
    x2 = q.front().x2;
    y2 = q.front().y2;
    q.pop();

    for (int k = 0; k < 4; ++k) {
      int i = dx[k] + x;
      int j = dy[k] + y;

      int i2 = dx2[k] + x2;
      int j2 = dy2[k] + y2;

      if (!in_bounds(i, j)) {
        i -= dx[k];
        j -= dy[k];
      }

      if (in_bounds(i2, j2)) {
        if (!viz[i][j][i2][j2]) {
          tt[i][j][i2][j2] = {x, y, x2, y2};
          viz[i][j][i2][j2] = k + 1;
          q.push({i, j, i2, j2});
        }
      }

      i2 = dx[k] + x2;
      j2 = dy[k] + y2;

      if (!in_bounds(i2, j2)) {
        i2 = x2;
        j2 = y2;
        if (!viz[i][j][i2][j2]) {
          tt[i][j][i2][j2] = {x, y, x2, y2};
          viz[i][j][i2][j2] = k + 1;
          q.push({i, j, i2, j2});
        }
      }
    }
  }

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

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (a[i][j] == 0)
        continue;
      for (int t = 0; t <= 1000; ++t) {
        ++nr;
        nrv = 0;
        dfp.clear();
        dfs(i, j);

        if (nrv != tot)
          continue;

        int i2 = i, j2 = j;

        for (auto it : dfp) {
          i2 += dx[it];
          j2 += dy[it];
          if (!in_bounds(i2, j2)) {
            i2 -= dx[it];
            j2 -= dy[it];
          }
        }
        
        for (auto it = dfp.rbegin(); it != dfp.rend(); ++it) {
          i2 += dx[*it];
          j2 += dy[*it];
          if (!in_bounds(i2, j2)) {
            i2 -= dx[*it];
            j2 -= dy[*it];
          }
        }

        if (viz[i][j][i2][j2]) {
          print_path(i, j, i2, j2);
          return;
        }
      }
    }
  }

  cout << "-1\n";
}

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

  int t = 1;
//  cin >> t;
  while (t--)
    solve();

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7844kb

input:

2 2
11
11
1 1 2 2

output:

DRUDLUULDURD

result:

ok Valid Solution (Length = 12).

Test #2:

score: 0
Accepted
time: 1ms
memory: 5920kb

input:

2 2
10
01
1 1 2 2

output:

-1

result:

ok No Solution.

Test #3:

score: 0
Accepted
time: 0ms
memory: 5952kb

input:

1 1
1
1 1 1 1

output:



result:

ok Valid Solution (Length = 0).

Test #4:

score: 0
Accepted
time: 2ms
memory: 9884kb

input:

5 4
1111
1111
1111
1111
1111
4 2 4 2

output:

DDDUUUULLDDRRDRUUULLDRLURRDDDLLDRRLLLUDRURULLUUUULLURURDULLLRRDLLDDDRRULRDLLUUURDRRDDLLUUUUDDD

result:

ok Valid Solution (Length = 94).

Test #5:

score: 0
Accepted
time: 1ms
memory: 8084kb

input:

5 5
11111
10101
11111
10101
11111
1 4 5 5

output:

DLLLUDDDDRRUURRUULLDULRRRDDDDLRUULLDDLLUURLUUUULRUULLDDLLUURLDDDDRRRLUDLLUURRUURRDDDDULLLD

result:

ok Valid Solution (Length = 90).

Test #6:

score: 0
Accepted
time: 1ms
memory: 9856kb

input:

5 3
111
100
111
001
111
4 3 3 2

output:

URLLUUDDRRDDLLRRUULLUURRLLLLRRUULLUURRLLDDRRDDUULLRU

result:

ok Valid Solution (Length = 52).

Test #7:

score: 0
Accepted
time: 1ms
memory: 7744kb

input:

5 4
1001
1101
1111
0011
0010
2 2 1 1

output:

ULUDDRRRUUDDDLDURULLUDLUUUULDULLURUDLDDDUURRRDDULU

result:

ok Valid Solution (Length = 50).

Test #8:

score: 0
Accepted
time: 0ms
memory: 12076kb

input:

5 3
101
111
100
111
100
4 1 2 2

output:

URUUUDDDDURRLLUURRUDLLUULLDURRUULLRRUDDDDUUURU

result:

ok Valid Solution (Length = 46).

Test #9:

score: 0
Accepted
time: 1ms
memory: 9944kb

input:

5 5
01110
10110
11110
11011
11100
2 4 5 1

output:

DLDUULRDRDDRLULLDDRLLUUUDDDRUURRUUDLULLULDUURRUURDDDUUULLRDDLLULRDDRDRLUUDLD

result:

ok Valid Solution (Length = 76).

Test #10:

score: 0
Accepted
time: 1ms
memory: 7880kb

input:

5 3
011
111
110
111
011
3 1 2 1

output:

ULRUDDLDRDRUDLULURULRRUDLUULDURRLURULULDURDRDLDDURLU

result:

ok Valid Solution (Length = 52).

Test #11:

score: 0
Accepted
time: 1ms
memory: 8020kb

input:

4 5
11111
11111
11111
11111
3 2 1 3

output:

UUULLDDRURDDLLRRUURRULLLRRRDDDLUDRUULLLDLUUUULDLLLUURDULDDDRRRLLLURRUURRLLDDRURDDLLUUU

result:

ok Valid Solution (Length = 86).

Test #12:

score: 0
Accepted
time: 1ms
memory: 10096kb

input:

5 5
11111
10101
11111
10101
11111
2 5 1 1

output:

UUUULLLLDDRRUULRDDDDLLUDRRRRUULRUULRDDDDLLUULLUUUULLUULLDDDDRLUURLUURRRRDULLDDDDRLUURRDDLLLLUUUU

result:

ok Valid Solution (Length = 96).

Test #13:

score: 0
Accepted
time: 1ms
memory: 7788kb

input:

4 5
11111
10000
11111
00001
1 3 4 5

output:

DRRRRLLLLDDRRRRDULLLLUURRRRLLLLLLLLRRRRUULLLLUDRRRRDDLLLLRRRRD

result:

ok Valid Solution (Length = 62).

Test #14:

score: 0
Accepted
time: 2ms
memory: 7764kb

input:

3 5
10100
00010
00111
1 3 1 1

output:

-1

result:

ok No Solution.

Test #15:

score: 0
Accepted
time: 0ms
memory: 7824kb

input:

4 5
10001
11111
11100
11111
4 5 3 1

output:

LLULULUDDDRUURDDRRLLUURRUDLLLDDLUUUUUULDDLLLDURRUULLRRDDRUURDDDULULULL

result:

ok Valid Solution (Length = 70).

Test #16:

score: 0
Accepted
time: 1ms
memory: 5740kb

input:

3 5
11111
10100
11111
1 2 3 5

output:

RRDLDULLDDRRRRLLUULRRRLLDDLLUUUULLDDLLRRRLUULLRRRRDDLLUDLDRR

result:

ok Valid Solution (Length = 60).

Test #17:

score: 0
Accepted
time: 0ms
memory: 8016kb

input:

4 5
01110
10101
11011
10111
1 3 2 3

output:

-1

result:

ok No Solution.

Test #18:

score: 0
Accepted
time: 0ms
memory: 9880kb

input:

5 5
11111
11111
11111
11111
11111
1 3 5 2

output:

LLLDRDRRULULRDRRULRDDDDLLLURRLLDRRRUUULDLLULDDDUUUUUUUUDDDLULLDLUUURRRDLLRRULLLDDDDRLURRDRLULURRDRDLLL

result:

ok Valid Solution (Length = 102).

Test #19:

score: 0
Accepted
time: 0ms
memory: 7800kb

input:

5 5
11111
10101
11111
10101
11111
5 1 2 3

output:

UUULULDDDDRRRRUULLDUUULRRRDULLDDRRDDLLLLUURLUUUULRUULLLLDDRRDDLLUDRRRLUUUDLLUURRRRDDDDLULUUU

result:

ok Valid Solution (Length = 92).

Test #20:

score: 0
Accepted
time: 1ms
memory: 7832kb

input:

5 5
11111
10000
11111
00001
11111
4 5 5 3

output:

ULLDLLDUUDDRRRRDDLLLLRRRRUULLLLUURRRRLLLLLLLLRRRRUULLLLUURRRRLLLLDDRRRRDDUUDLLDLLU

result:

ok Valid Solution (Length = 82).

Test #21:

score: 0
Accepted
time: 4ms
memory: 9764kb

input:

5 5
01010
10101
10101
11001
10011
4 1 5 4

output:

-1

result:

ok No Solution.

Test #22:

score: 0
Accepted
time: 1ms
memory: 7784kb

input:

5 5
10101
11111
10101
11111
11111
3 1 2 4

output:

LUUUDDDDRURUURRUDDDLDLRRLURUULLUDDDLDLUUURLUULRUUULDLDDDULLUURULRRLDLDDDURRUURURDDDDUUUL

result:

ok Valid Solution (Length = 88).

Test #23:

score: 0
Accepted
time: 1ms
memory: 9756kb

input:

5 5
00001
11111
01110
01111
01111
1 5 5 2

output:

DDDURDLLLLRDDDRRRULULDURDRDLLLUUURRRUURRRUUULLLDRDRUDLULURRRDDDRLLLLDRUDDD

result:

ok Valid Solution (Length = 74).

Test #24:

score: 0
Accepted
time: 6ms
memory: 7964kb

input:

5 5
01011
10111
11011
10101
01110
4 1 2 3

output:

-1

result:

ok No Solution.

Test #25:

score: 0
Accepted
time: 2ms
memory: 12380kb

input:

10 8
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
7 7 3 6

output:

UUUUUUURRRRRLLLLLLLDDRURRRURDDLLDLUDLDDRDLLUUUDDDDRRRRUUULDDUULRRURDDDRUUURULUURDULDDRDDDDDDLLDRRLLUURLDRRUUUUULDDDLUUULDDDDDDLULLLDRRLLURRRDRUULLLLURRULUURRURRUULDLULLRRDLLDLUUUULDLLDRRLLULDLUURRURRUULURRULLLLUURDRRRULLRRDLLLULDDDDDDLUUULDDDLUUUUURRDLRUULLRRDLLDDDDDDRDDLUDRUULURUUURDDDRURRLUUDDLUUU...

result:

ok Valid Solution (Length = 354).

Test #26:

score: 0
Accepted
time: 2ms
memory: 13844kb

input:

10 6
111111
101010
111111
101010
111111
101010
111111
101010
111111
101010
1 6 2 3

output:

UUUUUUURRULLLLLRRDDDDRRUURLUURLLRDDLRDDRLDDRLLLLLUUUUUDRLDDRLDDDDDUUURRDDLRRRRLDULLDUUURRDUUULLDUUUUULLLLUUUUUDLLUUUDRRUUUDLLUDLRRRRLDDRRUUUDDDDDLRDDLRDUUUUULLLLLRDDLRDDRLDDRLLRUULRUURRDDDDRRLLLLLURRUUUUUUU

result:

ok Valid Solution (Length = 206).

Test #27:

score: 0
Accepted
time: 0ms
memory: 12080kb

input:

10 10
1111111111
1000000000
1111111111
0000000001
1111111111
1000000000
1111111111
0000000001
1111111111
1000000000
5 5 5 2

output:

RURRRRDUULLLLLLLLLUURRRRRRRRRLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDURRRRRRRRRUULLLLLLLLLUURRRRRRRRRUULLLLLLLLLUUUULLLLLLLLLUURRRRRRRRRUULLLLLLLLLUURRRRRRRRRUDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLRRRRRRRRRUULLLLLLLLLUUDRRRRUR

result:

ok Valid Solution (Length = 256).

Test #28:

score: 0
Accepted
time: 31ms
memory: 7784kb

input:

10 10
1010110101
0000011010
1001001001
0011111000
1000111100
1011101001
1100110011
0110001011
0011111000
0101011101
7 5 4 3

output:

-1

result:

ok No Solution.

Test #29:

score: 0
Accepted
time: 2ms
memory: 12120kb

input:

10 6
100100
111111
100001
111111
101111
111111
101010
111111
111100
111111
6 5 7 3

output:

LLLULUUUUUDRRRUDRRDDDDLDDRLLDLULLUUUURRRDDLLRDURULRURDULLLLUDDDDDRRDLLDRRRRRLLLLLURRRURUURUUUULLLLLUULLLLLUUUURUURURRRULLLLLRRRRRDLLDRRDDDDDULLLLUDRURLURUDRLLDDRRRUUUULLULDLLRDDLDDDDRRDURRRDUUUUULULLL

result:

ok Valid Solution (Length = 200).

Test #30:

score: 0
Accepted
time: 0ms
memory: 12156kb

input:

10 8
11111110
11110111
01111111
11111101
11011101
11111111
11111011
11011101
11001111
11111110
7 1 2 1

output:

LUUURUULLULULLLDRDDRUURDRDDLDRRUUURRDDDDLUDRDDLLDLLLLLURUULDUURRDRDRRLDULURLLULULUDRDLDRDDLDRRRRRRLURRUUUUUUULLULLLLRRRRRLDRRDLLDDDLLUUDRUULULDDLUULUULUULDDLULUURDUULLDDDLLDRRDLRRRRRLLLLULLUUUUUUURRULRRRRRRDLDDRDLDRDULULULLRULUDLRRDRDRRUUDLUURULLLLLDLLDDRDULDDDDRRUUURRDLDDRDRUURDDRDLLLULULLUURUUUL

result:

ok Valid Solution (Length = 298).

Test #31:

score: 0
Accepted
time: 131ms
memory: 13852kb

input:

10 10
1111011101
1110111110
1111011111
1011101111
0101010111
1110111111
0111010111
1011111011
0111010101
1011111110
2 6 7 4

output:

-1

result:

ok No Solution.

Test #32:

score: 0
Accepted
time: 0ms
memory: 12148kb

input:

8 10
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
8 10 6 10

output:

DDDDDUUUUUUURRRRRRRRRLLLLLLLLLRRRDDRDDDLDLDRRURURURRRDDLULDDLLRUDRUURDDRLURUUULLUULURRDRDLRULURLLLDLULDURDDDRUDLDUUURRDDRRDLLLDLDLDLLULDLUURURULUURDULLDDDUUURDDRRDULDDULDLDDRURRURUUULUULLLLLLUULUUURURRURDDLDLUDDLUDRRDDRUUUDDDLLUDRUULURURUULDLULLDLDLDLLLDRRDDRRUUUDLDURDDDRUDLULDLLLRULURLDRDRRULUULLUU...

result:

ok Valid Solution (Length = 376).

Test #33:

score: 0
Accepted
time: 3ms
memory: 13980kb

input:

10 10
1111111111
1010101010
1111111111
1010101010
1111111111
1010101010
1111111111
1010101010
1111111111
1010101010
3 5 9 1

output:

LDLUULLRRRRDDDDLLDDDDDURRUURRRRUULLUURRDUUURLDDRLLLUURLLRDDDDDURRRLDDRLLLDDDURRUDRLDULLLRUULLUDLRDDDULLLLDUUURLDDRRUUUULLDUUUUDRRUDRLDULLDDRRRRRLUURLUULLLLLLLLUULRUULRRRRRDDLLUDLRDURRDUUUUDLLUUUURRDDLRUUUDLLLLUDDDRLDULLUURLLLUDLRDURRUDDDLLLRDDLRRRUDDDDDRLLRUULLLRDDLRUUUDRRUULLUURRRRUURRUDDDDDLLDDDDR...

result:

ok Valid Solution (Length = 310).

Test #34:

score: 0
Accepted
time: 0ms
memory: 10264kb

input:

10 10
1111111111
1000000000
1111111111
0000000001
1111111111
1000000000
1111111111
0000000001
1111111111
1000000000
5 4 7 2

output:

RDRRRRRDUULLLLLLLLLUURRRRRRRRRLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDURRRRRRRRRUULLLLLLLLLUURRRRRRRRRUULLLLLLLLLUUUULLLLLLLLLUURRRRRRRRRUULLLLLLLLLUURRRRRRRRRUDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLRRRRRRRRRUULLLLLLLLLUUDRRRRRDR

result:

ok Valid Solution (Length = 258).

Test #35:

score: 0
Accepted
time: 6ms
memory: 7836kb

input:

9 10
1111001000
1110010111
0001001000
0101000101
1101000001
0111100110
0010010000
0001001000
1011001101
2 8 5 2

output:

-1

result:

ok No Solution.

Test #36:

score: 0
Accepted
time: 0ms
memory: 7848kb

input:

5 10
1001011101
1111111111
1000011001
1111111111
1101110010
4 3 4 7

output:

LLUUUDRRRUDRRDRURULLRRDRRUDDDLLLLDLULLLDLUUDDRURRRDLRRURRRDURUULLLDLULLLLLUULLLLLULDLLLUURUDRRRURRLDRRRURDDUULDLLLULDLLLLDDDURRDRRLLURURDRRDURRRDUUULL

result:

ok Valid Solution (Length = 150).

Test #37:

score: 0
Accepted
time: 164ms
memory: 12032kb

input:

7 10
1111111001
1111101110
1111011111
1101011101
1111011111
1111001011
1111111011
7 10 7 2

output:

-1

result:

ok No Solution.

Test #38:

score: 0
Accepted
time: 2ms
memory: 12244kb

input:

9 10
0101011101
1111111111
0101111111
1111111111
1111110101
1011111111
1101111101
1111111011
1101011111
3 7 7 10

output:

URLLLLLLURRDDDDRDRRRRRDLLDLLURLDDULLLDRLURRRRRDRRRURDUUUUUUUUDDDDDDDLDLLLUURDURURLLLLLLURUURDDRURULUURDRRDDLDUUDRUULUDLULDLLLRUDRRDRDLDLUULDLRDLLLDDRLUUUDRUUULRUURLUUURDUUULRDDLLLDRLDLUULDLDRDRRDURLLLDLULDULUURDUUDLDDRRDRUULURURDDRUURULLLLLLRURUDRUULLLDLDDDDDDDUUUUUUUUDRURRRDRRRRRULRDLLLUDDLRULLDLLD...

result:

ok Valid Solution (Length = 322).

Test #39:

score: 0
Accepted
time: 0ms
memory: 12264kb

input:

10 10
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
9 5 3 2

output:

DDUUUUUUUUULLLLLLLLDRDDLUDDDDDDRDRUUURDDRRDLLRRUULUUUURRDDDRURDRUUUULLLLLLDDDLLURUUURURDRURRDRRULRDLLLRULLDLULLLRRDLDDDLDDDUURRUUURRRRRRDLLDRLURRDDDLULDDLDDRRRULLRURLDRDLLLUURULLUUDDRUUULLDDDDRDLLUULDDDLLRULUUUUURUULUULUURUUUUULURLLDDDLUULLDRDDDDLLUUURDDUULLURUULLLDRDLRURLLURRRDDLDDLULDDDRRULRDLLDRR...

result:

ok Valid Solution (Length = 434).

Test #40:

score: 0
Accepted
time: 3ms
memory: 12188kb

input:

10 10
1111111111
1010101010
1111111111
1010101010
1111111111
1010101010
1111111111
1010101010
1111111111
1010101010
2 1 1 3

output:

RRUUUUUUUUUDDRRUULRDDLLDDRRUDDDRRDDDURRDURRDURLLLLLUURRRRRLUURLLLDUUUUURRRLDDRLDULRUULLLLLRDDDDDULRRLUURLLRUURRDDDDRRDDDULLDULLLLDDRLLLDUUURLUDDDRRDUUUUULLUUUUUUUULLUUUUUDRRDDDULRUUUDLLLRDDLLLLUDLLUDDDRRDDDDRRUURLLRUULRRLUDDDDDRLLLLLUURLUDLRDDLRRRUUUUUDLLLRUULRRRRRUULLLLLRUDRRUDRRUDDDRRDDDURRDDLLDDR...

result:

ok Valid Solution (Length = 318).

Test #41:

score: 0
Accepted
time: 0ms
memory: 12100kb

input:

10 10
1111111111
1000000000
1111111111
0000000001
1111111111
1000000000
1111111111
0000000001
1111111111
1000000000
7 4 3 5

output:

LLLLLUURRRRRRRRRUULLLLLLLLLUUDDRRRRRRRRRDDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDURRRRRRRRRUULLLLLLLLLUURRRRRRRRRUULLLLLLLLLUURRRRRRRRRLLLLLLLLLLLLLLLLLLRRRRRRRRRUULLLLLLLLLUURRRRRRRRRUULLLLLLLLLUURRRRRRRRRUDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDDRRRRRRRRRDDUULLLLLLLLLUURRRRRRRRRUULLLLL

result:

ok Valid Solution (Length = 274).

Test #42:

score: 0
Accepted
time: 40ms
memory: 11952kb

input:

10 10
1110000101
1100110001
1011011001
0000000111
0010010111
0100111011
1110010011
1100111111
0110001110
0100000101
9 9 6 10

output:

-1

result:

ok No Solution.

Test #43:

score: 0
Accepted
time: 0ms
memory: 12264kb

input:

10 10
1110111011
1111111111
1110110000
1111111111
1010011100
1111111111
1111001110
1111111111
1001010011
1111111111
6 9 2 9

output:

DULLLLLLURRRUULUULRRDLLDDRRRRURUURDRRURDULDLLULDLUDRDDRRRRLLDLDLLLLDRDLLLDDRRRUDRRUURRULRRDDRUDDLLLRRRULUUURLLRDLDLLDDLLLLLUURRRRLULLULUDRDLRRUUDRRRRULRRULLULDLLULRDLLUURRRLULLLLULRRRUULLDRLULLDLULLURRLURRRRDUURRLDRDULULLULRRRRUULLLLLDDLLDLDRLLRUUULURRRLLLDDURDDRRLURRUURRDURRRDDLLLDRDLLLLDLDLLRRRRDD...

result:

ok Valid Solution (Length = 352).

Test #44:

score: 0
Accepted
time: 2ms
memory: 12024kb

input:

10 10
1111110011
1011111110
0111011111
1101101101
1111111110
1110111101
1011011111
1111111101
1011111101
1111111011
7 3 5 5

output:

ULUURURUULLRRRDDLUDLDDRDDRDRRRDLDRLLLULDLLUURRLLDDRRURRLDRRURURDUURRUDDDDLRUUULLUURLUULLULURLDRRRRDRDULUURLDLLLDRDDDLLULURLDRRLDRDRLURUUURDDDDDLLLLULUULDLUUDDDURUUURRUULLLDUUDLLLUURRUUURUDDDUULDLUULULLLLDDDDDRUUURULRDRDLRRDLRULULLDDDRDLLLDLRUULUDRDRRRRDLRULULLUULRUULLUUURLDDDDURRUUDRURURRDLRRURRDDLL...

result:

ok Valid Solution (Length = 346).

Test #45:

score: 0
Accepted
time: 231ms
memory: 7840kb

input:

10 10
1101011101
1110111011
0101110101
1111101011
1111111111
1010111010
0111111101
1111101011
0111111101
1011101111
3 8 2 2

output:

-1

result:

ok No Solution.

Test #46:

score: 0
Accepted
time: 5ms
memory: 16436kb

input:

20 12
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
111111111111
16 8 13 12

output:

DDDDDDDDDDDDUUUUUUUUUUUUUUUUUUURRRRRRRRRRRLLLLLLLLLLLRRDDRDDLLDLUUURDRLUULRDLDDDRRRRUUUURDDDDRUUUURDDRRDLDRRRDLLLLLLLDLULDDDRDDLULLDDDRDDRDDDRDLLLURUULUUDDRDLRDLDRRRRURUULUURRUULDLLLULUDRRRURULURRDDRRDLDRRRURDDDDLUULLDLDDRURUDLDDDRRULURRDDUULLDRDLLLULDLRURDRUULLUULRDDRUURULRRRDDRUUUULUUURDDUULLULURR...

result:

ok Valid Solution (Length = 1062).

Test #47:

score: -100
Time Limit Exceeded

input:

20 17
11111111111111111
10101010101010101
11111111111111111
10101010101010101
11111111111111111
10101010101010101
11111111111111111
10101010101010101
11111111111111111
10101010101010101
11111111111111111
10101010101010101
11111111111111111
10101010101010101
11111111111111111
10101010101010101
111111...

output:


result: