题目题意是:给你的棋盘,在A-H方向上可以拨动,问你最少拨动几次可以是中心图案的数字一致
解题思路:回溯法,剪枝
其中要把每次拨动的字母所代表的位置提前用数组表示;
然后在如果step+h()>maxd表示剪枝。
总之,用数组那里表示真的好棒,
自己太残了……!!!
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map> using namespace std; const int INF = 0xffffff;
const double esp = 10e-;
const double Pi = * atan(1.0);
const int Maxn = +;
const long long mod = ;
const int dr[] = {,,-,,-,,-,};
const int dc[] = {,,,-,,-,-,};
typedef long long LL; LL gac(LL a,LL b){
return b?gac(b,a%b):a;
}
int shift[][] = {
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,}
};
int center[] = {,,,,,,,};
int re[] = {,,,,,,,};
int d[];
char ans[];
int maxd; bool is_good(){
for(int i = ;i < ;i++){
if(d[ center[] ] != d[ center[i] ])
return ;
}
return ;
} void _move(int x){
int tmp = d[ shift[x][] ];
for(int i = ;i < ;i++)
d[ shift[x][i] ] = d[shift[x][i+] ];
d[shift[x][]] = tmp;
} int differ(int x){
int cnt = ;
for(int i = ;i < ;i++){
if(d[center[i]] != x){
cnt++;
}
}
return cnt;
} int h(){
return min(differ(),min(differ(),differ()));
} bool dfs(int step){
if(step == maxd){
if(is_good()){
ans[step] = '\0';
return ;
}
return ;
}
if(step + h() > maxd)
return ;
for(int i = ;i < ;i++){
ans[step] = i + 'A';
_move(i);
if(dfs(step+))
return ;
_move(re[i]);
}
return ;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("inpt.txt","r",stdin);
#endif
int goal[];
char str[] = "No moves needed";
while(~scanf("%d",&goal[]) && goal[]){
for(int i = ;i < ;i++){
scanf("%d",&goal[i]);
}
for(maxd = ;;maxd++){
memcpy(d,goal,sizeof(d));
if(dfs())
break;
}
printf("%s\n%d\n",(maxd?ans:str),d[center[]]);
}
return ;
}