HDU 1253 (simple BFS)

Source: Internet
Author: User

 

 

It's easy to use a BFs. Pay attention to it and cut it over ~~ 6 minutes to complete code debugging. 5 minutes later, it's still amazing ~~~

 

The following is a problem with the HDU server code. the maximum time for each submission is 765 ms or a small value.

 

Of course, it can be further optimized. For example, bit operation processing may take more than twice as long as it is too time-consuming to write.

 

 

# Include <iostream>
# Include <queue>
Using namespace STD;

Typedef struct
{
Long Mi, MJ, MK;
Long step;
} Node;

Long hash [50] [50] [50];
Long map [50] [50] [50];
Long A, B, C, T;
Node end;
Node start;

Inline void BFS ()
{

Queue <node> q;
Long cost = int_max;
Fill (& hash [0] [0] [0], & hash [50] [0] [0], int_max );
Q. Push (start );

Node T;

While (! Q. Empty ())
{
T = Q. Front ();
Q. Pop ();

If (T. Step> T)
{
Break;
}

If (ABS (T. mi-End.mi) + ABS (T. mj-End.mj) + ABS (T. mk-End.mk)> T)
{
Break;
}

If (T. Step> = hash [T. Mi] [T. MJ] [T. mk])
{
Continue;
}

Hash [T. Mi] [T. MJ] [T. mk] = T. step;

If (T. MI = end. Mi & T. mj = end. MJ & T. mk = end. mk)
{
If (T. Step <cost)
{
Cost = T. step;
Break;
}
Continue;
}

Node N;

If (T. Mi> 0)
{
N. MI = T. mi-1;
N. mj = T. MJ;
N. mk = T. mk;

If (! Map [n. Mi] [n. MJ] [n. mk])
{
N. Step = T. Step + 1;
If (T. Step <= T)
{
Q. Push (N );
}
}
}

If (T. Mi <A-1)
{
N. MI = T. Mi + 1;
N. mj = T. MJ;
N. mk = T. mk;

If (! Map [n. Mi] [n. MJ] [n. mk])
{
N. Step = T. Step + 1;
If (T. Step <= T)
{
Q. Push (N );
}
}
}

If (T. MJ> 0)
{
N. MI = T. Mi;
N. mj = T. MJ-1;
N. mk = T. mk;

If (! Map [n. Mi] [n. MJ] [n. mk])
{
N. Step = T. Step + 1;
If (T. Step <= T)
{
Q. Push (N );
}
}
}

If (T. MJ <B-1)
{
N. MI = T. Mi;
N. mj = T. MJ + 1;
N. mk = T. mk;

If (! Map [n. Mi] [n. MJ] [n. mk])
{
N. Step = T. Step + 1;
If (T. Step <= T)
{
Q. Push (N );
}
}
}

If (T. mk> 0)
{
N. MI = T. Mi;
N. mj = T. MJ;
N. mk = T. MK-1;

If (! Map [n. Mi] [n. MJ] [n. mk])
{
N. Step = T. Step + 1;
If (T. Step <= T)
{
Q. Push (N );
}
}
}

If (T. mk <C-1)
{
N. MI = T. Mi;
N. mj = T. MJ;
N. mk = T. mk + 1;

If (! Map [n. Mi] [n. MJ] [n. mk])
{
N. Step = T. Step + 1;
If (T. Step <= T)
{
Q. Push (N );
}
}
}
}

If (cost = int_max)
{
Puts ("-1 ");
}
Else
{
Printf ("% LD \ n", cost );
}

}

Int main ()
{

Long K;
Scanf ("% lD", & K );
Start. MI = start. mj = start. mk = 0;
Start. Step = 0;
While (k --)
{
Scanf ("% LD % lD", & A, & B, & C, & T );
End. MI = A-1;
End. mj = B-1;
End. mk = C-1;
Long I, J, K;
For (I = 0; I <A; ++ I)
{
For (j = 0; j <B; ++ J)
{
For (k = 0; k <C; ++ K)
{
Scanf ("% lD", & map [I] [J] [k]);
}
}
}
BFS ();
}
Return 0;
}

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.