Educational Codeforces Round 42 (Rated for Div. 2) C. Make a Square

Posted 行远山

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 42 (Rated for Div. 2) C. Make a Square相关的知识,希望对你有一定的参考价值。

很裸的一个DFS,但因为细节wa了10发才过。果然我还是太弱了。。

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int a[15]={0};
int index=0;
int t;
bool vis[15]={0};
int minum=1000000;
void dfs(int x)
{
    int temp=1,res=0,sum=0;
    int v=0,i;
    if(x==index+1)
    {
        return;
    }
    int flag=0;
    for(i=0;i<index;i++)
    {
        if(vis[i]&&a[i]==0)//判断是否有前导零,如果有,这个零必须被删除
        {
            sum--;
        }
        else if(vis[i]&&a[i]!=0)
        {

            break;
        }
    }
    for(i=0;i<index;i++)
    {
        if(vis[i])
        {   flag=1;
            res=res*10+a[i];
            sum++;
        }

    }
    int tmp=sqrt(res);
    if(tmp*tmp==res&&flag==1&&res!=0)
    {   //printf("%d %d\n",tmp,res);
        minum=min(minum,index-sum);
    }
    for(i=x;i<index;i++)
    {
        vis[i]=true;
        dfs(i+1);
        vis[i]=false;
    }
    dfs(x+1);
}
int main()
{
    long long n;
    scanf("%I64d",&n);
    memset(vis,false,sizeof(vis));
    t=n;
    int tt=sqrt(t);
    if(tt*tt==t)
    {
        printf("0\n");
        return 0;
    }
    while(n>0)
    {
        a[index]=n%10;
        n/=10;
        index++;
    }
    reverse(a,a+index);
    dfs(0);
    if(minum!=1000000) printf("%d\n",minum);
    else
    {
        printf("-1\n");
    }
    return 0;
}

  

  

以上是关于Educational Codeforces Round 42 (Rated for Div. 2) C. Make a Square的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27