Book of programming problems

Contains advanced programming problems for schools, colleges and problem-hungry inidividuals

Find a genuine princess among the spurious ones based on biofield level.
Complexity (1-100): 100
Each number in the matrix with dimensions W×H is a value of total biofield emanating from some number of princesses located in the same matrix. Each princess emits biofield in 8 directions (including the cell where the princess is standing): north, north-east, east, south-east, south, south-west, west, and north-west. The biofield level does not depend on the distance. All princesses have biofield level 1, except the genuine one, which has biofield level M. Example:
 2 0 0 2 0
0 2 0 2 0
0 0 2 2 2
2 2 2 2 2
0 0 2 2 2
M=2, and the genuine princess is in position (3,3), where the first number is the zero-based column index, and the second number is the zero-base row index.

Find the number of spurious princesses and the position of the genuine one. Print the error message if it is not possible. The data is stored in an input file in the following format:

Width Height
a00 a10 ... aw-1,0
a01 a11 ... aw-1,1
... ... ... ...
a0,h-1 a1,h-1 ... aw-1,h-1
M

Examples:
1. For the following input file (download):
5 5
4 3 4 1 2
5 4 4 5 4
4 3 5 1 1
1 5 0 4 0
2 4 1 2 4
3
The solution is: Number of spurious princesses: 2, genuine princess at: (1,1).
2. 3 matrix. Number of spurious princesses: 3, genuine princess at: (2,1).
3. 10×11 matrix. Number of spurious princesses: 45, genuine princess at: (6,0).
4. 30×31 matrix. Number of spurious princesses: 465, genuine princess at: (10,27).

Solution:Show/hide explanation | Solution.java
There are no posts in this topic.

Post Reply

Book of programming problems is powered by UseBB 1 Forum Software | Contact Admin