CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » September 2009 (Contest VIII) » A Bowling Game

A Bowling Game

Problem code: G1

  • All Submissions

All submissions for this problem are available.

Mary and Johnny are playing a game with bowling pins. There is a row of N bowling pins. Mary and Johnny alternatively take their turn to bowl. They are such skillful bowling players that at each player's turn, any of them can take out any row of adjacent bowling pins, provided that the number of pins taken out is no more than K. It is important to notice that all the pins taken out must be adjacent. It is impossible to knock down pins separated by a greater distance. Whoever knocks out the last bowling pin wins the game.

You have just come down to watch Mary and Johnny playing the game. Some pins are already removed and now it is Mary's turn. With a good knowledge of mathematical games, you know that it is possible to compute in advance whether Mary or Johnny will win the game, provided that the winner always plays the perfect strategy. So why not impress your two friends by telling them who will win? If Mary could win the game, show her a winning move. Since Mary always wants to win the game as fast as possible, if there are more than one possible winning moves, show her the move that has the most number of pins taken out. If there are still several solutions, show her the move in which the pins taken out are as near to the left as possible.

Input

The first line contains t, the number of test cases (about 15). Then t test cases follow. Each test case has the following form:

  • The first line contains two numbers N and K (1 <= N <= 2000, 1<= K <= 20)
  • The second lines contains N characters 0 or 1 representing the row of N bowling pins. A character 0 means the bowling pin at the corresponding position is already taken out whereas a character 1 means the bowling pin is still in its position.

Each test case is separated by a blank line.

Output

For each of the test case, print either the string "Mary wins" or "Johnny wins" depending on who will win the game. If Mary could win the game, in the next line print N characters 0 or 1 representing the row of N bowling pins after Mary's move. Use the same format as that of the input data.

Remember to print a blank line after the output for each test case.

Example

Input:
3

3 2
111

8 3
10110111

8 4
11111111

Output:
Mary wins
101

Johnny wins

Mary wins
11000011

Output details

Case 1: Mary knocks down the middle pin. Regardless of which pin Johnny takes out in the next move, Mary always takes out the remaining pin and wins the game.

Case 2: Mary will lose the game regardless of which move she plays. For example if she takes out the rightmost pin, it is easy to verify that Johnny will always win if he then takes out the leftmost pin.

Case 3: There is more than one move that allows Mary to win the game. The four middle pins make the most number of pins that Mary can take out.


Author: admin
Date Added: 15-08-2009
Time Limit: 2 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

In sample testcase, t = 2.

fushar @ 1 Sep 2009 04:25 PM

In sample testcase, t = 2. Why are there 3 testcases?

This is fixed.

admin @ 1 Sep 2009 04:33 PM

This is fixed.

How do I read input and right

Aby @ 1 Sep 2009 05:09 PM

How do I read input and right output for this problem in C#.

We can't help you with such

admin @ 1 Sep 2009 05:10 PM

We can't help you with such queries while the contest is on.

In the output, the blank line

fushar @ 1 Sep 2009 06:05 PM

In the output, the blank line is after each testcase or between testcases?

You need to print a blank

admin @ 1 Sep 2009 06:19 PM

You need to print a blank line after the output for each test  case.

  Is there a restriction

udayabhanug @ 1 Sep 2009 07:47 PM

 

Is there a restriction that, every player should knock out at least one pin, every time? 

I think this must be assumed to solve the problem, right?

There is no such restriction

admin @ 1 Sep 2009 07:53 PM

There is no such restriction in the problem statement.

Admin, if you do not put the

udayabhanug @ 1 Sep 2009 10:46 PM

Admin, if you do not put the restriction that, at least one pin should be knocked, the user might pass-on a losing situation to the other person, by knocking off nothing. And, this can continue till forever.

 If i have understood

prodigyaj @ 1 Sep 2009 10:47 PM

 If i have understood correctly either player can knock down UPTO K adjacent pins right i.e they can take out 1..K pin as per their choice best suited for the game ?


AJ

"There is no such

gerrob @ 1 Sep 2009 11:30 PM

"There is no such restriction in the problem statement."

 

Then for example the first game would never finish if Mary knocks down the middle pin, then Johnny do nothing, and in the next round if Mary knocks down a pin then Johnny wins, so she is also do nothing. And this continues.

Sorry for the goof-up. At

admin @ 1 Sep 2009 11:38 PM

Sorry for the goof-up. At every turn Mary or Johnny has to knock down atleast one pin.

@Udaya Bhanu    I guess it is

nibble @ 2 Sep 2009 12:09 AM

@Udaya Bhanu

 

 I guess it is assumed as there is nothing mentioned about skipping turn. The other thing is that they are expert bowlers , that is mentioned so if they play they will take out atleast one pin.

 

i would like to know what is

dwrakh @ 2 Sep 2009 12:45 AM

i would like to know what is k here?

 

There's an image below the

arahant7 @ 2 Sep 2009 02:11 AM

There's an image below the question text which is not being displayed. (it shows an X)

From the page source, this image is

<img src="//codechef.com/AUG09/content/bowling.jpg" alt="" width="400" />
 

@Jeetendra No, even if Mary

admin @ 2 Sep 2009 01:46 PM

@Jeetendra No, even if Mary knocks out the last 3 pins, she doesn't win.

@kartik 'k' is the maximum number of pins that can be knocked out as explained in the problem statement.

@Aiman Will check out the image issue.

in second case-10110111 if

Olympian @ 2 Sep 2009 07:36 PM

in second case-10110111

if mary knocks last 3 pins, it becomes-

10110000

if john knocks the next two pins-

10000000

now mary can knock remaining and win the game.

why wont mary win if she

prashanth_sada @ 2 Sep 2009 10:22 PM
why wont mary win if she knocks out the last three pins??

@ Zeeshan, Prashanth..... If

tsycho @ 3 Sep 2009 12:36 AM
@ Zeeshan, Prashanth..... If Mary knocks out the last 3, it becomes 10110000 John won't knock out the next 2 pins, he'll knock out only 1 of them 10100000 Now Mary will have to choose between one of the 2 non-adjacent ones, and John will win by knocking off the last one.

When is last date to submit

ganges @ 3 Sep 2009 04:52 PM

When is last date to submit ?

 

 

The contest ends on the 11th

admin @ 3 Sep 2009 05:16 PM

The contest ends on the 11th of September at 15:00 hrs.

ya,i got it ani,i missed it

dwrakh @ 3 Sep 2009 07:56 PM

ya,i got it ani,i missed it out while reading the program,thank you

@codeChef,   not able to

yashwa7 @ 3 Sep 2009 10:09 PM

@codeChef,

 

not able to download my own code by clicking plain text after I view my solution, can you please fix it sometime?

@codechef admin :  for case

sandeepkkothari @ 5 Sep 2009 04:51 PM

@codechef admin : 

for case 3: marry's move should be: 10000111

since it is mentioned in the problem that in case of several solution show her the move in which the pins taken out are as near to the left as possible. ..pls clarify this.

 

No, in that case Mary will

admin @ 5 Sep 2009 05:30 PM

No, in that case Mary will not win.

Hi   I'd like to know what

kyun @ 7 Sep 2009 12:54 PM

Hi

 

I'd like to know what kind of runtime error my code experiences (latest id 88855) . I see Runtime error (SIGSEGV) almost as soon as I submit. I see Runtime Error (Other), which I assume is too much memory (?) on the 'my submissions' page. The email I receive says NZEC. I'd really like to know which of the three it is.

 

Thanks!

 

It's the first one. You are

admin @ 7 Sep 2009 02:20 PM

It's the first one. You are getting a segfault.

if the input is 6

o.herna1 @ 10 Sep 2009 01:35 PM

if the input is

6 3

111111

can marry takes 2 pins from the most left and the state becomes 001111?

Yes, but that need not be the

admin @ 10 Sep 2009 02:01 PM

Yes, but that need not be the winning move.

  WHY ARE THERE ONLY PIIN NOT

fahadahmed @ 12 Sep 2009 12:46 AM

 

WHY ARE THERE ONLY PIIN NOT IN THE LAST ROW

 

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com