hdu 2234+1560 IDA*

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值