ZOJ 3814 Sawtooth Puzzle BFS

感觉可以用BFS撸,然后就撸了,样例无限不过,代码能力真是弱。。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>

using namespace std;

const int bufsize = 128;
char buf[bufsize][bufsize];

struct Block {
	char str[10][10];
	void Rotate() {
		for (int i = 0; i < 8; i++) {
			for (int j = i + 1; j < 8; j++) {
				swap(str[i][j], str[j][i]);
			}
		}
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 4; j++) {
				swap(str[i][j], str[i][8 - j - 1]);
			}
		}
	}

	bool operator == (const Block &x) const {
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (str[i][j] != x.str[i][j]) {
					return false;
				}
			}
		}
		return true;
	}
};

void getBlock(Block block[9]) {
	for (int i = 0; i < 24; i++) {
		for (int j = 0; j < 24; j++) {
			scanf(" %c", &buf[i][j]);
		}
	}
	int id = 0;
	for (int i = 0; i < 24; i += 8) {
		for (int j = 0; j < 24; j += 8) {
			for (int dx = 0; dx < 8; dx++) {
				for (int dy = 0; dy < 8; dy++) {
					block[id].str[dx][dy] = buf[i + dx][j + dy];
				}
			}
			id++;
		}
	}
}

bool finalst[1 << 18];
Block ori[9], tar[9];
int e[9];

void make_hash(int pos, int val) {
	if (pos == -1) {
		finalst[val] = true;
		return;
	}
	for (int i = 0; i < 4; i++) {
		if (ori[pos] == tar[pos]) {
			make_hash(pos - 1, val << 2 | i);
		}
		ori[pos].Rotate();
	}
}

void input() {
	getBlock(ori); getBlock(tar);
	memset(e, 0, sizeof(e));
	for (int i = 0; i < 9; i++) {
		for (int j = 0; j < 4; j++) {
			int tmp; scanf("%d", &tmp);
			e[i] |= (tmp << j);
		}
	}
}

bool vis1[9];
const int dx[4] = { 0, -1, 0, 1 };
const int dy[4] = { -1, 0, 1, 0 };

int gete(int state, int pos) {
	int mask = 3 << (pos << 1), val = (state & mask) >> (pos << 1);
	int ret = e[pos];
	for (int i = 0; i < val; i++) {
		int bit = (ret & (1 << 3)) >> 3;
		ret &= (1 << 3) - 1;
		ret <<= 1; ret |= bit;
	}
	return ret;
}

void dfs(int &nstate, int state, int pos, int d) {
	vis1[pos] = true;

	int mask = 3 << (pos << 1), val = (state & mask) >> (pos << 1);
	if (d == 0) val = (val + 1) % 4;
	else val = (val + 3) % 4;
	nstate &= ~mask;  nstate |= val << (pos << 1);

	int x = pos / 3, y = pos % 3;
	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i], ny = y + dy[i], npos = nx * 3 + ny;
		if (nx < 0 || nx > 2 || ny < 0 || ny > 2) continue;
		if (vis1[npos]) continue;
		int e1 = gete(state, pos), e2 = gete(state, npos);
		if ((e1 & (1 << i)) && (e2 & (1 << ((i + 2) % 4)))) {
			dfs(nstate, state, npos, d ^ 1);
		}
	}
}

int Rotate(int state, int pos) {
	memset(vis1, 0, sizeof(vis1));
	int ret = state;
	dfs(ret, state, pos, 0);
	return ret;
}

bool vis[1 << 18];

void solve() {
	memset(finalst, 0, sizeof(finalst));
	memset(vis, 0, sizeof(vis));
	make_hash(8, 0);
	queue<int> q, qd;
	q.push(0); qd.push(0);
	vis[0] = true;
	while (!q.empty()) {
		int nowstate = q.front(); q.pop();
		int nowdist = qd.front(); qd.pop();;
		if (finalst[nowstate]) {
			printf("%d\n", nowdist);
			return;
		}
		for (int i = 0; i < 9; i++) {
			int nstate = Rotate(nowstate, i);
			if (!vis[nstate]) {
				q.push(nstate);
				qd.push(nowdist + 1);
				vis[nstate] = true;
			}
		}
	}
	puts("-1");
}

int main() {
	int T; scanf("%d", &T);
	while (T--) {
		input();
		solve();
	}
	return 0;
}

  

posted @ 2014-09-07 20:16  acm_roll  阅读(238)  评论(0编辑  收藏  举报