Alice’s Cube
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 490 Accepted Submission(s): 161
Problem Description
![](http://writeblog.csdn.net/data/images/3220-1.jpg)
Alice has received a hypercube toy as her birthday present. This hypercube has 16 vertices, numbered from 1 to 16, as illustrated below. On every vertex, there is a light bulb that can be turned on or off. Initially, eight of the light bulbs are turned on and the other eight are turned off. You are allowed to switch the states of two adjacent light bulbs with different states (“on” to “off”, and “off” to “on”; specifically, swap their states) in one operation.
Given the initial state of the lights, your task is to calculate the minimum number of steps needed to achieve the target state, in which the light bulbs on the sub cube (1,2,3,4)-(5,6,7,8) are turned off, and the rest of them are turned on.
Input
There are multiple test cases. The first line of the input contains an integer T, meaning the number of the test cases. There are about 13000 test cases in total.
For each test case there are 16 numbers in a single line, the i-th number is 1 meaning the light of the i-th vertex on the picture is on, and otherwise it’s off.
For each test case there are 16 numbers in a single line, the i-th number is 1 meaning the light of the i-th vertex on the picture is on, and otherwise it’s off.
Output
For every test cases output a number with case number meaning the minimum steps needed to achieve the goal. If the number is larger than 3, you should output “more”.
Sample Input
3 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 0 0 0 0 0 1 0 1 0 1 1 1 1 1 1
Sample Output
Case #1: 0 Case #2: 1 Case #3: more#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
using namespace std;
const int org=(1<<8)-1;
const int _max=(1<<17)-1;
int map[20][20];
int vis[100000];
vector<int> G[20];
void init()
{
G[1].push_back(2);G[1].push_back(3);G[1].push_back(5);G[1].push_back(9);
G[2].push_back(1); G[2].push_back(4); G[2].push_back(6); G[2].push_back(10);
G[3].push_back(1);G[3].push_back(4);G[3].push_back(7);G[3].push_back(11);
G[4].push_back(2);G[4].push_back(3);G[4].push_back(8);G[4].push_back(12);
G[5].push_back(1); G[5].push_back(6); G[5].push_back(7); G[5].push_back(13);
G[6].push_back(2);G[6].push_back(5);G[6].push_back(8);G[6].push_back(14);
G[7].push_back(3);G[7].push_back(5);G[7].push_back(8);G[7].push_back(15);
G[8].push_back(4);G[8].push_back(6);G[8].push_back(7);G[8].push_back(16);
G[9].push_back(1);G[9].push_back(10);G[9].push_back(11);G[9].push_back(13);
G[10].push_back(2);G[10].push_back(9);G[10].push_back(12);G[10].push_back(14);
G[11].push_back(3);G[11].push_back(9);G[11].push_back(12);G[11].push_back(15);
G[12].push_back(4);G[12].push_back(10);G[12].push_back(11);G[12].push_back(16);
G[13].push_back(5);G[13].push_back(9);G[13].push_back(14);G[13].push_back(15);
G[14].push_back(6);G[14].push_back(10);G[14].push_back(13);G[14].push_back(16);
G[15].push_back(7);G[15].push_back(11);G[15].push_back(13);G[15].push_back(16);
G[16].push_back(8);G[16].push_back(12);G[16].push_back(14);G[16].push_back(15);
for(int i=1;i<=16;i++)
{
for(int j=0,len=G[i].size();j<len;j++)
{
map[i][G[i][j]]=1;
}
}
}
struct Node
{
int state;
int l;
};
queue<Node> q;
void sol()
{
Node pre;pre.state=org,pre.l=0;
vis[org]=1;
q.push(pre);
while(!q.empty())
{
Node t=q.front();q.pop();
if(t.l==3) continue;
int num[20]={0};
for(int i=16,h=t.state;i>=0&&h;i--,h/=2) num[i]=h%2;
for(int i=1;i<=16;i++)
{
for(int j=i+1;j<=16;j++)
{
if(num[i]!=num[j]&&map[i][j])
{
swap(num[i],num[j]);
int cnt=0;
for(int k=1;k<=16;k++) cnt=cnt*2+num[k];
swap(num[i],num[j]);
if(vis[cnt]) continue;
vis[cnt]=vis[t.state]+1;
Node temp;temp.state=cnt,temp.l=t.l+1;
q.push(temp);
}
}
}
}
}
int main()
{
init();
sol();
int ci;scanf("%d",&ci);
for(int pl=1;pl<=ci;pl++)
{
int cnt=0;
for(int i=0;i<16;i++)
{
int x;scanf("%d",&x);
cnt=cnt*2+x;
}
printf("Case #%d: ",pl);
if(vis[cnt]-1<=3&&vis[cnt]) printf("%d/n",vis[cnt]-1);
else printf("more/n");
}
return 0;
}