zoukankan      html  css  js  c++  java
  • ZOJ Monthly, November 2012 J Trim the Nails

    
    
    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.

    分析:将可以使用的指甲刀状态列出,然后bfs指甲的状态,直到为0
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    using namespace std;
    int nail[1 << 21];
    int cut[70];
    
    typedef struct S {
        bool vis[70];
        int ans;
    } NODE;
    NODE x,y;
    char s[13];
    int str[13];
    
    int main() {
        int n, m, i, j, cnt, temp, init, a,cal;
        while (scanf("%d", &n) != EOF) {
            scanf("%s", s);
            scanf("%d", &m);
            init = (1 << m) - 1;
            for (i = 0; i < n; ++i) {
                if (s[i] == '*')
                    str[i] = 1;
                else
                    str[i] = 0;
            }
            temp = 0;
            for (i = 0; i < n; ++i) {
                temp = (temp << 1) + str[i];
            }
           
            if(!temp){
                printf("-1\n");
                continue;
            }
            cnt = 0;
            a = temp;
            while (temp) {
                cut[cnt++] = temp;
                temp >>= 1;
            } 
            
            while ((a << 1) & init) {
                a<<=1;
                cut[cnt++] = a;
            }
            temp = 0;
            for (i = n - 1; i >= 0; --i) {
                temp = (temp << 1) + str[i];
            }
            a = temp;
            while (temp) {
                cut[cnt++] = temp;
                temp >>= 1;
            }
            while ((a << 1) & init) {
                a<<=1;
                cut[cnt++] = a;
            }
            memset(nail,0,sizeof(nail));
            queue <NODE> q;
            x.ans=init;
         //   printf("init=%d\n",init);
            nail[init]=1;
            memset(x.vis,0,sizeof(x.vis));
            q.push(x);
            for(i=0;i<cnt;++i){
             //   printf("%d %d\n",i,cut[i]);
                cut[i]=~cut[i];
            }
            while(!q.empty()){
                x=q.front();
            //    printf("ans=%d\n",x.ans);
                q.pop();
                for(i=0;i<cnt;++i){
                    if(x.vis[i])
                        continue;
                    cal=x.ans&cut[i];
                    if(!cal)
                        goto L;
                    if(!nail[cal]){
                        y.ans=cal;
                        nail[cal]=1;
                        memcpy(y.vis,x.vis,sizeof(x.vis));
                        y.vis[i]=1;
                        q.push(y);
                    }
                }          
            }
            L:
            int tot=1;
            for(i=0;i<cnt;++i)
                if(x.vis[i])
                    ++tot;
            printf("%d\n",tot);
        }
        return 0;
    }
     
  • 相关阅读:
    amfphp1.9 class mapping初探
    C#程序打包.exe应用程序
    数据库备份方案
    ListView 控件使用
    在C#中运用SQLDMO备份和恢复Microsoft SQL Server数据库
    .NET
    转载:MATLAB 符号函数作图
    整理雷达相关
    Python 程序 运行过程
    struts2 文件上传显示进度条
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2787735.html
Copyright © 2011-2022 走看看