我寫的代碼,代碼可以正常運行,但是結果是錯的,我沒有考慮人的位置,另外代碼還有一些問題,暫未找到。
import java.util.*;<br />public class Main1254 {<br />public static void main(String[] args) {<br />Scanner sc = new Scanner(System.in);<br />int t = sc.nextInt();<br />int m,n;<br />int[][] g;<br />BoxWorld bw;<br />while(0 != t--) {<br />m = sc.nextInt();<br />n = sc.nextInt();<br />g = new int[m+2][n+2];<br />//最外面多包裹一層牆<br />for(int i=0; i<m+2; i++) {<br />for(int j=0; j<n+2; j++) {<br />g[i][j] = 1;<br />}<br />}<br />for(int i=1; i<=m; i++) {<br />for(int j=1; j<=n; j++) {<br />g[i][j] = sc.nextInt();<br />}<br />}<br />bw = new BoxWorld(g);<br />System.out.println(bw.push(bw.cur, -1));<br />}<br />}<br />}<br />class BoxWorld {<br />int[][] graph;<br />Position cur, end;</p><p>public BoxWorld(int[][] g) { //初始化方格<br />graph = g;<br />for(int i=1; i<graph.length-1; i++) {<br />for(int j=1; j<graph[i].length-1; j++) {<br />if(graph[i][j] == 2) {<br />cur = new Position(i, j);<br />}<br />if(graph[i][j] == 3) {<br />end = new Position(i, j);<br />}<br />}<br />}<br />}</p><p>int push(Position cur, int steps) {<br />/* //下面寫法提示缺少返回語句<br />if(cur.equals(end)) {<br />return steps;<br />} else {<br />if(cur.canPushTo(0, 1)) { //up<br />return push(cur.to(0, 1), steps);<br />}<br />if(cur.canPushTo(0, -1)) { //down<br />return push(cur.to(0, 1), steps);<br />}<br />if(cur.canPushTo(-1, 0)) { //left<br />return push(cur.to(0, 1), steps);<br />}<br />if(cur.canPushTo(1, 0)) { //right<br />return push(cur.to(0, 1), steps);<br />}<br />steps ++;<br />}<br />*/<br />steps ++;<br />if(!cur.equals(end)) {<br />if(cur.canPushTo(0, 1)) { //up<br />return push(cur.to(0, 1), steps);<br />}<br />if(cur.canPushTo(0, -1)) { //down<br />return push(cur.to(0, -1), steps);<br />}<br />if(cur.canPushTo(-1, 0)) { //left<br />return push(cur.to(-1, 0), steps);<br />}<br />if(cur.canPushTo(1, 0)) { //right<br />return push(cur.to(1, 0), steps);<br />}<br />}<br />return steps;<br />}</p><p>class Position { //把箱子所在的位置定義成一個類<br />int x,y;<br />public Position(int _x, int _y) {<br />x = _x;<br />y = _y;<br />}<br />Position to(int xstep, int ystep) {<br />Position p = new Position(x+xstep, y+ystep);<br />return p;<br />}<br />public boolean equals(Object o) {<br />Position p = (Position)o;<br />if(this.x==p.x && this.y==p.y) {<br />return true;<br />} else {<br />return false;<br />}<br />}<br />boolean canPushTo(int xstep, int ystep) {<br />if(graph[this.x+xstep][this.y+ystep] == 1<br />|| graph[this.x-xstep][this.y-ystep] == 1) {<br />return false;<br />} else {<br />return true;<br />}<br />}<br />}<br />}
期待有人可以指點迷津……
網上的別人的C++代碼:
#include <iostream><br />#include <queue><br />using namespace std;<br />int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};<br />struct point<br />{<br /> int px;<br /> int py;<br /> int xx;<br /> int xy;<br /> int step;<br />};<br />queue<point> q;<br />queue<point> p;<br />int map[8][8];<br />int visit[8][8][8][8];<br />int hash[8][8][8][8];<br />int n, m, dx, dy, flag;</p><p>point input()<br />{<br /> int i, j;<br /> point temp;<br /> dx = 9, dy = 9;<br /> scanf("%d%d", &n, &m);<br /> temp.step = 0;<br /> for(i = 0; i < n ; i++)<br /> {<br /> for(j = 0; j < m; j++)<br /> {<br /> scanf("%d", &map[i][j]);<br /> if(map[i][j] == 4)<br /> {<br /> map[i][j] = 0;<br /> temp.px = i;<br /> temp.py = j;<br /> }<br /> if(map[i][j] == 2)<br /> {<br /> map[i][j] = 0;<br /> temp.xx = i;<br /> temp.xy = j;<br /> }<br /> if(map[i][j] == 3)<br /> {<br /> map[i][j] = 0;<br /> dx = i;<br /> dy = j;<br /> }<br /> }<br /> }<br /> return temp;<br />} </p><p>int PeopleTest(point temp)<br />{<br /> if(temp.px < 0 || temp.py < 0 || temp.px >= n || temp.py >= m)<br /> return 0;<br /> if(temp.px == temp.xx && temp.py == temp.xy)<br /> return 0;<br /> if(map[temp.px][temp.py] == 1)<br /> return 0;<br /> return 1;<br />}<br />void people_bfs(point temp, point tar)<br />{<br /> int i;<br /> point tt;<br /> memset(hash, 0, sizeof(hash));<br /> while(!p.empty())<br /> p.pop();<br /> p.push(temp);<br /> while(!p.empty())<br /> {<br /> temp = p.front();<br /> p.pop();<br /> tt = temp;<br /> if(tt.px == tar.px && tt.py == tar.py)<br /> {<br /> flag = 1;<br /> return;<br /> }<br /> for(i = 0; i < 4; i++)<br /> {<br /> tt.px = temp.px + dir[i][0];<br /> tt.py = temp.py + dir[i][1];</p><p> if(!PeopleTest(tt)) continue;</p><p> if(!hash[temp.xx][temp.xy][tt.px][tt.py])<br /> {<br /> p.push(tt);<br /> hash[temp.xx][temp.xy][tt.px][tt.py] = 1;<br /> }<br /> }<br /> }<br />}</p><p>int BoxTest(point tt)<br />{<br /> if(tt.xx >= n || tt.xy >= m || tt.xx < 0 || tt.xy < 0)<br /> return 0;<br /> if(map[tt.xx][tt.xy] == 1)<br /> return 0;<br /> return 1;<br />}<br />int box_bfs(point temp)<br />{<br /> int i;<br /> point tt;<br /> while(!q.empty())<br /> q.pop();<br /> q.push(temp);<br /> while(!q.empty())<br /> {<br /> temp = q.front();</p><p> q.pop();<br /> if(temp.xx == dx && temp.xy == dy)<br /> {<br /> return temp.step;<br /> }<br /> tt.step = temp.step + 1;<br /> for(i = 0; i < 4; i++)<br /> {<br /> tt.xx = temp.xx + dir[i][0];<br /> tt.xy = temp.xy + dir[i][1];</p><p> if( !BoxTest(tt) ) continue;</p><p> tt.px = temp.xx - dir[i][0];<br /> tt.py = temp.xy - dir[i][1];</p><p> if(tt.px < 0 || tt.py < 0 || tt.px >= n || tt.py >= m)<br /> continue;</p><p> if(map[tt.px][tt.py] == 1)<br /> continue;</p><p> flag = 0;<br /> people_bfs(temp, tt);<br /> if(flag)<br /> {<br /> tt.px = temp.xx;<br /> tt.py = temp.xy;<br /> if(!visit[tt.px][tt.py][tt.xx][tt.xy])<br /> {<br /> visit[tt.px][tt.py][tt.xx][tt.xy] = 1;<br /> q.push(tt);<br /> }<br /> }<br /> }<br /> }<br /> return -1;<br />}</p><p>point temp;</p><p>int main()<br />{<br /> int t;<br /> scanf("%d", &t);<br /> while(t--)<br /> {<br /> printf("%d/n", box_bfs( input() ));<br /> memset(visit, 0, sizeof(visit));<br /> }<br />}