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;
    }
     
  • 相关阅读:
    Web系统可复用架构
    asp.net mvc 2 简简单单做开发 实现基本数据操作操作RepositoryController<T>
    asp.net mvc 2 简简单单做开发 使用DataContext扩展方法Find<TEntity>(TEntity obj) 遇到的问题
    asp.net mvc 2 简简单单做开发 自定义DropdownList控件
    asp.net mvc 2 DisplayTemplates 的使用
    linq to sql 扩展方法
    asp.net mvc 2 简简单单做开发 通用增删改基本操作通用页面
    asp.net mvc 2 简简单单做开发 自定义Controller基类
    JNLP启动相关的东东
    easyexcel往已存在的excel文件里追加数据
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2787735.html
Copyright © 2011-2022 走看看