Nightmare HDU 1072 BFS (the BFS that can go back, hash is a bit handsome)

Source: Internet
Author: User

/* <Br/> it seems that it is really a lot of water. If you have written a question in the past, TLE <br/> only remember to get a 6 in hash, which will be AC, but it does not mean that you will <br/> This hash [SX] [sy] = 6 is really a good method <br/> */# include <iostream> <br/> # include <cstdio> <br/> # include <cstring> <br/> # include <queue> <br/> using namespace STD; <br/> const int n = 12; <br/> int hash [N] [N]; <br/> int map [N] [N]; <br/> struct node <br/> {<br/> int X, Y, step; <br/> node (){}; <br/> node (int xx, int YY, int st): X (XX), y (yy), St EP (ST) {}; <br/>}; <br/> int DX [] = {0, 0, 1,-1 }; <br/> int dy [] = {1,-1, 0, 0 };< br/> int n, m; <br/> void BFS (INT Sx, int Sy, int ex, int ey) <br/>{< br/> node now, next; <br/> queue <node> q; <br/> q. push (node (sx, Sy, 0); <br/> hash [SX] [sy] = 6; // the start time is 6 <br/> while (! Q. empty () <br/>{< br/> now = Q. front (); <br/> q. pop (); <br/> for (INT I = 0; I <4; I ++) <br/> {<br/> next = now; <br/> next. X = now. X + dx [I]; <br/> next. y = now. Y + dy [I]; <br/> If (next. x <0 | next. x> = n | next. Y <0 | next. y> = M | hash [now. x] [now. y] <= 1 | map [next. x] [next. y] = 0) <br/> continue; <br/> next. step = now. step + 1; <br/> If (Map [next. x] [next. y] = 3) <br/>{< br/> printf ("% d/N", next. step); <br/> return; <br/>}< br/> If (hash [next. x] [next. y] <pash [now. x] [now. y]-1) <br/> {// The next point enters the queue only when the explosion time is shorter than the explosion time of now. <br/> hash [next. x] [next. y] = hash [now. x] [now. y]-1; <br/> If (Map [next. x] [next. y] = 4) <br/>{< br/> hash [next. x] [next. y] = 6; <br/> // next. time = 6; <br/>}< br/> q. push (next); <br/>}</P> <p >}< br/> printf ("-1/N "); <br/> return; <br/>}< br/> int main () <br/>{< br/> // freopen ("hxsh. in "," r ", stdin); // do not copy such a file all day. <br/> int T; <br/> scanf ("% d", & T); <br/> while (t --) <br/>{< br/> scanf ("% d", & N, & M); <br/> int Sx, Sy, ex, ey; <br/> for (INT I = 0; I <n; I ++) <br/> for (Int J = 0; j <m; j ++) <br/> {<br/> hash [I] [J] = 0; <br/> scanf ("% d", & map [I] [J]); <br/> If (Map [I] [J] = 2) <br/> {<br/> SX = I; <br/> Sy = J; <br/>}< br/> If (Map [I] [J] = 3) <br/>{< br/> ex = I; <br/> ey = J; <br/>}< br/> BFS (sx, Sy, ex, ey ); <br/>}< br/>} 

/*
The method draws on the hdoj Forum and has not dealt with any questions that can be taken back. Therefore, it is very tricky to deal,
The memory burst for the first time. For the output is-1, I initially considered a problem, but I don't know how to solve it.
Later, I came to the Forum and thought it was because of the endless loop caused by my code.
The deformation of BFS really makes people feel a little bit ..
*/
# Include <iostream> // 2399633 2010-04-29 19:31:23 accepted 1072 0 Ms 292 K 1915 B C ++ regret
# Include <cstdio>
# Include <cmath>
# Include <queue>
Using namespace STD;

Int Si, SJ;
Int EI, EJ;
Int num;
Int n, m;
Char map [10] [10];
Int hash [10] [10]; // This hash is a bit handsome...
Int dir [4] [2] = {1, 0}, {-1, 0}, {0, 1}, {0,-1 }};
Struct Node
{
Int time;
Int X;
Int y;
// Int count;
};

Int BFS (INT Sx, int SY)
{
Node N, P;
Int I;
Queue <node> q;
N. x = SX;
N. Y = sy;
N. Time = 0;
Hash [n. x] [N. Y] = 6; // perfect Processing
// N. Count = 6;
Q. Push (N );
While (! Q. Empty ())
{
N = Q. Front ();
/*
If (N. x = EI & N. Y = EJ & hash [n. x] [N. Y]> 0)
{
Printf ("% d/N", N. time );
Return 1;
}
*/
/*
If (N. Time> = num)
{
Break;
}
*/
Q. Pop ();
For (I = 0; I <4; I ++)
{
P. x = n. x + dir [I] [0];
P. Y = n. Y + dir [I] [1];
// P. Count = n. Count-1;
P. Time = n. Time + 1;
If (hash [n. x] [n. y] <= 1 | P. x <0 | P. x> = n | P. Y <0 | P. y> = M | map [p. x] [p. y] = '0 ')
Continue;
/*
If (Map [p. x] [P. Y] = '1' | map [p. x] [P. Y] = '3 ')
{
Q. Push (P );
If (hash [p. x] [P. Y] Hash [p. x] [P. Y] = hash [n. x] [N. Y]-1;

}
*/
If (Map [p. x] [P. Y] = '3 ')
{
Printf ("% d/N", p. time );
Return 1;
}
If (hash [p. x] [P. Y] {
Hash [p. x] [P. Y] = hash [n. x] [N. Y]-1;
If (Map [p. x] [P. Y] = '4 ')
{
// P. Count = 6;
Hash [p. x] [P. Y] = 6;
// Q. Push (P );
}
Q. Push (P );
}

}

}
Printf ("-1/N ");
Return 1;

}

Int main ()
{
Int T;
Int I, J;
Cin> T;
While (t --)
{
Cin> N> m;
For (I = 0; I <n; I ++)
For (j = 0; j <m; j ++)
{
Cin> map [I] [J];
// Hash [I] [J] = 6;
If (Map [I] [J] = '2 ')
{
SI = I;
SJ = J;
}
If (Map [I] [J] = '3') // useless
{
Ei = I;
J = J;
}
}
// Num = 2 * (INT) (ABS (1.0 * si-EI) + ABS (1.0 * Sj-EJ ));
Memset (hash,-1, sizeof (hash ));
BFS (Si, SJ );
}
}

Related Article

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.