Guest User

Untitled

a guest
Jan 30th, 2017
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int INF = 1000000000;
  6. int n,m;
  7. vector<string> g;
  8. const int N = 55;
  9. const int dx[4] = {0, 0, 1, -1};
  10. const int dy[4] = {1, -1, 0, 0};
  11. string way = "DURL";
  12. int dp[N][N][3001];
  13. int solve(int x,int y,int k){
  14. int &res = dp[x][y][k];
  15. if (res != -1){
  16. return res;
  17. }
  18. if (k == 0 && x == n - 1 && y == m - 1){
  19. return res = 0;
  20. }
  21. if (k == 0 && x != n - 1 && y != m - 1){
  22. return res = INF;
  23. }
  24. if (k > 0){
  25. int ans = INF;
  26. for(int i = 0; i < 4; i++){
  27. int x1 = x + dx[i];
  28. int y1 = y + dy[i];
  29. if (0 <= x1 && x1 < n && 0 <= y1 && y1 < m && g[x1][y1] == '.'){
  30. ans = min(ans,solve(x1,y1,k - 1) + 1);
  31. }
  32. }
  33. return res = ans;
  34. }
  35. }
  36. string lastAns;
  37. void findAns(int x,int y,int k){
  38. if (k == 0 && x == n - 1 && y == m - 1){
  39. return;
  40. }
  41. if (k == 0 && x != n - 1 && y != m - 1){
  42. return;
  43. }
  44. if (k > 0){
  45. int ans = INF;
  46. int r1,r2,tt;
  47. for(int i = 0; i < 4; i++){
  48. int x1 = x + dx[i];
  49. int y1 = y + dy[i];
  50. if (0 <= x1 && x1 < n && 0 <= y1 && y1 < m && g[x1][y1] == '.' && dp[x1][y1][k - 1] != -1 && dp[x1][y1][k - 1] + 1 < ans){
  51. ans = dp[x1][y1][k - 1] + 1;
  52. r1 = x1;
  53. r2 = y1;
  54. tt = i;
  55. }
  56. }
  57. lastAns = lastAns + way[tt];
  58. findAns(r1,r2,k - 1);
  59. }
  60. }
  61. class StepsConstruct {
  62. public:
  63. string construct(vector <string> a, int k) {
  64. n = a.size();
  65. m = a[0].length();
  66. g = a;
  67. memset(dp,-1,sizeof dp);
  68. int x = solve(0,0,k);
  69. findAns(0,0,k);
  70. cout << "x = " << lastAns << endl;
  71. if ( x == k){
  72. cout << lastAns << endl;
  73. return lastAns;
  74. }
  75. return "";
  76. }
  77. };
  78.  
  79.  
  80. <%:testing-code%>
  81. //Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
Add Comment
Please, Sign In to add comment