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