Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #pragma hdrstop
- #include "pt4.h"
- #include <fstream>
- #include <string>
- using namespace std;
- int Cx, Cy, N;
- int l = 0, L = 0;
- string Ans;
- void go(char **lab, string Tra, int X, int Y)
- {
- if ((X >= 0) && (Y >= 0) && (X < N) && (Y < N))
- if (lab[X][Y] == ' ')
- {
- lab[X][Y] = '+';
- l++;
- if (X == Cx && Y == Cy)
- {
- if (l > L)
- {
- L = l;
- Ans.assign(Tra);
- }
- }
- else
- {
- Tra.append("0");
- Tra[l - 1] = 'U';
- go(lab, Tra, X - 1, Y);
- Tra[l - 1] = 'D';
- go(lab, Tra, X + 1, Y);
- Tra[l - 1] = 'R';
- go(lab, Tra, X, Y + 1);
- Tra[l - 1] = 'L';
- go(lab, Tra, X, Y - 1);
- Tra.erase(l - 1, 1);
- }
- lab[X][Y] = ' ';
- l--;
- }
- }
- void Solve()
- {
- Task("BackTrack16");
- string f1, S;
- pt >> f1;
- ifstream F(f1);
- int X, Y;
- pt >> N >> X >> Y >> Cx >> Cy;
- X--; Y--;
- Cx--; Cy--;
- char **lab = new char*[N];
- for (int i = 0;i < N;i++)
- lab[i] = new char[N];
- string Tra;
- for (int i = 0;i < N;i++)
- {
- getline(F, S);
- for (int j = 0;j < N;j++)
- lab[i][j] = S[j];
- }
- go(lab, Tra, X, Y);
- pt << Ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement