Connection Status:
Competition Arena > SeatingPlan
SRM 320 · 2006-09-30 · by lyc1977 · Dynamic Programming, Math
Class Name: SeatingPlan
Return Type: String
Method Name: expectedTrial
Arg Types: (int, int, int)
Problem Statement

Problem Statement

Right before the day of the final exam, a teacher is busy coming up with a seating assignment for the venue. The seats in the exam venue are arranged as m rows, each containing n seats, i.e., an m by n grid. The total number of students is exactly the same as the total number of seats, so all the seats will be occupied.

The teacher's task is complicated by the fact that there are k students who are suspected cheaters. In order to prevent cheating, the teacher will not allow any two suspected cheaters to sit in adjacent seats. Two seats are adjacent if and only if they are in adjacent columns of the same row, or adjacent rows of the same column.

The teacher's method for assigning the seats is simple. She finds a random seating assignment. Then, she checks if there are any suspected cheaters assigned to adjacent seats. If so, she repeats the process by finding another random assignment, and otherwise, she is done.

Your job is to determine the expected number of random seating assignments the teacher must generate before she finishes her task. Return this expected number as a String formatted "p/q" (quotes for clarity only), where p and q are relatively prime positive integers with no leading zeros. If there is no valid seating assignment, return "Impossible!" (quotes for clarity only).

Constraints

  • m will be between 1 and 80, inclusive.
  • n will be between 1 and 80, inclusive.
  • m*n will be less than or equal to 80.
  • k will be between 0 and 20, inclusive, and less than or equal to m*n.
Examples
0)
1
1
0
Returns: "1/1"
1)
1
1
1
Returns: "1/1"
2)
1
2
0
Returns: "1/1"
3)
2
1
1
Returns: "1/1"
4)
1
2
2
Returns: "Impossible!"
27)
2
3
0
Returns: "1/1"

Since there is no cheater, the teacher can finish the task with the first trial.

29)
2
3
2
Returns: "15/8"

Let S represent a suspected cheater, and N a non-cheater. All possible acceptable seating plans: SNS SNN SNN NSN NSN NNS NNS NNN NNN NSN NNS SNN NNS SNN NSN SNS And all possible unacceptable seating plans: SSN NSS NNN NNN SNN NSN NNS NNN NNN SSN NSS SNN NSN NNS

Submissions are judged against all 82 archived test cases, of which 7 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SeatingPlan with a public method string expectedTrial(int m, int n, int k) · 82 test cases · 2 s / 256 MB per case

Submitting as anonymous