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("\n");
}
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\n");
            continue;
        }
        while((Jidong&1)==0)Jidong>>=1;
        while((_Jidong&1)==0)_Jidong>>=1;
        L=(1<<L)-1;
        ans=-1;
        bfs();
        printf("%d\n",ans );
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-09 03:10:12

ZOJ 3675 Trim the Nails(bfs)的相关文章

zoj 2913 Bus Pass (BFS)

Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus syst

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

牛汇(BFS)入金具体流程(图文指导)

牛汇开户流程:bfsforex牛汇入金教程 所谓入金,也就是充值的意思,必须充钱到平台才能进行外汇交易.首先,我们先登录bfsforex牛汇官方网站,在交易办公室功能区域下面,点击账户入金: 为您提供中国各大银行的网银支付解决方案,支持人民币支付,和信用卡入金,入金是实时到账的. 牛汇(BFS)入金具体流程(图文指导),布布扣,bubuko.com

ZOJ 2724 Windows 消息队列 (优先队列)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text cha

URAL 1930 Ivan&#39;s Car(BFS)

Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are

【判重+广搜(bfs)】魔板

判重+广搜(bfs)]魔板 Time Limit: 1000MS Memory Limit: 32768KB Special Judge 有一个两行四列的魔板,每个格子里有一个1到8的数字(数字唯一),现在我们可以对魔板进行以下操作: 1.交换两行的数字. 2.将第一列移到第二列,第二列到第三列,第三列到第四列,第四列到第一列. 3.将中间四个数顺时针转一次. 现给你初始状态,我末状态请你用最小的步数将它从初始状态变到末状态. 输入: 前两行,每行4个数表示初状态. 后两行,每行4个数表示末状态

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig