ZOJ 3675 Trim the Nails (BFS)

Source: Internet
Author: User

Trim the Nails Time limit: 2 Seconds Memory Limit: 65536 KB

Robert is clipping his fingernails. But the nail clipper are old and the edge of the 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's edge good and half of its edge bad.)

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

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

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

Input

There'll is 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****. **46*. 7
Sample Output
12
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, cuts.

Test instructions: Robert needs to cut his nails, but his fingernails are flawed, some are not cut,

His fingernails are shaped as a string, symbol '. ' On behalf of the nail knife this place has defects this nail can not be trimmed to,

symbol ' * ' means this place is intact, this place can be trimmed to; * *, to cut the length of 6 nails,

Then cut out the nails (1 means that the nail has been trimmed, 0 is not) is 110011, which needs to be cut again;

Nail knife can be left and right to move, can also be flipped;

The puzzle: Always from the left side has the position of the nail to start cutting, nail pliers and pros and cons of the two states cut nails, BFS to find the minimum number of steps. 1 of the situation is all '. '.

Can be processed first two nail clippers, positive and negative each, with a number. The original nail state can be used (1<<l)-1, that is, all

is 1, then the bitwise operation, the remaining state to find out, known until 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&LT;&L                T;i);            _jidong|= (1&LT;&LT;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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ZOJ 3675 Trim the Nails (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.