All pastes #829149 Raw Edit

Unnamed

public text v1 · immutable
#829149 ·published 2007-12-22 23:13 UTC
rendered paste body
/*
ID: ferromr1
PROG: msquare
LANG: C++
*/
#include<cstdio>
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
#include<numeric>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<ctime>
#include<utility>
using namespace std;

#define full ((1 << 24) - 1)
#define sz 123456

ifstream fin ("msquare.in");
ofstream fout ("msquare.out");

int x, i, init, act, target, pos[10], l, r;
string ret;
queue <pair <int, string> > q;
map <int, bool> v;

void print (int x)
{
	for (i = 0; i < 8; ++i)
	{
		fout << (x & 7) << " ";
		x >>= 3;
	}
	fout << endl;
}

int A (int x)
{
	for (i = 0; i < 8; ++i)
	{
		int zero = full ^ 7, tmp = ((x >> ((7 - i) * 3)) & 7) ^ 7;
		x &= (zero << ((7 - i) * 3)) | ((1 << ((7 - i) * 3)) - 1);
		x |= tmp << ((7 - i) * 3);
	}
	return x & full;
}

int B (int x)
{
	for (i = 0; i < 8; ++i)
	{
		int zero = full ^ 7, tmp = (x >> ((7 - i) * 3)) & 7;
		if (tmp & 4)
		{
			if (tmp == 4) tmp = 7;
			else --tmp;
		}
		else
		{
			if (tmp == 3) tmp = 0;
			else ++tmp;
		}
		x &= (zero << ((7 - i) * 3)) | ((1 << ((7 - i) * 3)) - 1);
		x |= tmp << ((7 - i) * 3);
	}
	return x & full;
}

int C (int x)
{
	for (i = 0; i < 8; ++i)
	{
		int zero = full ^ 7, tmp = (x >> ((7 - i) * 3)) & 7;
		if (tmp == 1) tmp = 2;
		else
		if (tmp == 2) tmp = 5;
		else
		if (tmp == 5) tmp = 6;
		else
		if (tmp == 6) tmp = 1;
		x &= (zero << ((7 - i) * 3)) | ((1 << ((7 - i) * 3)) - 1);
		x |= tmp << ((7 - i) * 3);
	}
	return x & full;
}

int main ()
{
	for (i = 0; i < 8; ++i)
	{
		fin >> x;
		pos[x] = i;
		init = (init << 3) | i;
	}
	for (i = 1; i <= 8; ++i)
	{
		target = (target << 3) | pos[i];
	}
	l = r = 0;
	q.push (make_pair (init, ""));
	while (1)
	{
		act = q.front().first;
		ret = q.front().second;
		q.pop();
		if (act == target) break;
		if (v[act]) continue;
		v[act] = 1;
		q.push (make_pair (A(act), ret + "A"));
		q.push (make_pair (B(act), ret + "B"));
		q.push (make_pair (C(act), ret + "C"));
	}
	fout << ret.size() << endl;
	if (!(int)ret.size()) fout << endl;
	for (i = 0; i < (int)ret.size(); i += 60)
		fout << ret.substr (i, 60) << endl;
	return 0;
}