HDOJ1015(简单深搜)

简单的深度优先搜索。求最大字典序,注意要先排序。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int SIZE=13;
char a[5];
int vis[SIZE*2+10];
bool cmp(char a,char b)
{
    return b > a;
}

int npow(int x,int n)
{
    int res=1;
    while(n)
    {
        if(n&1)    res*=x;
        x*=x;
        n>>=1;
    }

    return res;
}

bool check(long long target)
{
    long long ans=0;
    for(int i=0;i<5;i++)
    {
        if((i+1)%2==1)
            ans+=npow(a[i]-‘A‘+1,i+1);
        else
            ans-=npow(a[i]-‘A‘+1,i+1);
    }
    if(ans==target)
        return true;

    return false;
}

char out[6];
void dfs(int i,char op[],long long target)
{
    if(i==5)
    {
        if(check(target))
        {
            for(int j=0;j<5;j++)
                out[j]=a[j];
        }

        return ;
    }
    for(int j=0;op[j];j++)
    {
        int it=op[j]-‘A‘+1;
        if(!vis[it])
        {
            vis[it]=1;
            a[i]=op[j];
            dfs(i+1,op,target);
            vis[it]=0;
            a[i]=‘\0‘;
        }
    }
}
int main()
{

    long long target;
    char op[SIZE]={‘\0‘};
    while(scanf("%lld %s",&target,&op)!=EOF&&target!=0&&strcmp(op,"END")!=0)
    {
        sort(op,op+strlen(op),cmp);
        memset(out,‘\0‘,sizeof(out));
        memset(a,‘\0‘,sizeof(a));
        dfs(0,op,target);
        if(out[0]==‘\0‘)
            printf("no solution\n");
        else
            printf("%s\n",out);
        memset(op,‘\0‘,sizeof(op));
    }

    return 0;
}
时间: 2024-10-10 21:45:22

HDOJ1015(简单深搜)的相关文章

hdu1016(简单深搜)

这是一个简单深搜问题,要求相邻的数之间相加为素数.采用深搜,把满足条件的值放在parent[]中.最后输出parent[]. A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: th

fzu 1920 Left Mouse Button(简单深搜题)

题目地址:http://acm.fzu.edu.cn/problem.php?pid=1920 题目大意是给定一个n*n的图,模拟扫雷游戏,0代表没有雷区,1代表附近九宫格内只有一个雷-- 如果不懂的话去玩下扫雷,挺好玩的,99个雷的玩了好几百盘才赢了一次............ 此题假设神操作,点不到雷,问你最少要多少下才可以把图中非雷的点完,怎么样才最快呢? 当然先点0啦,,,,,,,, 好吧,不废话了..... ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1

ZOJ Seeding 2100【简单深搜】

Seeding Time Limit: 2 Seconds      Memory Limit: 65536 KB It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with n * m squares. There are big stones in some of the squares. Tom has a seeding-ma

简单深搜:POJ1546——Sum it up

结束了三分搜索的旅程 我开始迈入深搜的大坑.. 首先是一道比较基础的深搜题目(还是很难理解好么) POJ 1564 SUM IT UP 大体上的思路无非是通过深搜来进行穷举.匹配 为了能更好地理解深搜 可以尝试去画一下二叉树理解一下,查看遍历的路径 代码还是百度到别人的自己再参悟- -佩服别人的功底啊 先上代码: /*POJ 1546 Sum it up*/ # include<iostream> # include<algorithm> # include<cstdio&g

hdu 1241 Oil Deposits(八方向简单深搜,对新手挺经典)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12461 Accepted Submission(s): 7245 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground

简单深搜(poj 3009)

题目链接:http://poj.org/problem?id=3009 题目:冰壶撞向目的地,只有遇到"1"才能停下来,并且把"1"撞成"0".只能横冲直撞,不允许蛇皮走位等等骚操作.从"2"要撞到"3",周围有"0",才能向有"0"的地方滑.运动员只能推十次,问最少要多少次才到"3"? 用深搜遍历每一个方向. 1 #include<stdi

poj1321棋盘简单深搜

这个题确实有点简单,看清楚不能同列同行就好了,用一个一维数组标记哪一列用了往下搜就好了 #include<stdio.h> int flag[8]={1,1,1,1,1,1,1,1},i,j,n,k,num=0; char arr[10][11]; void dfs(int a,int b) { int ii,jj; if(b==0) { num++; return ; } for(ii=a+1;ii<n;ii++) for(jj=0;jj<n;jj++) { if(flag[jj

nyoj587 hdu1045 简单深搜

#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<map> #include<list> #include<set> #include<stack> #include<cstring> #include<string> #include<algorithm> #inclu

简单深搜

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=27 #include <stdio.h> #include <string.h> int t; int r,c; int maps[105][105]; int color[105][105]; int to[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; void dfs(int i,int j) { if(maps[i][j]==1&&a