fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e2 + 1;
  5. char a[N][N];
  6. vector<vector<char>> ans;
  7. vector<char> v;
  8.  
  9. bool val(int i, int j) {
  10. return i >= 1 && j >= 1 && i <= 4 && j <= 4 && a[i][j] != '#';
  11. }
  12.  
  13. void solve(int i, int j) {
  14. if (i == 4 && j == 4) {
  15. ans.push_back(v);
  16. return;
  17. }
  18.  
  19. // Try moving right
  20. if (val(i, j + 1)) {
  21. v.push_back('R'); // Move right
  22. solve(i, j + 1); // Recur
  23. v.pop_back(); // Backtrack
  24. }
  25.  
  26. // Try moving down
  27. if (val(i + 1, j)) {
  28. v.push_back('D'); // Move down
  29. solve(i + 1, j); // Recur
  30. v.pop_back(); // Backtrack
  31. }
  32. }
  33.  
  34. int main() {
  35. for (int i = 1; i <= 4; i++) {
  36. for (int j = 1; j <= 4; j++)
  37. cin >> a[i][j];
  38. }
  39. solve(1, 1);
  40. for (auto &x : ans) {
  41. for (auto &i : x)
  42. cout << i << ' ';
  43. cout << '\n';
  44. }
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0.01s 5280KB
stdin
...#
.#..
....
.#..
stdout
R R D R D D 
R R D D R D 
R R D D D R 
D D R R R D 
D D R R D R