HDU 1667 百练 1288 POJ 2286

来源:互联网 发布:匡恩网络 知乎 编辑:程序博客网 时间:2024/06/05 23:48

北大暑期课练习题,愚蠢的我企图暴力深搜,然而正解是 迭代加深搜索

附爱神博客链接http://blog.csdn.net/acm_cxlove/article/details/7745575

//高山仰止,景行行止。虽不能至,心向往之。#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<bitset>#include<queue>#include<stack>#include<list>#include<numeric>#include<functional>#define ll long long#define MP make_pair#define rep(a,b,c) for(int a=(b);a<(c);a++)#define rerep(a,b,c) for(int a=(b);a>(c);a--)#define mem(a,b) memset(a,b,sizeof(a))#define lf else if#define P puts("ppppppppppppppppppp")using namespace std;template<class T> T f_max(T a, T b){ return a > b ? a : b; }template<class T> T f_min(T a, T b){ return a < b ? a : b; }template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T>T lcm(T a, T b){ return a / gcd(a, b)*b; }template<class T> void swap(T *a, T *b){ T c; c = a; a = b; b = c; }const int N = 24;const int MAXN = 1000005;const int MAX = 10005;const int Inf = 1000000009;const double precision = 1e-8;const int hashsize = 1000003;int s[N];int flag;int key[8] = { 6, 7, 8, 11, 12, 15, 16, 17 };int road[MAXN];int rev[8] = { 5, 4, 7, 6, 1, 0, 3, 2 };int dir[8][7] = { { 0, 2, 6, 11, 15, 20, 22 },{ 1, 3, 8, 12, 17, 21, 23 },{ 10, 9, 8, 7, 6, 5, 4 },{ 19, 18, 17, 16, 15, 14, 13 },{ 23, 21, 17, 12, 8, 3, 1 },{ 22, 20, 15, 11, 6, 2, 0 },{ 13, 14, 15, 16, 17, 18, 19 },{ 4, 5, 6, 7, 8, 9, 10 } };void init(){flag = 0;}bool data_in(){if (~scanf("%d", &s[0])&&s[0]!=0){rep(i, 1, 24){scanf("%d", &s[i]);}return true;}return false;}int get_h(int a[]){int c3, c1, c2;c3 = c1 = c2 = 0;rep(i, 0, 8){if (a[key[i]] == 3)c3++;else if (a[key[i]] == 1)c1++;elsec2++;}return 8 - f_max(f_max(c3, c1), c2);}void mov(int a[], int ins){int t = a[dir[ins][0]];rep(i, 0, 6)a[dir[ins][i]] = a[dir[ins][i + 1]];a[dir[ins][6]] = t;}void dfs(int a[], int depth,int tot, int father){if (get_h(a) > depth)return;if (flag)return;if (depth == 0 && get_h(a) == 0){flag = 1; rep(i, 0, tot)printf("%c", 'A' + road[i]);puts("");printf("%d\n", a[6]);return;}rep(i, 0, 8){if (rev[i] == father)continue;int b[N];memcpy(b, a, N*sizeof(int));//rep(j, 0, N)//b[j] = a[j];mov(b, i);road[tot] = i;dfs(b, depth - 1, tot + 1, i);}}void solve(){if (get_h(s)==0){puts("No moves needed");printf("%d\n", s[6]);return;}init();for (int depth = get_h(s);; depth++){dfs(s, depth, 0, -1);if (flag)break;}}int main(){while (data_in()){solve(); }return 0;}


0 0
原创粉丝点击