zoukankan      html  css  js  c++  java
  • ZOJ 3675 Trim the Nails(bfs)

    Trim the Nails

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Robert is clipping his fingernails. But the nail clipper is old and the edge of the nail clipper is potholed.

    The nail clipper's edge is N millimeters wide. And we use N characters('.' or '*') to represent the potholed nail clipper. '.' represents 1 bad millimeter edge, and '*' represents 1 good millimeter edge.(eg. "*****" is a 5 millimeters nail clipper with the whole edge good. "***..." is a 6 millimeters nail clipper with half of its edge good and half of its edge bad.)

    Notice Robert can turn over the clipper. Turning over a "**...*"-nail clipper will make a "*...**"-nail clipper.

    One-millimeter good edge will cut down Robert's one-millimeter fingernail. But bad one will not. It will keep the one-millimeter unclipped.

    Robert's fingernail is M millimeters wide. How many times at least should Robert cut his fingernail?

    Input

    There will be multiple test cases(about 15). Please process to the end of input.

    First line contains one integer N.(1≤N≤10)

    Second line contains N characters only consists of '.' and '*'.

    Third line contains one integer M.(1≤M≤20)

    Output

    One line for each case containing only one integer which is the least number of cuts. If Robert cannot clipper his fingernail then output -1.

    Sample Input

    8
    ****..**
    4
    6
    *..***
    7
    

    Sample Output

    1
    2
    

    Hint

    We use '-' to present the fingernail.
    For sample 1:
    fingernail:	----
    nail clipper:	****..**
    Requires one cut.
    
    For sample 2:
    fingernail:			-------
    nail clipper:			*..***
    nail clipper turned over:	 ***..*
    Requires two cuts.
    
    
    

    题意: Robert须要剪指甲。可是他的指甲刀有缺陷,有些是剪不到的,

    他的指甲刀形如是一个字符串。符号'.'代表指甲刀这处有缺陷这处的指甲不能修剪到,

    符号'*'代表这处是完善的,这处的能够修剪到。如指甲刀**..**,要剪长度为6的指甲,

    则剪出来的指甲(1代表该处指甲已修剪。0则没有)是110011,这须要再剪一次;

    指甲刀能够左右移动,还能够翻转;

    题解:一直从最左端有指甲的位置開始 剪,指甲钳分正反两种状态剪指甲,bfs求出最小步数。

    -1的情况全是‘.’。

               能够先处理出两把指甲钳,正反各一把,用一个数表示。指甲原始状态能够用(1<<L)-1表示,即所有

               都是1,然后进行位运算,把剩下的状态求出来。知直到0.


    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    
    using namespace std;
    
    int n,L,ans;
    char s[14];
    int Jidong,_Jidong;
    
    struct node
    {
        int nail;
        int step;
    };
    queue<node>que;
    bool vis[(1<<20)+50];
    
    void debug(int x)
    {
        while(x)
        {
            printf("%d",x&1 );
            x>>=1;
        }
        printf("
    ");
    }
    void bfs()
    {
        while(que.size())que.pop();
        memset(vis,false,sizeof vis);
        node it;
        it.nail=L;
        it.step=0;
        que.push(it);
        vis[L]=true;
        while(que.size())
        {
            it=que.front();
            if(it.nail==0)
            {
                ans=it.step;
                return;
            }
            que.pop();
            while((it.nail&1)==0)
            {
                it.nail>>=1;
            }
            int nit=it.nail,_nit=it.nail;
            for(int i=0;i<=20;i++)
            {
                if((nit&(1<<i))&&(Jidong&(1<<i)))
                    nit^=(1<<i);
                if((_nit&(1<<i))&&(_Jidong&(1<<i)))
                    _nit^=(1<<i);
            }
            node _it;
            _it.nail=nit;
            _it.step=it.step+1;
            if(!vis[nit])
            {
                vis[nit]=true;
                que.push(_it);
            }
            _it.nail=_nit;
            if(!vis[_nit])
            {
                vis[_nit]=true;
                que.push(_it);
            }
        }
    }
    
    int main()
    {
        //freopen("test.in","r",stdin);
        while(cin>>n)
        {
            scanf("%s",s);
            cin>>L;
            Jidong=0,_Jidong=0;
            int length=strlen(s);
            for(int i=0,j=length-1;i<length;i++,j--)
            {
                if(s[i]=='*')
                {
                    Jidong|=(1<<i);
                    _Jidong|=(1<<j);
                }
            }
            //debug(Jidong);
            if(!Jidong)
            {
                printf("-1
    ");
                continue;
            }
            while((Jidong&1)==0)Jidong>>=1;
            while((_Jidong&1)==0)_Jidong>>=1;
            L=(1<<L)-1;
            ans=-1;
            bfs();
            printf("%d
    ",ans );
        }
        return 0;
    }


  • 相关阅读:
    面向机器学习的特征工程
    卷积可视化,图像理解,deepdream,风格迁移
    损失函数
    开源是如何支撑区块链技术发展的
    揭秘机密计算:弥补最后一个入侵 “漏洞”
    什么是超大规模数据中心?
    比特币是避险资产还是储备资产?
    区块链技术生态持续优化,五大趋势不容忽视
    物联网低功耗广域网(LPWAN)的比较
    碎片化是物联网快速发展的阻碍,也是机会
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7142642.html
Copyright © 2011-2022 走看看