Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <windows.h>
  2. #pragma hdrstop
  3. #include "pt4.h"
  4. #include <fstream>
  5. #include <string>
  6.  
  7. using namespace std;
  8. int Cx, Cy, N;
  9.  
  10. int l = 0, L = 0;
  11. string Ans;
  12.  
  13. void go(char **lab, string Tra, int X, int Y)
  14. {
  15.     if ((X >= 0) && (Y >= 0) && (X < N) && (Y < N))
  16.    
  17.         if (lab[X][Y] == ' ')
  18.         {
  19.  
  20.             lab[X][Y] = '+';
  21.             l++;
  22.             if (X == Cx && Y == Cy)
  23.             {
  24.                 if (l > L)
  25.                 {
  26.                     L = l;
  27.                     Ans.assign(Tra);
  28.                 }
  29.             }
  30.             else
  31.             {
  32.                 Tra.append("0");
  33.                 Tra[l - 1] = 'U';
  34.                 go(lab, Tra, X - 1, Y);
  35.                 Tra[l - 1] = 'D';
  36.                 go(lab, Tra, X + 1, Y);
  37.                 Tra[l - 1] = 'R';
  38.                 go(lab, Tra, X, Y + 1);
  39.                 Tra[l - 1] = 'L';
  40.                 go(lab, Tra, X, Y - 1);
  41.                 Tra.erase(l - 1, 1);
  42.             }
  43.  
  44.  
  45.             lab[X][Y] = ' ';
  46.             l--;
  47.         }
  48.    
  49. }
  50.  
  51. void Solve()
  52. {
  53.     Task("BackTrack16");
  54.     string f1, S;
  55.     pt >> f1;
  56.     ifstream F(f1);
  57.  
  58.     int X, Y;
  59.    
  60.     pt >> N >> X >> Y >> Cx >> Cy;
  61.     X--; Y--;
  62.     Cx--; Cy--;
  63.     char **lab = new char*[N];
  64.     for (int i = 0;i < N;i++)
  65.         lab[i] = new char[N];
  66.  
  67.     string Tra;
  68.  
  69.     for (int i = 0;i < N;i++)
  70.     {
  71.     getline(F, S);
  72.         for (int j = 0;j < N;j++)
  73.             lab[i][j] = S[j];
  74.     }
  75.    
  76.     go(lab, Tra, X, Y);
  77.     pt << Ans;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement