hdu 1667 IDA*

来源:互联网 发布:windows 10自带备份 编辑:程序博客网 时间:2024/06/05 04:17

第一道IDA*。。。。。。

给链接:点击打开链接

AC代码如下:

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int num[7][7];int maxdeep;char record[1000];bool check(){int temp = num[2][2];for( int i = 2; i <= 4; i++ ){if( num[2][i] != temp ){return false;}}if( num[3][2] != temp || num[3][4] != temp ){return false;}for( int i = 2; i <= 4; i++ ){if( num[4][i] != temp ){return false;}}return true;}int h(){int cnt[4] = { 0 };cnt[num[2][2]]++;cnt[num[2][3]]++;cnt[num[2][4]]++;cnt[num[3][2]]++;cnt[num[3][4]]++;cnt[num[4][2]]++;cnt[num[4][3]]++;cnt[num[4][4]]++;int ans = 0;for( int i = 1; i <= 3; i++ ){ans = max( ans, cnt[i] );}return 8 - ans;}int rotationA(){int temp = num[0][2];for( int i = 0; i < 6; i++ ){num[i][2] = num[i+1][2];}num[6][2] = temp;return 0;}int rotationB(){int temp = num[0][4];for( int i = 0; i < 6; i++ ){num[i][4] = num[i+1][4];}num[6][4] = temp;return 0;}int rotationC(){int temp = num[2][6];for( int i = 6; i > 0; i-- ){num[2][i] = num[2][i-1];}num[2][0] = temp;return 0;}int rotationD(){int temp = num[4][6];for( int i = 6; i > 0; i-- ){num[4][i] = num[4][i-1];}num[4][0] = temp;return 0;}int rotationE(){int temp = num[6][4];for( int i = 6; i > 0; i-- ){num[i][4] = num[i-1][4];}num[0][4] = temp;return 0;}int rotationF(){int temp = num[6][2];for( int i = 6; i > 0; i-- ){num[i][2] = num[i-1][2];}num[0][2] = temp;return 0;}int rotationG(){int temp = num[4][0];for( int i = 0; i < 6; i++ ){num[4][i] = num[4][i+1];}num[4][6] = temp;return 0;}int rotationH(){int temp = num[2][0];for( int i = 0; i < 6; i++ ){num[2][i] = num[2][i+1];}num[2][6] = temp;return 0;}bool DFS( int deep ){if( deep == maxdeep ){return check();}if( deep + h() > maxdeep ){return false;} rotationA();record[deep] = 'A';if( DFS( deep + 1 ) ){return true;}rotationF();rotationB();record[deep] = 'B';if( DFS( deep + 1 ) ){return true;}rotationE();rotationC();record[deep] = 'C';if( DFS( deep + 1 ) ){return true;}rotationH();rotationD();record[deep] = 'D';if( DFS( deep + 1 ) ){return true;}rotationG();rotationE();record[deep] = 'E';if( DFS( deep + 1 ) ){return true;}rotationB();rotationF();record[deep] = 'F';if( DFS( deep + 1 ) ){return true;}rotationA();rotationG();record[deep] = 'G';if( DFS( deep + 1 ) ){return true;}rotationD();rotationH();record[deep] = 'H';if( DFS( deep + 1 ) ){return true;}rotationC();return false;}int main(){while( cin >> num[0][2] && num[0][2] ){cin >> num[0][4] >> num[1][2] >> num[1][4];for( int i = 0; i < 7; i++ ){cin >> num[2][i];}cin >> num[3][2] >> num[3][4];for( int i = 0; i < 7; i++ ){cin >> num[4][i];}for( int i = 5; i < 7; i++ ){cin >> num[i][2] >> num[i][4];}maxdeep = 0;while( 1 ){if( DFS( 0 ) ){break;}maxdeep++;}if( maxdeep == 0 ){cout << "No moves needed" << endl;}else{for( int i = 0; i < maxdeep; i++ ){cout << record[i];}cout << endl;}cout << num[2][2] << endl;}return 0;}


0 0
原创粉丝点击