#include <iostream>
#include <fstream>
#include <queue>
#include <algorithm>
using namespace std;
long int src;
long int des;
const int MAX = 1 << 16;
int num[MAX];
int how[MAX];
bool flag = false;
void search()
{
queue<long int> q;
num[src] = 0;
q.push(src);
while(!q.empty())
{
long int enode = q.front();
q.pop();
for(int j=0, mode=3; j<16; j++, mode<<=1)
{
if(j%4!=3 && (((enode & mode)>>j==1) || (enode & mode)>>j==2))
{
long int now = enode ^ mode;
if(num[now] == -1)
{
num[now] = num[enode] + 1;
how[now] = j;
q.push(now);
}
}
}
for(j=0, mode=17; j<12; j++, mode<<=1)
{
if(((enode & mode)>>j==1) || (enode & mode)>>j==16)
{
long int now = enode ^ mode;
if(num[now] == -1)
{
num[now] = num[enode] + 1;
how[now] = -j-1;
q.push(now);
}
}
}
if(num[des] != -1)
{
flag = true;
break;
}
}
}
void output(long int node)
{
if(node == src)
return;
long int last = node;
if(how[node] >=0)
last ^= 3 << how[node];
else
last ^= 17 << (-how[node]-1);
output(last);
if(how[node] >=0)
cout << (how[node]/4+1) << " " << (how[node]%4+1) << " " << (how[node]/4+1) << " " << (how[node]%4+2) << endl;
else
cout << (how[node]/4+1) << " " << (how[node]%4+1) << " " << (how[node]/4+2) << " " << (how[node]%4+1) << endl;
}
int main()
{
int a, b;
ifstream fin("图形变换.txt");
cout << "输入图形A的方格阵列:\n";
int i, j;
for(i=0; i<16; i++)
{
fin >> a;
cout << a << " ";
if(i % 4 == 3)
cout << endl;
src |= (a << i);
}
cout << "输入图形B的方格阵列:\n";
for(i=0; i<16; i++)
{
fin >> b;
cout << b << " ";
if(i % 4 == 3)
cout << endl;
des |= (b << i);
}
memset(num, -1, sizeof(num));
memset(how, 0, sizeof(how));
search();
if(flag)
{
cout << "\n变换次数及过程:\n";
cout << num[des] << endl;
output(des);
}
else
cout << "No Solution!";
cout << endl << endl;
fin.close();
return 0;
}