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
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » Codechef Campus Snackdown » The Matrix Game

The Matrix Game

Problem code: SNCK01

  • All Submissions

All submissions for this problem are available.

[Problem Idea : Varun Jalan]

Two players A and B play the following game.

1) First, a matrix M of size N*M is chosen, and filled with non-zero numbers.

2) Player A starts the game and the players play alternately.

3) In his turn, a player chooses any row which has atleast one non zero nuumber in it. In this row, the leftmost non zero number is chosen. Let this number be K. The player subtracts any number between 1 and K inclusive from it.

4) The game ends when all the numbers in the matrix M are 0.

5) The player who plays last wins the game.

Given N,M and the initial matrix, determine who wins the game.

Assume that both players play optimally.

Input:

The first line contains the number of test cases T.

Each test case consists of 2 numbers N and M. There follow N lines each having M integers. The jth number on the ith line is the number M[i][j].

There is a blank line between consecutive test cases.

Output:

Output T lines, one for each case. Output "FIRST" if player A wins, else output "SECOND".

Constraints :

T <= 1000

1 <= N,M <= 50

The initial matrix values are between 1 and 50 inclusive.

Time limit : 2 seconds.

Sample Input :

3
2 2
1 1
1 1

1 3
2 1 1

2 2
3 2
3 2

Sample Output:

SECOND
FIRST
SECOND




Date: 2009-11-13
Time limit: 2

s
Source limit: 50000
Languages: C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK JS


  • Submit

Comments

  • Login or Register to post a comment.

what are test cases...?!!

OneManArmy @ 21 Nov 2009 07:17 PM

what are test cases...?!!

line 5: "substracts any

TriPas @ 21 Nov 2009 08:35 PM

line 5: "substracts any number means" ... one(1) number?

line 5: "substracts any

TriPas @ 21 Nov 2009 08:35 PM

line 5: "substracts any number" means ... one(1) number?

From K he can substract any

admin @ 21 Nov 2009 08:38 PM

From K he can substract any number between 1 and K and not necessarily 1 or K.

we should subtract between

voidpointer @ 21 Nov 2009 09:12 PM

we should subtract between 1-k ??

do we subtract from the whole

LnR @ 21 Nov 2009 09:22 PM

do we subtract from the whole ROW or just from tat cell

Yah its me again Busted again

busted @ 21 Nov 2009 09:25 PM

Yah its me again Busted again assholes!!!!!!!!!!!!

 

Come on fuckin block my account?

I got more brains than your dad fuckers.

Heard of dynamic IP? Yah u gonna get your asses fucked motherfuckers!

Cya later, i gotta go and fuck ur moms.

 

send a mail to fuckmesideways@gmail.com if u need a pic of me fuckin ur mom.

 

SAALO BHOSADIKO JAAKE APNI MAA CHUDAO.

SABKA TIME KHARAB KAR RAHE HO

i know d fuckin prizes u got

busted @ 21 Nov 2009 09:28 PM

i know d fuckin

prizes u got 4 the top20 muddafuckas!!

u got ur moms 2 b fucked wid niggas!!

wid deir 14 inch dicks stucked b/w ur moms legs

 

yeah!!baby

u got it rite

big black dicks in addn to dildos!!!!!1

gotta go fuckers

@ Lnr Kindly read the

admin @ 21 Nov 2009 09:31 PM

@ Lnr

Kindly read the question carefully.

@ BUSTED Please have

admin @ 21 Nov 2009 09:32 PM

@ BUSTED

Please have patience. There are many people who have got accepted submissions.

what does optimally mean???  

richi18007 @ 21 Nov 2009 10:09 PM

what does optimally mean???

 

For the 3rd sample input /

Avatars @ 21 Nov 2009 10:42 PM

For the 3rd sample input / output, if we follow this way, then I can always FIRST win:

 

1st chance:

1 2

3 2

3 2

 

If SECOND touches any of 2nd or 3rd row, I do the same on 3rd or 2nd row respectively.

And if he changes 1st row to 0 2, I make it 0 0.

So, how is SECOND winning here ?

:)

BackBenchers @ 21 Nov 2009 11:37 PM

:)

@avtar.. i think u have ti

LnR @ 21 Nov 2009 11:43 PM

@avtar.. i think u have ti wrong..

2 2 in the frst line is the dim of the matrix.

 

woops :) .. when would these

saurabhagg @ 22 Nov 2009 01:02 AM

woops :) ..

when would these questions be put up on the practice session ?

I believe this problem is

LTUnited @ 22 Nov 2009 01:29 AM

I believe this problem is based on Grundy numbers. But I can't understand why my solution doesn't work. I treat each row a subgame. And then calculate XOR of Grundy number of each of the subgames.

To solve a subgame, let's say that when Grundy number is equal to 0, the player to go loses the game, when Grundy number is > 0, player wins the game. If there is a single positive number in a row, then this is basically a nim game, and Grundy number of a single pile will be the same as the number.

By induction we can solve the problem for more numbers on a single row, this can be easily found by drawing the graph of states with Grundy numbers.

Unfortunately I got WA, is there anyone with AC who could describe the solution?

In the 2nd case: 1 32 1

CCWinners @ 22 Nov 2009 03:33 AM

In the 2nd case:

1 3
2 1 1

shuoldn't the player B win??

No, the first players

triplem @ 22 Nov 2009 03:39 AM

No, the first players subtracts 2 from the first number and will win.

LTUnited // Although I didn't

Emotional_Expression @ 22 Nov 2009 10:05 AM

LTUnited // Although I didn't understand your solution perfectly, especially for the part "By induction we can solve the problem for more numbers on a single row, this can be easily found by drawing the graph of states with Grundy numbers.", my algorithm is quite similar to yours. I guess you made some mistakes during the contest... ;_;

@LTUnited I think if you have

imrankane2005 @ 22 Nov 2009 11:37 AM

@LTUnited I think if you have a correct algorithm, it will not take much to get it AC as it's easy in implementation. So if you can explain how you work with your induction part, we can help more here.Rest of your explanation part is same as what i did.

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 computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various 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 judge accepts solutions in over 35+ programming languages. Online programming was 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 competitions 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 programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

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. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

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