TWOKINGS - TRIVIADOR



TRIVIADOR

Triviador is a war between two Kings. A king can attack an enemy region at each step. When a king attacks a region, he conquers all the enemy regions connected to it(not just the immediate ones). All the 8 regions around any region are connected to it. The kings get alternate chances to attack. King1 gets the chance to attack first. Assume both kings are intelligent and find who will conquer the whole territory at the end of the war. It can be proved that one of the Kings can win for sure if he is intelligent!

 

Input Specification:

The first line is an integer t, denotes the number of test cases. In each test case the first line consists of two integers denoting the number of rows and columns in the territory (each cell is a region). Then the description of each cell follows. Every region contains a character ‘X’ if it is owned by king1 or ‘O’ otherwise.


Output Specification:
For each test case output the result in a single line ‘X’ if king1 wins or ‘O’ if king2 wins.

 

Input Constraints:

T<=100

1<=rows,columns<=10

 

Sample input:


3

3 3

XOX

XXX

XOX

 

3 5

XXXXX

XXXOO

XXXOO

 

4 4

XXXX

OOOO

XXXX

OOOO


Sample Output:

O

X

X

 

Explanation of testcase 1 and 2:

Case 1: King1 has two possibilities to attack. But after attacking any of them, he will lose surely when king2 attacks back.

Case 2: King1 can attack any of the four enemy positions. He conquers all the connected enemy positions. So this is a one step victory.


Try the 1D version of this problem : http://spoj.com/problems/QWERTY04/
Try the 3D version of this problem : http://spoj.com/problems/CONQUER/


hide comments
krcma96: 2014-04-09 21:10:58

Can someone post more test cases please, my program works for all three bu it shows WA when it's running 10. Can anybody help me id 11416799?

Aleksandar: 2014-04-03 16:56:43

can someone post some more test cases pls

Relja: 2014-04-02 13:39:10

@Peyman (not just the immediate ones)

Swapnil R.Mehta: 2013-04-14 22:26:55

@cegprakash
pls have a look at my code id=8315714.
getting WA.
HAVE CODED TO MUCH HARD!PLS HELP!

Mitch Schwartz: 2013-04-14 22:26:55

I just want to confirm my interpretation of connectedness for this problem:

XOXO
OXOX
XOXO
OXOX

is a one-step victory for King1 because all of King2's regions are connected, right?

Edit: It's right.

Last edit: 2012-09-15 18:52:43
cegprakash: 2013-04-14 22:26:55

Sample case 1:
Regardless of King1's move, King 2 will win in his first move

Sample case 2:
King1 wins in his first move

Sample case 3:
King1 wins in his second move if he chooses his first move intelligently

Last edit: 2012-09-18 03:00:43
Aditya Pande: 2013-04-14 22:26:55

plz explain the games with examples

cegprakash: 2013-04-14 22:26:55

@peyman: both kings attack optimally!

Peyman: 2013-04-14 22:26:55

The sample says that in Case 3, King1 will win. But what if King2 uses the strategy of attacking whichever region King1 has just attacked? How can King1 ever conquer the whole territory in this scenario?


Added by:cegprakash
Date:2012-01-02
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64