Study Notes:
1. the queue <node> queue = new queue list <node> (); can implement the queue function in Java. util.
2. Java does not support Operator overloading. In addition to basic data types, some of the obviously valid =, = also do not work here.
3. Understand the meaning and usage of static, final, and public.
It is also a Java program that looks like C ++. Don't bother with Java, I will change it slowly ~~
Push boxes to search people and boxes. The box search destination is used to search for people to see if they can reach the background of the box.
Import Java. util. *; <br/> class node {<br/> Public int X, Y, T, fr; <br/> node () {}< br/> node (INT _ x, int _ y, int _ t) {<br/> X = _ x; y = _ y; t = _ t; <br/>}< br/> Public void set (INT _ x, int _ y, int _ t) {<br/> X = _ x; y = _ y; T = _ t; Fr =-1; <br/>}< br/> }; </P> <p> Public classmain {</P> <p> static node man = new node (), box = new node (), target = new node (); <br/> static final int dir [] [] = {1, 0 },{ -1, 0}, {0,-1}, {0, 1 }}, maxn = 10; <br/> static int n, m; <br/> static int map [] [] = new int [maxn] [maxn]; <br/> static Boolean inmap (node N1) {<br/> return n1.x> = 0 & n1.y> = 0 & n1.x <n & n1.y <m; <br/>}</P> <p> static Boolean canreach (node N1) {</P> <p> If (n1.x = man. X & n1.y = man. y) return true; <br/> queue <node> queue = new queue list <node> (); <br/> queue. clear (); <br/> queue. A Dd (man); </P> <p> Boolean hash [] [] = new Boolean [n + 2] [M + 2]; <br/> hash [Man. x] [Man. y] = true; <br/> hash [box. x] [box. y] = true; <br/> node now, next = new node (); <br/> int I; <br/> while (queue. isempty () = false) {<br/> now = queue. remove (); <br/> for (I = 0; I <4; I ++) {<br/> next. set (now. X + dir [I] [0], now. Y + dir [I] [1], now. t + 1); <br/> If (inmap (next) & map [next. x] [next. y] = 0 & hash [next. x] [ne XT. y] = false) {<br/> hash [next. x] [next. y] = true; <br/> If (next. X = n1.x & next. y = n1.y) {<br/> return true; <br/>}< br/> queue. add (next); <br/>}< br/> return false; <br/>}</P> <p> static int BFS () {<br/> queue <node> queue = new queue list <node> (); <br/> queue. clear (); <br/> queue. add (box); </P> <p> Boolean hash [] [] [] = new Boolean [n + 2] [M + 2] [4]; <br/> node now, next = new node (), Back = new node (); </P> <p> int I; <br/> while (queue. isempty () = false) {<br/> now = queue. remove (); <br/> for (I = 0; I <4; I ++) {<br/> If (hash [now. x] [now. y] [I] = false) {<br/> next. set (now. X + dir [I] [0], now. Y + dir [I] [1], now. t + 1); <br/> back. set (now. x-dir [I] [0], now. y-dir [I] [1], now. t + 1); <br/> If (now. fr! =-1) {<br/> man. set (now. x-dir [now. fr] [0], now. y-dir [now. fr] [1], 0); <br/>}< br/> box = now; <br/> If (inmap (next) & inmap (back) & map [next. x] [next. y]! = 1 & canreach (back) {<br/> If (next. X = target. X & next. y = target. y) return next. t; <br/> next. fr = I; <br/> hash [now. x] [now. y] [I] = true; <br/> queue. add (next); <br/>}< br/> return-1; <br/>}< br/> Public static void main (string [] A) {<br/> int T, I, J; <br/> memory in = new memory (system. in); <br/> T = in. nextint (); </P> <p> while (t --! = 0) {<br/> N = in. nextint (); <br/> M = in. nextint (); <br/> for (I = 0; I <n; I ++) {<br/> for (j = 0; j <m; j ++) {<br/> map [I] [J] = in. nextint (); <br/> If (Map [I] [J] = 4) {<br/> man. set (I, j, 0); <br/> map [I] [J] = 0; <br/>} else if (Map [I] [J] = 2) {<br/> box. set (I, j, 0); <br/> map [I] [J] = 0; <br/>} else if (Map [I] [J] = 3) {<br/> target. set (I, j, 0); <br/> map [I] [J] = 0; <br/>}< br/> system. out. println (BFS (); <br/>}</P> <p >}< br/>}