fork download
  1. #include <cstdio>
  2.  
  3. int main()
  4. {
  5. int T;
  6. scanf("%d", &T);
  7. for (int t = 0; t < T; t++)
  8. {
  9. int R, C;
  10. scanf("%d%d", &R, &C);
  11. int warehouse[20][20];
  12. warehouse[0][0] = -1;
  13. // pickup phase: fill the warehouse from south to north, east to west
  14. for (int r = R-1; r >= 0; r--)
  15. for (int c = C-1; c >= 0; c--)
  16. {
  17. if (!r && !c) continue;
  18. int shipment;
  19. scanf("%d", &shipment);
  20. warehouse[r][c] = shipment;
  21. putchar('P');
  22. if (r == 0)
  23. {
  24. for (int i = 0; i < c-1; i++)
  25. putchar('E');
  26. printf("UE");
  27. for (int i = 0; i < c-1; i++)
  28. putchar('W');
  29. }
  30. else
  31. {
  32. for (int i = 0; i < c; i++)
  33. putchar('E');
  34. for (int i = 0; i < r-1; i++)
  35. putchar('S');
  36. printf("US");
  37. for (int i = 0; i < r-1; i++)
  38. putchar('N');
  39. for (int i = 0; i < c; i++)
  40. putchar('W');
  41. }
  42. }
  43. // dropoff phase
  44. for (int s = 1; s < R*C; s++)
  45. {
  46. // shipment position
  47. int sr = 0, sc = 0;
  48. for (int r = 0; r < R; r++)
  49. for (int c = 0; c < C; c++)
  50. {
  51. if (warehouse[r][c] == s)
  52. {
  53. sr = r;
  54. sc = c;
  55. }
  56. }
  57. // forklift position
  58. int r = 0, c = 0;
  59. while (true)
  60. {
  61. if (r == 0 && c == 0)
  62. {
  63. if (sr == 0 && sc == 1)
  64. {
  65. printf("LED");
  66. warehouse[sr][sc] = -1;
  67. break;
  68. }
  69. if (sr == 1 && sc == 0)
  70. {
  71. printf("LSD");
  72. warehouse[sr][sc] = -1;
  73. break;
  74. }
  75. }
  76. // pick a direction for the forklift
  77. int move;
  78. if (r == sr)
  79. {
  80. if (c < sc)
  81. move = 'E';
  82. else if (r == 0)
  83. move = 'S';
  84. else
  85. move = 'N';
  86. }
  87. else if (c == sc)
  88. {
  89. if (r < sr)
  90. move = 'S';
  91. else if (c == 0)
  92. move = 'E';
  93. else
  94. move = 'W';
  95. }
  96. else if (r < sr && c < sc)
  97. {
  98. if (sr > sc)
  99. move = 'E';
  100. else
  101. move = 'S';
  102. }
  103. else if (r > sr && c < sc)
  104. {
  105. move = 'N';
  106. }
  107. else if (r < sr && c > sc)
  108. {
  109. move = 'W';
  110. }
  111. else if (r < c)
  112. {
  113. move = 'W';
  114. }
  115. else
  116. {
  117. move = 'N';
  118. }
  119. int dr = 0, dc = 0;
  120. int back;
  121. switch (move)
  122. {
  123. case 'N': dr--; back = 'S'; break;
  124. case 'W': dc--; back = 'E'; break;
  125. case 'S': dr++; back = 'N'; break;
  126. case 'E': dc++; back = 'W'; break;
  127. }
  128. // if that direction is unblocked, move there
  129. if (warehouse[r+dr][c+dc] == -1)
  130. putchar(move);
  131. else
  132. {
  133. // otherwise, swap positions with the shipment there
  134. printf("L%c%cU%c", move, move, back);
  135. warehouse[r][c] = warehouse[r+dr][c+dc];
  136. warehouse[r+dr][c+dc] = -1;
  137. if (sr == r+dr && sc == c+dc)
  138. {
  139. sr = r;
  140. sc = c;
  141. }
  142. }
  143. r += dr;
  144. c += dc;
  145. }
  146. }
  147. putchar('\n');
  148. }
  149. }
Success #stdin #stdout 0s 4348KB
stdin
1
2 3
3 1 2 4 5
stdout
PEEUSWWPEUSWPUSPEUEWPUELSSUNLEEUWLNNUSLWWUELSDLEDSLEEUWLEEUWLNNUSWLSSUNLWWUENLEDSLEEUWLEEUWNWLSSUNLWWUENLEDSLEEUWNWLSD