java與acm—–java版搜尋(hdu 1254 )–推箱子

來源:互聯網
上載者:User

學習筆記:

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 />}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.