HDU 1560 IDA*搜索

用N个串中找到最短的公共串(不要求连续,只要相对位置一样即可)

迭代加深搜索即可

剪枝:当前的深度+最少还有加深的深度是否大于限制的长度,若是,则退回。

#include "stdio.h"
#include "string.h"

const char ch[10]="ATCG";
int deep,n;
char s[10][10];
int pos[10];

int Max(int a,int b)
{
    if (a<b) return b;else return a;
}

int get_h()
{
    int ans,i;
    ans=0;
    for (i=0;i<n;i++)
        ans=Max(ans,strlen(s[i])-pos[i]);
    return ans;
}
int dfs(int w)
{
    int h,i,j,flag;
    int temp[10];
    h=get_h();
    if(w+h>deep) return 0;
    if(h==0) return 1;

    for (i=0;i<4;i++)
    {
        flag=0;
        for (j=0;j<n;j++)
            temp[j]=pos[j];
        for (j=0;j<n;j++)
            if (s[j][pos[j]]==ch[i])
            {
                flag=1;
                pos[j]++;
            }

        if (flag==1)
        {
            if (dfs(w+1)==1) return 1;
            for (j=0;j<n;j++)
                pos[j]=temp[j];
        }
    }

    return 0;
}
int main()
{
    int Case,i;
    scanf("%d",&Case);
    while (Case--)
    {
        scanf("%d",&n);
        deep=0;
        for (i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            deep=Max(deep,strlen(s[i]));
            pos[i]=0;
        }

        while (1)
        {
            if (dfs(0)==1) break;
            deep++;
        }
        printf("%d\n",deep);
    }
    return 0;
}
时间: 2024-08-10 21:21:55

HDU 1560 IDA*搜索的相关文章

HDU 3220 IDA*搜索 || BFS

给出一个16个点所构成的图形,分别由0,1组成,每次操作可以任选其中相连的两个点(必须一个为0,一个为1),进行0,1,交换 问3步内是否可以把图形变成:0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1状态 若不行,输出more 状压存储图形状态.ida*搜索  或者 BFS都行 IDA*搜索 #include "stdio.h" #include "string.h" const int b[]={0,1,2,4,8,16,32,64,128,256

HDU 1560 DNA sequence(DNA序列)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; fo

[BZOJ 1085][SCOI 2005]骑士精神(IDA*搜索)

题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1085 考虑到深度不超过15,IDA*搜索可做. 估价函数h()=当前不在目标位置的棋子个数. 然后其他细节就和普通的迭代加深一样了. #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm>

HDU 2828 DLX搜索

Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 771    Accepted Submission(s): 230 Special Judge Problem Description There are several switches and lamps in the room, however, the connecti

[BZOJ 1085] [SCOI2005] 骑士精神 [ IDA* 搜索 ]

题目链接 : BZOJ 1085 题目分析 : 本题中可能的状态会有 (2^24) * 25 种状态,需要使用优秀的搜索方式和一些优化技巧. 我使用的是 IDA* 搜索,从小到大枚举步数,每次 DFS 验证在当前枚举的步数之内能否到达目标状态. 如果不能到达,就枚举下一个步数,重新搜索,即使某些状态在之前的 DFS 中已经搜索过,我们仍然搜索. 并且在一次 DFS 中,我们不需要判定重复的状态. 在 IDA* 中,重要的剪枝优化是 估价函数 ,将一些不可能存在可行解的枝条剪掉. 如果估价函数写得

HDU 1518 Square 搜索

Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 11   Accepted Submission(s) : 5 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Given a set of sticks of vario

IDA 搜索中文字符串

IDA 搜索中文字符串 IDA 的字符串窗口默认只能显示英文,网上的一些方法是指定启动时的参数可以显示中文 ida64 -DCULTURE=all ida -DCULTURE=all 还有就是修改 cfg/ida.cfg 文件,但是这两种方法都没试成功.实际上没有那么麻烦,IDA 7.0 操作很方便,在字符串列表窗口右键点击 Setup,如下图所示. 对话框勾上 Unicode C-style (16 bits),点击 OK,如下图所示.另外需要注意的是图中最后面的 Minimal string

hdu 1560 迭代加深搜索

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 只能说bin神太给力了.. 又学到不少新知识.. 迭代加深搜索,貌似 又叫IDA*, 就是给搜索深度一个限制,搜索到一个满足条件就结束. 注意剪枝~ 代码: #include <iostream> #include <cstdio> #include <cstring> using namespace std; char g[10][10]; int size[10];

hdu 1560 DNA sequence(迭代加深搜索)

DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 15   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description The twenty-first century