zoj 3814

来源:互联网 发布:新版mac如何强制关机 编辑:程序博客网 时间:2024/06/15 12:27

好小的一个错误找了好久

#pragma comment(linker, "/STACK:102400000,102400000")#include<iostream>#include<vector>#include<algorithm>#include<cstdio>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<cmath>#include<cassert>#include<cstring>#include<iomanip>using namespace std;#ifdef _WIN32#define i64 __int64#define out64 "%I64d\n"#define in64 "%I64d"#else#define i64 long long#define out64 "%lld\n"#define in64 "%lld"#endif/************ for topcoder by zz1215 *******************/#define foreach(c,itr)  for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)#define FFD(i,a,b)      for( int i = (a) ; i >= (b) ; i --)#define S64(a)          scanf(in64,&a)#define SS(a)           scanf("%d",&a)#define LL(a)           ((a)<<1)#define RR(a)           (((a)<<1)+1)#define pb              push_back#define pf              push_front#define X               first#define Y               second#define CL(Q)           while(!Q.empty())Q.pop()#define MM(name,what)   memset(name,what,sizeof(name))#define MC(a,b)memcpy(a,b,sizeof(b))#define MAX(a,b)        ((a)>(b)?(a):(b))#define MIN(a,b)        ((a)<(b)?(a):(b))#define read            freopen("out.txt","r",stdin)#define write           freopen("out2.txt","w",stdout)const int inf = 0x3f3f3f3f;const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;const double oo = 10e9;const double eps = 10e-9;const double pi = acos(-1.0);const int maxs = 1 << 18;const int dir[4][2] = { { 0, -1 }, { -1, 0 }, { 0, 1 }, { 1, 0 } };char cs[24][24];char ce[24][24];int dp[maxs];int mov[1 << 12][9];int nxt[2][maxs][9];int have[9][4];int h[9];int hx[9];int a[3][3];int sign[9];int cor[9][9];vector<int>tx[9];bool vis[maxs];vector<int>g[9];int ary[12] = { 0, 1, 3, 4, 6, 7, 0, 1, 2, 3, 4, 5 };void build(int x){int now, to;for (int i = 0; i < 9; i++){g[i].clear();}for (int i = 0; i < 6; i++){if (x&(1 << i)){now = ary[i];to = now + 1;g[now].push_back(to);g[to].push_back(now);}}for (int i = 6; i < 12; i++){if (x &(1 << i)){now = ary[i];to = now + 3;g[now].push_back(to);g[to].push_back(now);}}for (int i = 0; i < 9; i++){int vis = 0;queue<int>q;while (!q.empty()) q.pop();q.push(i);vis |= (1 << i);while (!q.empty()){now = q.front();q.pop();for (int i = 0; i < g[now].size(); i++){to = g[now][i];if (!(vis & (1 << to))){vis |= (1 << to);q.push(to);}}}mov[x][i] = vis;}}int gao(int now, int x, int add){int to = now;int temp = now &(3 << (x * 2));temp >>= (x * 2);temp += add + 4;temp %= 4;temp <<= (x * 2);to &= ~(3 << (x * 2));to |= temp;return to;}void init(){int temp = 0;for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){if ((i + j) % 2) {sign[temp] = 1;}else{sign[temp] = -1;}a[i][j] = temp++;}}for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){if (sign[i] == sign[j]){cor[i][j] = 0;}else{cor[i][j] = 1;}}}for (int i = 0; i < (1 << 12); i++){build(i);}for (int i = 0; i < maxs; i++){for (int j = 0; j < 9; j++){nxt[0][i][j] = gao(i, j, 1);nxt[1][i][j] = gao(i, j, -1);}}}void change(int sx, int sy){int temp[8][8];for (int i = 0; i < 8; i++){for (int j = 0; j < 8; j++){temp[j][7 - i] = cs[i + sx][j + sy];}}for (int i = 0; i < 8; i++){for (int j = 0; j < 8; j++){cs[i + sx][j + sy] = temp[i][j];}}}bool ok(int sx, int sy){for (int i = sx; i < sx + 8; i++){for (int j = sy; j < sy + 8; j++){if (cs[i][j] != ce[i][j]) {return false;}}}return true;}void geth(int x){int temp;for (int i = 0; i < 9; i++){temp = x&(3 << (i * 2));temp >>= i * 2;hx[i] = h[i];for (int step = 1; step <= temp; step++){hx[i] <<= 1;if (hx[i] & (1 << 4)){hx[i] |= 1;}}}}int gets(int x){geth(x);int re = 0;for (int i = 0; i < 6; i++){int now = ary[i];int to = now + 1;if ((hx[now] & (1 << 2)) && (hx[to] & 1)){re |= 1 << i;}}for (int i = 6; i < 12; i++){int now = ary[i];int to = now + 3;if ((hx[now] & (1 << 3)) && (hx[to] & (1 << 1))){re |= 1 << i;}}return re;}int bfs(){MM(dp, -1);int now, to, ts, temp;queue<int>q;q.push(0);dp[0] = 0;while (!q.empty()){now = q.front();if (vis[now]) return dp[now];q.pop();ts = gets(now);for (int i = 0; i < 9; i++){temp = mov[ts][i];to = now;for (int j = 0; j < 9; j++){if (temp&(1 << j)){to = nxt[cor[i][j]][to][j];}}if (dp[to] == -1){dp[to] = dp[now] + 1;q.push(to);}}}return -1;}bool you;void dfs(int x = 0, int step = 0){if (step == 9){vis[x] = true;you = true;return;}else{for (int i = 0; i < tx[step].size(); i++){dfs(x|tx[step][i], step + 1);}}}int start(){MM(vis, false);for (int i = 0; i < 9; i++){tx[i].clear();}for (int x = 0; x < 3; x++){for (int y = 0; y < 3; y++){int temp = 0;while (true){if (temp == 4) break;if (ok(x * 8, y * 8)){tx[a[x][y]].push_back(temp << (a[x][y] * 2));}change(x * 8, y * 8);temp++;}}}you = false;dfs();if (!you) return -1;return bfs();}int main(){init();int T;cin >> T;while (T--){for (int i = 0; i < 24; i++){for (int j = 0; j < 24; j++){cin >> cs[i][j];}}for (int i = 0; i < 24; i++){for (int j = 0; j < 24; j++){cin >> ce[i][j];}}for (int i = 0; i < 9; i++){h[i] = 0;for (int j = 0; j < 4; j++){cin >> have[i][j];h[i] |= have[i][j] << j;}}int ans = start();cout << ans << endl;}return 0;}


0 0
原创粉丝点击