hdu 2234+1560 IDA*

来源:互联网 发布:vivo手机库存软件 编辑:程序博客网 时间:2024/05/16 19:12

IDA* 基本框架 hdu 1560

hdu 2234 稍复杂的IDA*

代码思路清晰,故不多加注释了。

#include<cstdio>//hdu 1560#include<cstring>#include<cmath>#include<algorithm>#define MS(x,y) memset(x,y,sizeof(x))#define pi acos(-1.0)using namespace std;void fre(){freopen("t.txt","r",stdin);}typedef long long LL;typedef unsigned long long ULL;const int MOD = 1e9 + 7;const int inf = (1<<63)-1;const int MAXN = 4*(1e5)+1;const int eps = -(1<<30);char index[] = "AGCT";struct node{    char s[10];    int len;}a[10];int pos[10],ans,n;int get_h(){    int res = 0;    for(int i = 0; i < n; ++i)    {        res = max(a[i].len - pos[i],res);    }    return res;}bool dfs(int step){    if(step+get_h() > ans) return 0;    if(!get_h()) return 1;    int i,j;    int tem[10];    for(i = 0; i < 4; ++i)    {        int flag = 0;        for(j = 0; j < n; ++j) tem[j] = pos[j];        for(j = 0; j < n; ++j)        {            if(a[j].s[pos[j]] == index[i])            {                flag = 1;                pos[j]++;            }        }        if(flag)        {            if( dfs(step+1) ) return 1;            for(j = 0; j < n; ++j) pos[j] = tem[j];        }    }    return 0;}int main(){  //  fre();    int t,i,j;    scanf("%d",&t);    while(t--)    {        int maxx = 0;        scanf("%d",&n);        for(i = 0; i < n; ++i)        {            scanf("%s",a[i].s);            a[i].len = strlen(a[i].s);            maxx = max(maxx,a[i].len);            pos[i] = 0;        }        ans = maxx;        while(1)        {            if( dfs(0) ) break;            ans++;        }        printf("%d\n",ans);    }    return 0;}


#include<cstdio>//hdu 2234#include<cstring>#include<cmath>#include<algorithm>#define MS(x,y) memset(x,y,sizeof(x))#define pi acos(-1.0)using namespace std;void fre(){freopen("t.txt","r",stdin);}typedef long long LL;typedef unsigned long long ULL;const int MOD = 1e9 + 7;const int inf = (1<<63)-1;const int MAXN = 4*(1e5)+1;const int eps = -(1<<30);int map[5][5],ans,vis[5];int get_h(){    int i,j;    int s1 = 0,s2 = 0;    for(i = 1; i <= 4; ++i)//row    {        MS(vis,0); int cnt = 0;        for(j = 1; j <= 4; ++j)        {            if(vis[map[i][j]]) continue;            vis[map[i][j]] = 1;            cnt++;        }        s1 = max(s1,cnt-1);    }    for(i = 1; i <= 4; ++i)//column    {        MS(vis,0); int cnt = 0;        for(j = 1; j <= 4; ++j)        {            if(vis[map[j][i]]) continue;            vis[map[j][i]] = 1;            cnt++;        }        s2 = max(s2,cnt-1);    }    return min(s1,s2);}void TR(int x){    int i,tem = map[x][4];    for(i = 4; i >= 2; --i) map[x][i] = map[x][i-1];    map[x][1] = tem;}void TL(int x){    int i,tem = map[x][1];    for(i = 1; i <= 3; ++i) map[x][i] = map[x][i+1];    map[x][4] = tem;}void TU(int x){    int i,tem = map[1][x];    for(i = 1; i <= 3; ++i) map[i][x] = map[i+1][x];    map[4][x] = tem;}void TD(int x){    int i,tem = map[4][x];    for(i = 4; i >= 2; --i) map[i][x] = map[i-1][x];    map[1][x] = tem;}bool dfs(int step){    if(!get_h()) return 1;// first must.    if(step+get_h() > ans) return 0;    int i,j;    for(i = 1; i <= 4; ++i)    {        TL(i);        if(dfs(step+1)) return 1;        TR(i);        TR(i);        if(dfs(step+1)) return 1;        TL(i);        TU(i);        if(dfs(step+1)) return 1;        TD(i);        TD(i);        if(dfs(step+1)) return 1;        TU(i);    }    return 0;}int main(){   // fre();    int t,i,j;    scanf("%d",&t);    while(t--)    {        for(i = 1; i <= 4; ++i)//read        {            for(j = 1; j <= 4; ++j) scanf("%d",&map[i][j]);        }        ans = 0;        while(1)        {            if(dfs(0)) break;            ans++;            if(ans > 5) break;        }        if(ans > 5) printf("-1\n");        else printf("%d\n",ans);    }    return 0;}



1 0
原创粉丝点击