您的位置:首页 > 其它

Sicily 1150. 简单魔板

2012-02-05 23:08 357 查看
从原始状态开始,分层宽搜找到所有10步(N<=10)以内的结果,然后对输入进行查找,根据查找结果进行输出即可。

Run Time: 0sec

Run Memory: 436KB

Code length: 2109Bytes

SubmitTime: 2012-01-07 19:21:05

// Problem#: 1150
// Submission#: 1180166
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <cstdio>
#include <string>
#include <queue>
#include <map>
using namespace std;

void changeA( const string &s, string &t );
void changeB( const string &s, string &t );
void changeC( const string &s, string &t );

int main()
{
int M;
string now, target;
int i, j;

map<string, string> m;
target = "12348765";
m[ target ].clear();
queue<string> q;
q.push( target );
for ( j = 1; j <= 10; j++ ) {
i = q.size();
while ( i-- ) {
now = q.front();
changeA( now, target );
if ( m.find( target ) == m.end() ) {
m[ target ] = m[ now ] + 'A';
q.push( target );
}
changeB( now, target );
if ( m.find( target ) == m.end() ) {
m[ target ] = m[ now ] + 'B';
q.push( target );
}
changeC( now, target );
if ( m.find( target ) == m.end() ) {
m[ target ] = m[ now ] + 'C';
q.push( target );
}
q.pop();
}
}

while ( scanf( "%d", &M ) && M != -1 ) {
for ( i = 0; i < 8; i++ ) {
scanf( "%d", &j );
target[ i ] = '0' + j;
}
if ( m.find( target ) == m.end() || m[ target ].size() > M )
printf( "-1\n" );
else
printf( "%d %s\n", m[ target ].size(), m[ target ].c_str() );
}

return 0;

}

void changeA( const string &s, string &t ) {
t[ 0 ] = s[ 4 ];
t[ 1 ] = s[ 5 ];
t[ 2 ] = s[ 6 ];
t[ 3 ] = s[ 7 ];
t[ 4 ] = s[ 0 ];
t[ 5 ] = s[ 1 ];
t[ 6 ] = s[ 2 ];
t[ 7 ] = s[ 3 ];
}

void changeB( const string &s, string &t ) {
t[ 0 ] = s[ 3 ];
t[ 1 ] = s[ 0 ];
t[ 2 ] = s[ 1 ];
t[ 3 ] = s[ 2 ];
t[ 4 ] = s[ 7 ];
t[ 5 ] = s[ 4 ];
t[ 6 ] = s[ 5 ];
t[ 7 ] = s[ 6 ];
}

void changeC( const string &s, string &t ) {
t[ 0 ] = s[ 0 ];
t[ 1 ] = s[ 5 ];
t[ 2 ] = s[ 1 ];
t[ 3 ] = s[ 3 ];
t[ 4 ] = s[ 4 ];
t[ 5 ] = s[ 6 ];
t[ 6 ] = s[ 2 ];
t[ 7 ] = s[ 7 ];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: