學習筆記:
1.Queue < Node > queue = new LinkedList< Node >();可以實現隊列的功能,在java.util.*中。
2.java不支援運算子多載,除了基礎資料型別 (Elementary Data Type),有些顯然成立的=,==也在這裡不起作用。
3.搞清楚static,final,public等等的意義和用法。
又是一個長得像C++的java程式,java大牛不要痛苦,偶慢慢改~~
推箱子,分別對人和箱子進行廣搜。箱子搜尋目的地,對人搜尋是否能到達箱子背後。
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 LinkedList< Node >();<br />queue.clear();<br />queue.add(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][next.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 />}<br />}<br />return false;<br />}</p><p>static int BFS(){<br />Queue < Node > queue = new LinkedList< 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 />}<br />}<br />}<br />return -1;<br />}<br />public static void main(String []a){<br />int t , i , j;<br />Scanner in = new Scanner(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 />}<br />}<br />System.out.println(BFS());<br />}</p><p>}<br />}