您的位置:首页 > 其它

BFS ZOJ 3814 Sawtooth Puzzle

2014-09-20 12:00 344 查看
Sawtooth Puzzle

Time Limit: 10 Seconds Memory Limit: 65536 KB

Recently, you found an interesting game called Sawtooth Puzzle. This is a single-player game played on a grid with 3 x 3 cells. Each cell contains a part of an image. Besides,
each edge of a cell will be either smooth or saw-toothed. Two edge-adjacent cells are considered as meshed if their adjacent edges are both saw-toothed.
The game contains zero or more steps. At each step, the player can choose one cell, rotate it by 90 degrees clockwise. At the beginning of rotation, any cell which is going to rotate
will drive other cells to rotate together if they are meshed. Of course, the driven (passive) cell will rotate in the opposite direction against the driver (active) cell. The following image demonstrates the procedure of rotating the middle cell clockwise:



Given the initial image and target image of the puzzle, please find out the minimum number of steps needed to solve it.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The input data consists of three parts. The first part represents the initial image of the puzzle. It contains 26 lines. Each line has 26 characters (excluding the line break '\n'). The
image is composed of 3 x 3 cells. Each cell is a monochrome image of 8 x 8 pixels ('#' for black, '.' for white). These cells are separated by space characters of 1 pixel width.
The second part represents the target image of the puzzle. It has the same format as which described above.
The last part contains 9 lines. Each line has 4 integers indicating the edge type (0 for smooth, 1 for saw-toothed) of a cell. The cells are listed in the order of top-left, top-middle,
top-right, middle-left, ... , till bottom-right. For each cell, the edge type are given in the order of left, top, right and bottom.
There is an empty line between any two adjacent parts (including of different cases).

Output

For each test case, output the minimum number of steps needed to solve the puzzle. If there is no solution, output -1 instead.

Sample Input

1
......#. ........ ........
......## ........ ........
.......# #....... ........
.......# ##...... ...##...
.......# ###..... .###....
.......# ####...# ####....
.......# ######## ###.....
.......# ######## ###.....

........ ######## ###.....
.......# ######## ##......
.....### ######## ##......
....#### ######## ###.....
..###### ######## ####....
######## ######## #####...
...##### ######## ######..
........ ######## #######.

........ ........ ..######
........ .....### ........
........ ######## ........
........ .####### ........
........ ...##### ........
........ .....### ........
........ .......# ........
........ ........ ........

......#. ........ ........
......## ........ ........
.......# #....... ........
.......# ##...... ...##...
.......# ###..... .###....
.......# ####...# ####....
.......# ######## ###.....
.......# ######## ###.....

........ ######## ###.....
.......# ######## ##......
.....### ######## ##......
....#### ######## ###.....
..###### ######## ####....
######## ######## #####...
...##### ######## ######..
........ ######## #######.

........ .######. ..######
........ .#####.. ........
........ .#####.. ........
........ ..###... ........
........ ..###... ........
........ ..##.... ........
........ ..##.... ........
........ ..#..... ........

0 1 0 1
0 0 1 1
1 0 0 1
0 0 1 1
1 1 1 0
1 0 1 1
1 1 1 1
0 0 1 0
0 1 1 0

Sample Output

6

Hint

Here is the detailed solution to the sample input: (1,2) (1,2) (1,2) (1,2) (1,3) (3,1).
This problem comes from an interesting game. You can play it here (Level
2-2).

题意:我就不多说了,你玩一下下面给的链接的那个游戏,在第二行第二列。

思路:其实我们将状态处理一下就好了,一个8*8的格子只有四种状态,所以总的状态数只有4^9,然后转移的时候我们只需要处理出转动一个后,哪些会联动起来,注意到从第一个转的出发,距离为偶数的是顺时针,奇数是逆时针,只要状态的表示处理好了,题目就不难了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define rrep(i,b,a) for(int i=(b);i>=(int)(a);--i)
#define eps 1e-9
#define clr(a,x) memset(a,x,sizeof(a))
#define LL long long
const int maxn = 100000+5;
char S[9][8][10],T[9][8][10];
const int Move[2][4] = { {0,-1,0,1},{-1,0,1,0} };
vector<int> goal[9];

inline int getx(int r,int c) { return (r*3)+c; }
inline int row(int x) { return x / 3; }
inline int col(int x) { return x % 3; }
inline bool inRange(int r,int c) { return 0 <= r && r < 3 && 0<=c && c < 3; }
int d[20];
int q[20],front,rear;

struct State
{
int a[3][3];
int saw[3][3];

void out_saw()
{
puts("");
rep(i,0,3) {
rep(j,0,3) printf("%3d",saw[i][j]);
puts("");
}
puts("");
}
bool isFinal() const
{
rep(i,0,9) {
bool ok =false;
rep(j,0,goal[i].size()) {
if (goal[i][j] == a[row(i)][col(i)]) {
ok = true;
break;
}
}
if(!ok) return false;
}
return true;
}

void out()
{
puts("");
rep(i,0,3) {
rep(j,0,3) printf("%2d",a[i][j]);
puts("");
}
puts("");
}
int key;
int mask = (1<<18)-1;
void rotate(int r,int c,int d)
{
if (d & 1) {
saw[r][c] = (((saw[r][c] & 1) << 4) | saw[r][c]) >> 1;
--a[r][c];
key &= mask ^ (3<<(getx(r,c)<<1));
if (a[r][c] < 0) a[r][c] += 4;
key |= a[r][c] << (getx(r,c)<<1);
} else {
saw[r][c] = (((saw[r][c] & (1<<3)) >> 3) | (saw[r][c] << 1)) & ((1<<4)-1);
++a[r][c];
key &= mask ^ (3<<(getx(r,c)<<1));
if (a[r][c] >= 4) a[r][c] -= 4;
key |= a[r][c] << (getx(r,c)<<1);
}
}
void expand(int r,int c)
{
clr(d,-1); int u = getx(r,c);
d[u] = 0;
front = rear = 0;
q[rear++] = u;
while (front < rear) {
u = q[front++];
r = row(u), c = col(u);
rep(i,0,4) {
if ((saw[r][c] & (1<<i)) == 0) continue;
int rr = r + Move[0][i];
int cc = c + Move[1][i];
int v = getx(rr,cc);
if(!inRange(rr,cc) || d[v] != -1 || (saw[rr][cc] & (1<<((i+2)%4))) == 0) continue;
d[v] = d[u] + 1;
q[rear++] = v;
}
}
rep(i,0,9)
if (d[i] != -1) rotate(row(i),col(i),d[i]&1);
}
}start;

bool equal(char S[8][10],char T[8][10])
{
rep(i,0,8) rep(j,0,8)
if(S[i][j] != T[i][j]) return false;
return true;
}

void input()
{
rep(j,0,8) {
rep(i,0,3) scanf("%s",S[i][j]);
}
rep(j,0,8) {
rep(i,3,6) scanf("%s",S[i][j]);
}
rep(j,0,8) {
rep(i,6,9) scanf("%s",S[i][j]);
}

rep(j,0,8) {
rep(i,0,3) scanf("%s",T[i][j]);
}
rep(j,0,8) {
rep(i,3,6) scanf("%s",T[i][j]);
}
rep(j,0,8) {
rep(i,6,9) scanf("%s",T[i][j]);
}

clr(start.a,0);
clr(start.saw,0);
rep(i,0,3) rep(j,0,3) {
rep(k,0,4) {
int x; scanf("%d",&x);
start.saw[i][j] |= x << k;
}
}
start.key = 0;
}

void Rotate(char S[8][10])
{
char _T[8][10];
rep(i,0,8) rep(j,0,8) _T[i][j] = S[7-j][i];
rep(i,0,8) rep(j,0,8) S[i][j] = _T[i][j];
}

void output(char S[8][10])
{
puts("");
rep(i,0,8) printf("%s\n",S[i]);
puts("");
}

void pre_init()
{
rep(i,0,9) {
goal[i].clear();
rep(j,0,4) {
if (equal(S[i],T[i]))
goal[i].push_back(j);
//    output(S[i]);
Rotate(S[i]);
}
}
}

int dist[1<<19];
void solve()
{
pre_init();
queue<State> Que;
Que.push(start);
clr(dist,-1); dist[0] = 0;
while (Que.size()) {
State S = Que.front(); Que.pop();
int u = S.key;
if (S.isFinal()) {
printf("%d\n",dist[u]);
return;
}
rep(r,0,3) rep(c,0,3) {
State now = S;
now.expand(r,c);
//    now.out_saw();
int v = now.key;
//   now.out();
if (dist[v] == -1) {
dist[v] = dist[u] +1;
Que.push(now);
}
}
}
puts("-1");
}

int main()
{
int T; cin >> T;
while (T--) {
input();
solve();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: