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 » Practice(easy) » The White Knight

The White Knight

Problem code: E1

  • Submit
  • All Submissions

All submissions for this problem are available.

You are given a chessboard of size NxN. There is a white knight and several black pawns located on the board. The knight can move similarly to the normal knight in the game of chess; however it can only move towards the right of the board (see the below figure).

The mission of the knight is to capture as many black pawns as possible. Its journey ends when it moves to the rightmost column of the board.

Compute the maximum number of black pawns the white knight can capture.

Input

The first line contains t, the number of test cases (about 10). Then t test cases follow.

Each test case has the following form:

  • The first line contains N, the size of the chessboard (4 ? N ? 1000).
  • Then N lines follow, each line containing N characters which may be '0', 'K' or 'P', corresponding to the empty cell, the white knight, and the black pawn, respectively. There is exactly one 'K' character in the whole of the board.

Output

For each test case, print in a single line the maximum number of black pawns that can be captured.

Example

Input:
1
5
K....
..P..
.P...
...P.
.....

Output:
2 


Author: admin
Date Added: 5-06-2009
Time Limit: 4 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.

My code was accepted ,but it

TheSadReaper @ 8 Sep 2009 06:31 PM

My code was accepted ,but it says it ran for 2.85s and used 7516 KB of memory.

The time limit above says 2s,am i missing something here....

There are multiple input

admin @ 8 Sep 2009 06:33 PM

There are multiple input files and the total time displayed is the sum of the times taken for the different input files.

in the problem statement its

aansu @ 22 Sep 2009 06:42 PM

in the problem statement its given that blank cells will be denoted by zero..

in sample input '.' is given..

explanation needed please...

thanx neways.. it doesnt

aansu @ 23 Sep 2009 01:07 AM

thanx neways.. it doesnt matter as we hace to check for K and P only..

should have read the whole question first..

i am getting correct answer

aadarsh1093 @ 11 Oct 2009 08:06 PM
i am getting correct answer for the sample input but still i am getting wrong answer when i submit my code. as far as i think my logic is correct. can you provide another test case

i am getting correct for the

aadarsh1093 @ 11 Oct 2009 08:13 PM
i am getting correct for the sample input but still i am getting wrong answer. as far as i think my logic is correct. can you provide another test case

i am getting correct answer

aadarsh1093 @ 11 Oct 2009 08:13 PM
i am getting correct answer for the sample input but still i am getting wrong answer. as far as i think my logic is correct. can you provide another test case

thanx... neways. got accepted

aadarsh1093 @ 11 Oct 2009 10:27 PM
thanx... neways. got accepted

i am getting correct answer

mij @ 29 Oct 2009 04:29 PM

i am getting correct answer for the sample input but still i am getting wrong answer when i submit my code. as far as i think my logic is correct. can you provide another test case

@admin i view the solution

dabbcomputers @ 20 Jan 2010 11:02 PM

@admin

i view the solution from this  problem submmited at the 1st positon and i copied the source code from solution and run it on my machine and i entered the same sample input and the output of the submitted solution is 0 and not match with require output then how it succsess fully submitted...

pls help.....

the N lines of a test

shubh09 @ 16 May 2010 11:15 PM

the N lines of a test case...do they represent the status of a row, or column??

They are rows.

triplem @ 17 May 2010 07:01 AM

They are rows.

@admin: There should be more

rajneesh2k10 @ 17 Jan 2011 11:01 AM

@admin:

There should be more test cases.

I am not able to find why my solution gives a wrong answer when submitted. Please give any test case where it fails.

Or it'll be nice of you if you can tell me where I am doing mistake.

My solution: http://www.codechef.com/viewsolution/425076

No, there shouldn't be more

triplem @ 17 Jan 2011 12:10 PM

No, there shouldn't be more test cases. The sample input is only there to make sure you understood the problem; it is always your job to generate real test cases as part of solving the problem.

I haven't looked at your main algorithm, but I don't think you understand how the memset function works.

@Stephen: Removed memset !

rajneesh2k10 @ 17 Jan 2011 02:45 PM

@Stephen:

Removed memset ! Still wrong!

http://www.codechef.com/viewsolution/425128

Algorithm works as:

1. board array contains the entire position of pawns and the initial position of knight as given in input.

2. knight array will keep all the possible positions of knight.

3. eaten array will keep the value of eaten pawns for possible positions of knight.

4. every row of board is scanned and the positions of knight is observed,  for every position the next 4 positions are determined and corresponding values of eaten array are updated.

5. If a pawn is there on the next knight position the eaten value is updated by current position's eaten value plus one, this is done if new value is greater than previous value.

6. If there is no pawn on the next knight position the the eaten value is simply copied, this is done if the previous value is smaller than the new value.

This way the last row of eaten array contains all the possible eaten pawn values starting from the knight's initial position. From that row the largest value is picked up giving us the result!!

Are you sure your board is

triplem @ 18 Jan 2011 04:55 AM

Are you sure your board is oriented the right way?

Problem says the knight goes

rajneesh2k10 @ 18 Jan 2011 10:26 AM

Problem says the knight goes from left to right. I assume it to go from top to bottom. Either way you do, you achieve the same thing. Right?

So will you consider

triplem @ 18 Jan 2011 02:11 PM

So will you consider capturing a pawn which starts above the knight like you are allowed to?

I am not grabbing any pawn

rajneesh2k10 @ 18 Jan 2011 03:36 PM

I am not grabbing any pawn above the position of the knight!!

If you see first the knight array is empty. Inside the solve function first the position of the knight is determined and after that using that position the algorithm proceeds and captures the pawns which are below that position.

That's my point. The problem

triplem @ 18 Jan 2011 03:37 PM

That's my point. The problem statement clearly shows the knight is allowed to move upwards.

That was blunder!! Thank

rajneesh2k10 @ 18 Jan 2011 05:44 PM

That was blunder!! Thank you.

Fixed that but still runtime error! :( :(

http://www.codechef.com/viewsolution/425852

Runtime errors are easy to

triplem @ 19 Jan 2011 04:08 AM

Runtime errors are easy to debug. In your sort of code it is almost always due to issues with array indices. Check them carefully and you'll find your mistake (albeit indirectly).

Now 'm loosing my temper!! I

rajneesh2k10 @ 19 Jan 2011 12:04 PM

Now 'm loosing my temper!!

I got why it was runtime error but now struck by wrong answer!

http://www.codechef.com/viewsolution/426336

Please help. I have no more patience.

Arrrrgggggghhh..... It's

rajneesh2k10 @ 19 Jan 2011 03:20 PM

Arrrrgggggghhh.....

It's running good on almost all test cases I have!!!!!! :( :( :( :(

Stephen! F1 !!!!!

rajneesh2k10 @ 20 Jan 2011 07:31 AM

Stephen! F1 !!!!!

@Admin: May I know for which

rajneesh2k10 @ 21 Jan 2011 01:16 PM

@Admin:

May I know for which test case my solution is giving wrong output?? I'm damn tired checking my solution! Please give me the test case for which my input is failing!!

@Admin: Tutorial to this

rajneesh2k10 @ 22 Jan 2011 03:33 PM

@Admin:

Tutorial to this problem (http://www.codechef.com/wiki/tutorial-white-knight) says to move back from the right most column. Whats wrong if we move from left to right??

@Admin My code is not

tricky @ 12 Mar 2011 06:24 PM

@Admin

My code is not accepted giving a runtime error stating the reason : SIGSEGV.

When I try to submit , it goes into processing forever but meanwhile I get an e-mail stating the above.

My code runs just fine when I try it on my computer.

http://www.codechef.com/views

cooltodinesh @ 30 Mar 2011 09:39 AM

http://www.codechef.com/viewsolution/499067

 

plz tel me y m getting wrong err.

when i have submitted in c

aayush123 @ 18 May 2011 03:54 PM
when i have submitted in c its giving wrong answer..:) changed to c++ wallah!!!! giving rite ans. what is this????????????????????????????????

@Admin: plz tell me for

klerigado @ 11 Jun 2011 02:32 PM
@Admin: plz tell me for which test case my solution is giving wrong answer. http://www.codechef.com/viewsolution/571330

@admin.. could you plz tell

kush87 @ 23 Jun 2011 04:47 PM
@admin.. could you plz tell me why am i getting runtime error(OTHER)... is it because i am using too much memory. or something else... My code is http://www.codechef.com/viewsolution/582187 Thanks

I want to know can this

imtiaz_cuet @ 22 Nov 2011 07:51 PM
I want to know can this problem be solvable using bfs?if yes then how?

@admin ...i got TLE ...pls

practice786 @ 18 May 2012 06:53 PM
@admin ...i got TLE ...pls help me... my code is http://www.codechef.com/viewsolution/1048725. thanks

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

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