3520. Step out of maze tju

Source: Internet
Author: User

/*
This is the second exercise competition for summer training. This topic gave me the first impression of DFS.
However, DFS is my weakness, so it has been stuck for a long time, and there is no return in the useless path of DFS.
The second game ended in a tragedy.
I gradually began to understand that many questions have similarities. For example, this is similar to hdu1026, but I didn't
Think of BFS. If you think of BFs at the beginning, I think I can do it.
There are still many details to be aware of during the next day's knock, but PE 3 is more
I believe that I will be more mature after summer vacation
*/
# Include <iostream>
# Include <cstdio>
# Include <queue>
# Include <stack>
Using namespace STD;

Int map [105] [105];
Bool hash [105] [105];
Int dir [4] [2] = {0, 1}, {1, 0}, {-1, 0}, {0,-1 }};
Int n, m;
Bool flag;
Int PT [10000];
Int Len;

Struct Node
{
Int X;
Int y;
Int pre;
Int di;
} Path [10000];
Void BFS (int x, int y)
{
Queue <node> q;
Node P, N;
Int T = 0;
Memset (hash, false, sizeof (hash ));
/*
For (INT I = 0; I <4; I ++)
{
P. x = x + dir [I] [0];
P. Y = Y + dir [I] [1];
P. Pre = T;
P. DI = I;
If (Map [p. x] [P. Y] = 0)
{
Hash [p. x] [P. Y] = true;
Q. Push (P );
}
}
*/
P. x = X;
P. Y = y;
P. Pre =-1;
Q. Push (P );
While (! Q. Empty ())
{
N = Q. Front ();
Path [T] = N;
If (N. x <1 | n. x> N | N. Y <1 | N. Y> m)
{
Stack <int> S;
// Int TT = n. Pre;
Int d = n. Di;
S. Push (d );
Int TT = n. Pre;
While (TT! = 0)
{
S. Push (d );
Tt = path [TT]. Pre;
D = path [TT]. Di;
}
D = S. Top ();
S. Pop ();
If (D = 0)
Cout <"R ";
Else if (D = 1)
Cout <"D ";
Else if (D = 2)
Cout <"U ";
Else if (D = 3)
Cout <"L ";
While (! S. Empty ())
{
D = S. Top ();
S. Pop ();
If (D = 0)
Cout <"R ";
Else if (D = 1)
Cout <"D ";
Else if (D = 2)
Cout <"U ";
Else if (D = 3)
Cout <"L ";
}
Cout <Endl;
Return;

}

Q. Pop ();
For (INT I = 0; I <4; I ++)
{
P. x = n. x + dir [I] [0];
P. Y = n. Y + dir [I] [1];
P. DI = I;
P. Pre = T;
If (! Hash [p. x] [P. Y] & map [p. x] [P. Y] = 0)
{
Q. Push (P );
Hash [p. x] [P. Y] = true;
}
}
T ++;
}

}

Void DFS (int x, int y, int di)
{
 
If (x <1 | x> N | Y <1 | Y> m)
{
Flag = true;
Return;
}
 
For (INT I = 0; I <4; I ++)
{
Int X1 = x + dir [I] [0];
Int Y1 = Y + dir [I] [1];
DI = I;
If (FLAG)
Return; // during the competition, I forgot to add return here, and output the redundant path, which cannot be changed.
If (Map [X1] [Y1] = 0)
{
Map [x] [Y] = 1;
DFS (x1, Y1, DI );
If (FLAG)
{
PT [Len ++] = di;
/*
If (DI = 0)
Cout <"R ";
Else if (DI = 1)
Cout <"D ";
Else if (DI = 2)
Cout <"U ";
Else if (DI = 3)
Cout <"L ";
*/
}
Map [x] [Y] = 0;

}
}
 
 
}

Int main ()
{
Int T;
Cin> T;
While (t --)
{
Cin> N> m;
Memset (MAP, 0, sizeof (MAP ));
For (INT I = 1; I <= N; I ++)
For (Int J = 1; j <= m; j ++)
Cin> map [I] [J];
Int X, Y;
Cin> x> Y;
Flag = false;
Len = 0;
DFS (X, Y, 0 );
If (PT [Len-1] = 0)
Cout <"R ";
Else if (PT [Len-1] = 1)
Cout <"D ";
Else if (PT [Len-1] = 2)
Cout <"U ";
Else if (PT [Len-1] = 3)
Cout <"L ";
For (INT I = len-2; I> = 0; I --)
{
If (PT [I] = 0)
Cout <"R ";
Else if (PT [I] = 1)
Cout <"D ";
Else if (PT [I] = 2)
Cout <"U ";
Else if (PT [I] = 3)
Cout <"L ";
}
Cout <Endl;

// BFS (x, y );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.