labyrinth

hello, I need to run the program labyrinth . A sample labyrinth and its representation with a two-dimensional array LAB. Wall cells are marked 1 and free ones 0. Three exits are LAB[1,6], LAB[7,6] and LAB[6,7]. The agent starts from the position X=5, Y=4 marked 2, i.e. LAB[5,4] = 2
I must print all the steps and one solution of board!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  const int M = 7, N = 7;
int LAB[M][N];
int CX[4], CY[4];
int L, X, Y, I, J, TRIAL;
bool YES;
 
void TRY(int X, int Y, bool &YES) 
{
  int K, U, V;
  if (X == 1 || X == M || Y == 1 || Y == N) YES = true;
  else
  {
    K = 0; 
    do
    {
        ++K;
        U = X + CX[K - 1];
        V = Y + CY[K - 1];
        if (LAB[U - 1][V - 1] == 0)
        {
            ++TRIAL;
            ++L;
            LAB[U - 1][V - 1] = L;
            TRY(U, V, YES);
            if (!YES)
            {
                LAB[U - 1][V - 1] = -1;
                --L; 
            }
        }
    } while (!(YES || K == 4)); 
  }
}
 
int main()
{
  for (J = N - 1; J <= 0; --J)
    for (I = 0; I < M; ++I)
      cin >> LAB[I][J];
 
  cin >> X >> Y;
  L = 2;
  LAB[X - 1][Y - 1] = L;
 
  CX[0] = -1; CY[0] = 0;
  CX[1] = 0; CY[1] = -1;
  CX[2] = 1; CY[2] = 0;
  CX[3] = 0; CY[3] = 1;
 
  YES = false; TRIAL = 0; 
 
  TRY(X, Y, YES); 
  if (YES) cout << "Path exists\n";
  else cout << "Path does not exist\n";
}
Topic archived. No new replies allowed.