Hdu 1254 push box

Source: Internet
Author: User

First, perform BFS according to the box, and then judge the opposite side of the box. Whether a person can arrive, that is, conduct DFS on the person ..

The following is the AC code:

[Cpp]
# Include <iostream>
# Include <cstring>
# Include <queue>
 
Using namespace std;
Const int MAX = 10;
Int map [MAX] [MAX];
Int dir [4] [2] = {}, {}, {0,-1 }};
Int other_dir [4] [2] = {0,-1}, {-}, {}, {}; // reverse direction
Bool vis [MAX] [MAX] [MAX] [MAX];
Bool mark [MAX] [MAX];
Int flag, ans;
Struct node
{
Int ps_x, ps_y;
Int bk_x, bk_y;
Int step;
Int cur_map [MAX] [MAX];
} S_pos;
Int end_x, end_y;
Int m, n;
Void init ()
{
S_pos.step = 0;
For (int I = 0; I <m; I ++) for (int j = 0; j <n; j ++)
{
If (map [I] [j] = 2) s_pos.bk_x = I, s_pos.bk_y = j;
Else if (map [I] [j] = 3) end_x = I, end_y = j;
Else if (map [I] [j] = 4) s_pos.ps_x = I, s_pos.ps_y = j;
S_pos.cur_map [I] [j] = map [I] [j];
}
Memset (vis, 0, sizeof (vis ));
}
Bool cheak (int x, int y ){
If (x> = 0 & x <m & y> = 0 & y <n & map [x] [y]! = 1) return true;
Return false;
}
Int can;
Void dfs (int p_x, int p_y, int B _x, int B _y, int now_map [10] [10]) // determines whether a person can reach the side of the box.
{
If (can) return;
If (p_x = B _x & p_y = B _y) {can = 1; return;} // cout <p_x <"" <p_y <endl;
For (int I = 0; I <4; I ++ ){
Int x = p_x + dir [I] [0], y = p_y + dir [I] [1];
If (cheak (x, y) & now_map [x] [y]! = 2 &&! Mark [x] [y]) {
Mark [x] [y] = true; dfs (x, y, B _x, B _y, now_map; mark [x] [y] = false;
}
}
}
Void bfs ()
{
Init (); queue <node> q;
Vis [s_pos.bk_x] [s_pos.bk_y] [s_pos.ps_x] [s_pos.ps_y] = true;
Q. push (s_pos );
 
While (! Q. empty ()){
Node now = q. front ();
Q. pop ();
If (now. bk_x = end_x & now. bk_y = end_y ){
Flag = 1, ans = now. step; return;
}
For (int I = 0; I <4; I ++ ){
Node next = now;
Next. bk_x + = dir [I] [0], next. bk_y + = dir [I] [1]; next. step + = 1;
Int x = now. bk_x + other_dir [I] [0], y = now. bk_y + other_dir [I] [1];
If (cheak (x, y) & cheak (next. bk_x, next. bk_y )&&! Vis [next. bk_x] [next. bk_y] [now. bk_x] [now. bk_y])
{
Memset (mark, 0, sizeof (mark); mark [next. ps_x] [next. ps_y] = true;
Can = 0;
Dfs (next. ps_x, next. ps_y, x, y, now. cur_map );
If (can ){
Vis [next. bk_x] [next. bk_y] [now. bk_x] [now. bk_y] = true; // cout <next. bk_x <"" <next. bk_y <endl;
Next. ps_x = now. bk_x;
Next. ps_y = now. bk_y;
Next. cur_map [next. bk_x] [next. bk_y] = 2;
Next. cur_map [now. bk_x] [now. bk_y] = 0;
Q. push (next );
}
}
}
}
}
Int main ()
{
 
Int t, I, j;
Cin> t;
While (t --)
{
Cin> m> n;
For (I = 0; I <m; I ++) for (j = 0; j <n; j ++) cin> map [I] [j];
Flag = 0;
Ans =-1;
Bfs ();
If (flag) cout <ans <endl;
Else cout <-1 <endl;
}
Return 0;
 
}


Author: w00w12l

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.