QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270402#7751. Palindrome Pathucup-team859TL 92ms8968kbC++175.0kb2023-11-30 20:19:572023-11-30 20:19:58

Judging History

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

  • [2023-11-30 20:19:58]
  • 评测
  • 测评结果:TL
  • 用时:92ms
  • 内存:8968kb
  • [2023-11-30 20:19:57]
  • 提交

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 <= 400; ++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: 5484kb

input:

2 2
11
11
1 1 2 2

output:

DRUDLUULDURD

result:

ok Valid Solution (Length = 12).

Test #2:

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

input:

2 2
10
01
1 1 2 2

output:

-1

result:

ok No Solution.

Test #3:

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

input:

1 1
1
1 1 1 1

output:



result:

ok Valid Solution (Length = 0).

Test #4:

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

input:

5 4
1111
1111
1111
1111
1111
4 2 4 2

output:

DDDUUUULLRDDLDDRURUUURDDDDLRUUUULDDDLDLUUUDRUULLUURDUUULDLDDDLUUUURLDDDDRUUURURDDLDDRLLUUUUDDD

result:

ok Valid Solution (Length = 94).

Test #5:

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

input:

5 5
11111
10101
11111
10101
11111
1 4 5 5

output:

DLLLURRRRDDLLLLUDDDRRUDRRUDLLLLUURRUDRRUULLLLLLLLUURRDURRUULLLLDURRDURRDDDULLLLDDRRRRULLLD

result:

ok Valid Solution (Length = 90).

Test #6:

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

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: 5608kb

input:

5 4
1001
1101
1111
0011
0010
2 2 1 1

output:

UULLULDDRRRDLDURUUUDDLLLURLUULRULLLDDUUURUDLDRRRDDLULLUU

result:

ok Valid Solution (Length = 56).

Test #8:

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

input:

5 3
101
111
100
111
100
4 1 2 2

output:

UUULDDDRRLLDUUURRUDLLUULLDURRUUUDLLRRDDDLUUU

result:

ok Valid Solution (Length = 44).

Test #9:

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

input:

5 5
01110
10110
11110
11011
11100
2 4 5 1

output:

DLDUULRRDLDLDLDRRLLUUUDDRURURDDRLUUULLLLUUULRDDRURURDDUUULLRRDLDLDLDRRLUUDLD

result:

ok Valid Solution (Length = 76).

Test #10:

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

input:

5 3
011
111
110
111
011
3 1 2 1

output:

ULLRUDLDDRRDLRULUDLUURRUDLUULDURRUULDULURLDRRDDLDURLLU

result:

ok Valid Solution (Length = 54).

Test #11:

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

input:

4 5
11111
11111
11111
11111
3 2 1 3

output:

UUULLDDRDRRUURULLDDULUDRURRDDDUULDDLLLRULUUUULURLLLDDLUUDDDRRURDULUDDLLURUURRDRDDLLUUU

result:

ok Valid Solution (Length = 86).

Test #12:

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

input:

5 5
11111
10101
11111
10101
11111
2 5 1 1

output:

UUUULLLLRRDDDDRRUULRUULRDDDDLLUUUULLDDRLDDRLUUUUUUUULRDDLRDDLLUUUULLDDDDRLUURLUURRDDDDRRLLLLUUUU

result:

ok Valid Solution (Length = 96).

Test #13:

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

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: 1ms
memory: 5488kb

input:

3 5
10100
00010
00111
1 3 1 1

output:

-1

result:

ok No Solution.

Test #15:

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

input:

4 5
10001
11111
11100
11111
4 5 3 1

output:

DLLUULLUDDDRUURRRUDLLDDRRLLUULDDLUUUUUULDDLUULLRRDDLLDURRRUURDDDULLUULLD

result:

ok Valid Solution (Length = 72).

Test #16:

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

input:

3 5
11111
10100
11111
1 2 3 5

output:

RRDLDULLDDRRUURRLLLRDDRRLLLLUUUULLLLRRDDRLLLRRUURRDDLLUDLDRR

result:

ok Valid Solution (Length = 60).

Test #17:

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

input:

4 5
01110
10101
11011
10111
1 3 2 3

output:

-1

result:

ok No Solution.

Test #18:

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

input:

5 5
11111
11111
11111
11111
11111
1 3 5 2

output:

LLLRRRRDLLDDLDRRUURDDUULDDLLUUULDDDUUURDDRUURRULLLLLLLLURRUURDDRUUUDDDLUUULLDDLUUDDRUURRDLDDLLDRRRRLLL

result:

ok Valid Solution (Length = 102).

Test #19:

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

input:

5 5
11111
10101
11111
10101
11111
5 1 2 3

output:

UUULULDDDDRRRRUULLDULRRRDDLLLLUUUURRDURRDULLLLLLLLUDRRUDRRUUUULLLLDDRRRLUDLLUURRRRDDDDLULUUU

result:

ok Valid Solution (Length = 92).

Test #20:

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

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: 2ms
memory: 5600kb

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: 5788kb

input:

5 5
10101
11111
10101
11111
11111
3 1 2 4

output:

RUUUDRRUDDDLLDRRRRUUUUDLRDDLRDLLLLUUDRRUULLUULLUURRDUULLLLDRLDDRLDUUUURRRRDLLDDDURRDUUUR

result:

ok Valid Solution (Length = 88).

Test #23:

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

input:

5 5
00001
11111
01110
01111
01111
1 5 5 2

output:

DLLDRDLLDRRRUDLLLUUULRDDRRULURRUURRULURRDDRLUUULLLDURRRDLLDRDLLD

result:

ok Valid Solution (Length = 64).

Test #24:

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

input:

5 5
01011
10111
11011
10101
01110
4 1 2 3

output:

-1

result:

ok No Solution.

Test #25:

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

input:

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

output:

UUUUUUURRRRRLLLLLLLDDRDRUUULDURDRRDLDDRRDDLLLLURRRLLULLUDDDDDDRRRURURDDRUURDDUULDDLLRUULLLDLUDRURRDLDLLLUUUUURRDLDRRRRRURDUUULDUUURULLLLRRDDDLRUUURRDDULDDRDDLDLUULLUURULLDDLULUUUULULDDLLURUULLUULDLDDRDDLUDDRRUUURLDDDRRLLLLURUUUDLUUUDRURRRRRDLDRRUUUUULLLDLDRRURDULDLLLUURLLDDLUUDDRUURDDRURURRRDDDDDDUL...

result:

ok Valid Solution (Length = 354).

Test #26:

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

input:

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

output:

LLDLLLRRRRDDDDRLDDLLLLUUUURRRLUDDDLRDUUULLUDDDDDRRDDRRUDRLDULLLLUDDURRDUUURRRLUULRUURLUURLLLLLLLLLLRUULRUURLUULRRRUUUDRRUDDULLLLUDLRDURRDDRRDDDDDULLUUUDRLDDDULRRRUUUULLLLDDLRDDDDRRRRLLLDLL

result:

ok Valid Solution (Length = 188).

Test #27:

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

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: 13ms
memory: 5724kb

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: 0ms
memory: 6036kb

input:

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

output:

LULULLUUUDRRRRRDDLDRDLLLDDDRURRLUDLDDRRLLULDLLUURDULUUUURRDRUDLULLUDDDRLDDDDRRUUUURRRULURUULLUDLLLUULLLDULLUURULURRRUUUURRDDDDLRDDDULLULDURDRRUUUULUDRUULLDLULLRRDDLDULRRURDDDLLLDRDLDDRRRRRDUUULLULUL

result:

ok Valid Solution (Length = 198).

Test #30:

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

input:

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

output:

LUUURUULLULULLLRRDRURRRDLDRRUDDDDLDRDDLDLULDLLLLUUURRRRURUULLLLLDDRRRURLDLLLURLURURRRLLLDRRRRDDLDDRLLRULLLLDRDULDDRRRRURDRURUULURUUULLURULLLDLULLDRLUULRDLLULDLLLURULLUUURULUURURDRURRRRDDLUDRDLLLLURLLRDDLDDRRRRDLLLRRRURULRULLLDLRURRRDDLLLLLUURURRRRUUULLLLDLULDLDDRDLDDDDURRDLDRRRURDRRLLLULULLUURUUUL

result:

ok Valid Solution (Length = 298).

Test #31:

score: 0
Accepted
time: 54ms
memory: 6140kb

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: 6228kb

input:

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

output:

DDDDDUUUUUUURRRRRRRRRLLLLLLLLLDRURRDDLDDLDDRDRUURDDRUUULLURRULURULRRDRRULRRDDDDDDLLDLUUUUURRDLDDRUDLUURULLDDDDDRRRLLURRUUUUUULDLLULDLDRDLLDRRDDDLUULLRDDLLLUUUUURDULDDDDDRRULUURUUUDRUULLDLUULDLLUURDUUURUULURRDDDDDLUDRUUUUULLLDDRLLUULDDDRRDLLDRDLDLULLDLUUUUUURRULLRRRDDDDDLLURUULDURDDLDRRUUUUULDLLDDDDD...

result:

ok Valid Solution (Length = 376).

Test #33:

score: 0
Accepted
time: 10ms
memory: 6200kb

input:

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

output:

LDDDDDDDDLULLLURRRDLLLDDLLUURLDDDDRRDDDDDULLUUUDDDDURRRRRRDURRDURLUULLUUUULLUDLRRRRRDDLRRLDUUURLUURLLLDURRDDLLDDLLUDLRRRDDDURRRLDDLLLLDULLUULRRRDUUDRLLLUUUDLLUURRUURRRRRRUURRUULLDUUULLLRDUUDRRRLUULLUDLLLLDDLRRRUDDDRRRLDULLDDLLDDRRUDLLLRUULRUUUDLRRLDDRRRRRLDULLUUUULLUULRUDRRUDRRRRRRUDDDDUUULLUDDDDDRR...

result:

ok Valid Solution (Length = 334).

Test #34:

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

input:

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

output:

RDRRRRRDUULLLLLLLLLUUDDRRRRRRRRRDDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDURRRRRRRRRUULLLLLLLLLUURRRRRRRRRUULLLLLLLLLUURRRRRRRRRLLLLLLLLLLLLLLLLLLRRRRRRRRRUULLLLLLLLLUURRRRRRRRRUULLLLLLLLLUURRRRRRRRRUDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDDRRRRRRRRRDDUULLLLLLLLLUUDRRRRRDR

result:

ok Valid Solution (Length = 258).

Test #35:

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

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: 1ms
memory: 5780kb

input:

5 10
1001011101
1111111111
1000011001
1111111111
1101110010
4 3 4 7

output:

LLUUURRRUDDDRDLRURRRDRURUULDUURRDRRUDDDLDULRRUULLULLDRDDLDLULDULLLUURRRUDRLLLLUULLLLRDURRRUULLLUDLULDLDDRDLLULLUURRLUDLDDDURRDRRUUDLUURURDRRRURLDRDDDURRRUUULL

result:

ok Valid Solution (Length = 158).

Test #37:

score: 0
Accepted
time: 66ms
memory: 6088kb

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: 6308kb

input:

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

output:

DDDDDDLLLLLUUURRUDRRUDRRRURDDDRURDDDDDDLUDLLLUURULULLUURDRURDULDLULDDDDRUDLULUULDLDDRDRRRLDULLDLUDRUULUUUDRUUDRDDRURRDRRDURLLDDULDDRRRRUUUUUUULRUDDLDLDUUUULLRDLLLLLLRUURLLLLLLDRLLUUUUDLDLDDURLUUUUUUURRRRDDLUDDLLRUDRRDRRURDDRDUURDUUULUURDULDLLUDLRRRDRDDLDLUULULDURDDDDLULDLUDRURDRUULLULURUULLLDULDDDDD...

result:

ok Valid Solution (Length = 334).

Test #39:

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

input:

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

output:

DDUUUUUUUUULLLLLLLLRRDLDRDRDLDRDDDDRRRRURDRUULLURURULURULUURDULLLDDRDLLLDDRURDRUDLULDLUUURUULDLUDRURDDLLRDRRRUUDLUURRDDRDLDRDLDRLLLDDLLURULRDLDRRUURDRRDDLULDLLLLUULLURLULDDDDRURDULDLUUUUUURDULDDRDDRRUULURULULULDURRULLLLURRUDLULULURULUURRDDRDDLUDRUUUUUULDLUDRURDDDDLULRULLUULLLLDLULDDRRDRUURRDLDRLURUL...

result:

ok Valid Solution (Length = 434).

Test #40:

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

input:

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

output:

ULLLLLLLRRDDLLDDRRUDDDLLUDRRDDLLDUUDRRRRUURRDDRRRLDULLDUUURRRLDULLLLLRDDRLDULLDUUUUURRUUUURRRRDDDDRLDULLDUUULRUDDDRRUULRRLUURLLLLLLRDDLRDDDURLLLLLUUUDRRUULLLLUURRDUUULLLLLRUDDDRLDDRLLLLLLRUULRRLUURRDDDURLUUUDLLUDLRDDDDRRRRUUUURRUUUUUDLLUDLRDDRLLLLLUDLRRRUUUDLLUDLRRRDDRRUURRRRDUUDLLDDRRDULLDDDURRDDLL...

result:

ok Valid Solution (Length = 312).

Test #41:

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

input:

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

output:

LLLLLUURRRRRRRRRUULLLLLLLLLUURRRRRRRRRLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDURRRRRRRRRUULLLLLLLLLUURRRRRRRRRUULLLLLLLLLUUUULLLLLLLLLUURRRRRRRRRUULLLLLLLLLUURRRRRRRRRUDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLDDRRRRRRRRRDDLLLLLLLLLRRRRRRRRRUULLLLLLLLLUURRRRRRRRRUULLLLL

result:

ok Valid Solution (Length = 274).

Test #42:

score: 0
Accepted
time: 16ms
memory: 5732kb

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: 6420kb

input:

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

output:

DRRRUUULUULLURULLLLULRRDRRURRDLDDLLLULDLUURLDDRURDRRUDRRRRRLLDLDLUDLLDDDDRRRRRRULURLUURLDDDRDLLLLUURURDUUDLDLDDLLUULLURUUDDLLUUDDDDDRRLLUUURUDDRRRLUURRRURULLUURRRURDULDLLULLDLLULLLLULLDLLULLDLUDRURRRUULLURURRRUULRRRDDURUUULLRRDDDDDUULLDDUURULLUULLDDLDLDUUDRURUULLLLDRDDDLRUULRULURRRRRRDDDDLLDULDLDLLR...

result:

ok Valid Solution (Length = 358).

Test #44:

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

input:

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

output:

ULUUURDUULLDURRRDLDLDLDDRURRURDDRDRUURUUULLDRDULULURLDRRRRDRDULUURLDLDDDRLLLRDRDRRUDDDDLRUUULLULDLULUULUDDLDDRDDRDLLLLUURRDULLDDRRRRRURDURULLLRRRDLLDLULUULUULDLDUUURURURULLLLLLURURURUUUDLDLUULUULULDLLDRRRLLLURUDRURRRRRDDLLUDRRUULLLLDRDDRDDLDDULUULULDLULLUUURLDDDDURRDRDRLLLRDDDLDLRUULUDRDRRRRDLRULULU...

result:

ok Valid Solution (Length = 346).

Test #45:

score: 0
Accepted
time: 92ms
memory: 5468kb

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: 4ms
memory: 8968kb

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:

DDDDDDDDDDDDUUUUUUUUUUUUUUUUUUURRRRRRRRRRRLLLLLLLLLLLDRDLDDRURUUULRRRRRDRURDDLDLDRDDLULDLLDDLUUULLDRDLDDDDDRRRULLURURDRURUULDURRRDRDDDRRDDLDDRRUUUUUUUUULLDRDDLUDRUULUULDDUUURRDRUULLURURDUULLDURRDLDLDLRRRDDLULLDRDRRDDDDLLRRDDDDLRDDLDDLDLLURUUUUUULDDDDLUULDDDRDLDLLURULLDDLLUUUUUURDDRDRRULULURRDURUURRU...

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: