#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
const int read [25][2] =
{
{ 0 , 0 } ,
{ 1 , 3 } , { 1 , 5 } ,
{ 2 , 3 } , { 2 , 5 } ,
{ 3 , 1 } , { 3 , 2 } , { 3 , 3 } , { 3 , 4 } , { 3 , 5 } , { 3 , 6 } , { 3 , 7 } ,
{ 4 , 3 } , { 4 , 5 } ,
{ 5 , 1 } , { 5 , 2 } , { 5 , 3 } , { 5 , 4 } , { 5 , 5 } , { 5 , 6 } , { 5 , 7 } ,
{ 6 , 3 } , { 6 , 5 } ,
{ 7 , 3 } , { 7 , 5 } ,
};
int md ;
int x [ 11 ] [ 11 ] ;
namespace cz{
void leftsandrights ( int q , int type ) {
if ( type == 1 ) {//lefts
int s = x [ q ] [ 1 ] ;
for ( int i = 1 ; i < 7 ; i ++ )
x [ q ] [ i ] = x [ q ] [ i + 1 ] ;
x [ q ] [ 7 ] = s ;
}
if ( type == 2 ) {//rights
int s = x [ q ] [ 7 ] ;
for ( int i = 7 ; i > 1 ; i -- )
x [ q ] [ i ] = x [ q ] [ i - 1 ] ;
x [ q ] [ 1 ] = s ;
}
}
void upsanddowns ( int q , int type ) {
if ( type == 1 ) {//ups
int s = x [ 1 ] [ q ] ;
for ( int i = 1 ; i < 7 ; i ++ )
x [ i ] [ q ] = x [ i + 1 ] [ q ] ;
x [ 7 ] [ q ] = s ;
}
if ( type == 2 ) {//downs
int s = x [ 7 ] [ q ] ;
for ( int i = 7 ; i > 1 ; i -- )
x [ i ] [ q ] = x [ i - 1 ] [ q ] ;
x [ 1 ] [ q ] = s ;
}
}
}
string str;
int checks ( ) {
int sum [ 4 ] = { 0 , 0 , 0 , 0 } ;
for ( int i = 3 ; i <= 5 ; i ++ )
for ( int j = 3 ; j <= 5 ; j ++ )
sum [ x [ i ] [ j ] ] ++ ;
return 8 - max ( sum [ 1 ] , max ( sum [ 2 ] , sum [ 3 ] ) ) ;
}
void print ( ) {
for ( int i = 1 ; i <= 7 ; i ++ , cout << '\n' ) {
for ( int j = 1 ; j <= 7 ; j ++ ) cout << x [ i ] [ j ] << " " ;
}
return ;
}
bool dfs ( int now , char pre ) {
int number = checks ( ) ;
if ( number == 0 ) return true ;
if ( number + now > md ) return false ;
if ( pre != 'F' ) {
cz :: upsanddowns ( 3 , 1 ) ;
if ( dfs ( now + 1 , 'A' ) ) { str = str + "A" ; return true ; }
cz :: upsanddowns ( 3 , 2 ) ;
}
if ( pre != 'E' ) {
cz :: upsanddowns ( 5 , 1 ) ;
if ( dfs ( now + 1 , 'B' ) ) { str = str + "B" ; return true ; }
cz :: upsanddowns ( 5 , 2 ) ;
}
if ( pre != 'H' ) {
cz :: leftsandrights ( 3 , 2 ) ;
if ( dfs ( now + 1 , 'C' ) ) { str = str + "C" ; return true ; }
cz :: leftsandrights ( 3 , 1 ) ;
}
if ( pre != 'G' ) {
cz :: leftsandrights ( 5 , 2 ) ;
if ( dfs ( now + 1 , 'D' ) ) { str = str + "D" ; return true ; }
cz :: leftsandrights ( 5 , 1 ) ;
}
if ( pre != 'B' ) {
cz :: upsanddowns ( 5 , 2 ) ;
if ( dfs ( now + 1 , 'E' ) ) { str = str + "E" ; return true ; }
cz :: upsanddowns ( 5 , 1 ) ;
}
if ( pre != 'A' ) {
cz :: upsanddowns ( 3 , 2 ) ;
if ( dfs ( now + 1 , 'F' ) ) { str = str + "F" ; return true ; }
cz :: upsanddowns ( 3 , 1 ) ;
}
if ( pre != 'D' ) {
cz :: leftsandrights ( 5 , 1 ) ;
if ( dfs ( now + 1 , 'G' ) ) { str = str + "G" ; return true ; }
cz :: leftsandrights ( 5 , 2 ) ;
}
if ( pre != 'C' ) {
cz :: leftsandrights ( 3 , 1 ) ;
if ( dfs ( now + 1 , 'H' ) ) { str = str + "H" ; return true ; }
cz :: leftsandrights ( 3 , 2 ) ;
}
return false ;
}
int main ( ) {
while ( 1 )
{
str = "";
md = 0 ;
cin >> x [ read [ 1 ] [ 0 ] ] [ read [ 1 ] [ 1 ] ] ;
if ( x [ read [ 1 ] [ 0 ] ] [ read [ 1 ] [ 1 ] ] == 0 ) return 0 ;
for ( int i = 2 ; i <= 24 ; i ++ )
cin >> x [ read [ i ] [ 0 ] ] [ read [ i ] [ 1 ] ] ;
for ( md = 0 ; ; md ++ )
if ( dfs ( 1 , '1' ) )
{
if ( md == 0 )
cout << "No moves needed\n";
else
{
reverse ( str . begin () , str . end ( ) ) ;
cout << str << endl;
}
cout << x [ 3 ] [ 3 ] << endl ;
break;
}
}
return 0 ;
}
标签:return,leftsandrights,int,UVA1343,cz,Game,dfs,str,Rotation
From: https://www.cnblogs.com/dadidididi/p/16977196.html