[代码备份]poj1755

/*
 * 半成品,但是今天快写吐了,
 * 改天再来搞你.
 * *花式*小冬/Apr 4th. 2014/ooxx
 */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cassert>
#include <queue>
#include <vector>
using namespace std;

const int INF = 0xfffffff;
const int MAX = 32;
char Map[MAX][MAX];
int Move[4][2] = {-1,0, 0,1, 1,0, 0,-1};
char minDis_move_c[5] = "nesw";
char move_c[5] = "NESW";

struct Point {
	int x, y;
	int step;
	vector<char> path;
	bool operator==(const Point& p)const {
			return x == p.x && y == p.y;
	}
} minDis_path;
/* 用Node表示一个状态,这个状态包含如下要素:箱子的坐标,"我"的坐标,
 * 距起点走了多少步. 每次移动进行一次BFS获得最短路径.
 */
struct Node:public Point {
	int mx, my;
} B, E; //起点,终点
int r, c;

bool minDis_vis[MAX][MAX];
bool minDis_check(int x, int y, int bx, int by) {
	return x >= 0 && y >= 0 && x < r && y < c
			&& !minDis_vis[x][y] && Map[x][y] != '#'
			&& !(x == bx && y == by);
}

int minDis_BFS(int lx, int ly, int rx, int ry, int bx, int by) {
	memset(minDis_vis, false, sizeof(minDis_vis));
	Point from, to;
	from.x = lx , from.y = ly, from.step = 0;
	to.x = rx, to.y = ry;
	queue<Point> Q;
    Q.push(from);
    Point now, next;
    while (!Q.empty()) {
		now = Q.front(); Q.pop();
		if (now == to) {
			minDis_path = now;
			return now.step;
		}
		next = now; //next.step = now.step + 1;
		next.step = now.step + 1;
		for (int i = 0; i < 4; ++i) {
			int tmpx = now.x + Move[i][0];
			int tmpy = now.y + Move[i][1];
			if (minDis_check(tmpx, tmpy, bx, by)) {
				next.x = tmpx, next.y = tmpy;
				next.path.push_back(minDis_move_c[i]);
				Q.push(next);
				next.path.pop_back();
				minDis_vis[tmpx][tmpy] = true;
			}
		}
    }
    return -1;
}


bool vis[MAX][MAX];
Point getNode(int x, int y, int tx, int ty) { //把箱子从x,y移到tx,ty人应该到达甚么位置
	Point ans;
	if (x == tx) {
		ans.x = x;
        ans.y = y + (ty > y ? -1 : 1);
	} else {
		ans.y = y;
		ans.x = x + (tx > x ? -1 : 1);
	}
	return ans;
}
bool check(int x, int y) {
	return x >= 0 && y >= 0 && x < r && y < c
			&& !vis[x][y] && Map[x][y] != '#';
}
void TXZ(void) {
    int ans = INF;
    Node ans_path;
    queue<Node> Q;
    Node now, next;
    Q.push(B);
    while (!Q.empty()) {
        now = Q.front(); Q.pop();
        if (now == E) {
            if (now.step < ans) {
				ans = now.step;
				ans_path = now;
            }
            continue;
        }

		next = now;
        for (int i = 0; i < 4; ++i) {
            int tmpx = now.x + Move[i][0];
            int tmpy = now.y + Move[i][1];
            if (check(tmpx, tmpy)) {
				Point shell = getNode(now.x, now.y, tmpx, tmpy);
                if (shell.x < 0 || shell.y < 0
					|| shell.x >= r || shell.y >=c
					|| Map[shell.x][shell.y] == '#') continue;
                //minDis_path.path.clear();
				int minDis = minDis_BFS(now.mx, now.my,
						shell.x, shell.y, now.x, now.y);
				if (minDis < 0 || minDis + now.step > ans) continue;
				//printf("(%d,%d)->(%d,%d): %d\n", now.mx, now.my, shell.x, shell.y, minDis);
                //printf("%ld\n", minDis_path.path.size());
                next.mx = now.x, next.my = now.y;
                next.step = now.step + minDis + 1;
                next.x = tmpx, next.y = tmpy;
                for (int i = 0; i < minDis_path.path.size(); ++i) {
                    next.path.push_back(minDis_path.path[i]);
                }
                next.path.push_back(move_c[i]);
                Q.push(next);
                for (int i = -1; i < minDis_path.path.size(); ++i) {
					next.path.pop_back();
                }//弹出
            }
        }
    }
    if (ans == INF) {
		puts("Impossible");
		return;
    }
    for (int i = 0; i < ans_path.path.size(); ++i) {
		putchar(ans_path.path[i]);
    }
    putchar('\n');
}

int main() {
	while (~scanf(" %d %d", &r, &c) && r) {
        for (int i = 0; i < r; ++i) {
			scanf(" %s", Map[i]);
			memset(vis, false, sizeof(vis));
            B.step = 0;
			for (int j = 0; j < c; ++j) {
				if (Map[i][j] == '#' || Map[i][j] == '.') continue;
				if (Map[i][j] == 'B')
                    B.x = i, B.y = j;
				else if (Map[i][j] == 'T')
					E.x = i, E.y = j;
				else if (Map[i][j] == 'S')
					B.mx = i, B.my = j;
				Map[i][j] = '.';
			}
        }
        TXZ(); //我想解释,这个函数名只是"推箱子"的拼音...
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值