您的位置:首页 > 其它

907B - Tic-Tac-Toe

2017-12-25 19:10 309 查看
B. Tic-Tac-Toe

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Two bears are playing tic-tac-toe via mail. It's boring for them to play usual tic-tac-toe game, so they are a playing modified version of this game. Here are its rules.

The game is played on the following field.



Players are making moves by turns. At first move a player can put his chip in any cell of any small field. For following moves, there are some restrictions: if during last move the opposite player put his chip to cell with coordinates (xl, yl) in
some small field, the next move should be done in one of the cells of the small field with coordinates (xl, yl).
For example, if in the first move a player puts his chip to lower left cell of central field, then the second player on his next move should put his chip into some cell of lower left field (pay attention to the first test case). If there are no free cells
in the required field, the player can put his chip to any empty cell on any field.

You are given current state of the game and coordinates of cell in which the last move was done. You should find all cells in which the current player can put his chip.

A hare works as a postman in the forest, he likes to foul bears. Sometimes he changes the game field a bit, so the current state of the game could be unreachable. However, after his changes the cell where the last move was done is not empty. You don't need
to find if the state is unreachable or not, just output possible next moves according to the rules.

Input

First 11 lines contains descriptions of table with 9 rows and 9 columns which are divided into 9 small fields by spaces and empty lines. Each small field is described by 9 characters without spaces and empty lines. character "x"
(ASCII-code 120) means that the cell is occupied with chip of the first player, character "o" (ASCII-code 111) denotes a field occupied with chip of the second
player, character "." (ASCII-code 46) describes empty cell.

The line after the table contains two integers x and y (1 ≤ x, y ≤ 9).
They describe coordinates of the cell in table where the last move was done. Rows in the table are numbered from up to down and columns are numbered from left to right.

It's guaranteed that cell where the last move was done is filled with "x" or "o".
Also, it's guaranteed that there is at least one empty cell. It's not guaranteed that current state of game is reachable.

Output

Output the field in same format with characters "!" (ASCII-code 33) on positions where the current player can put his chip. All other cells should not be modified.

Examples

input
... ... ...
... ... ...
... ... ...

... ... ...
... ... ...
... x.. ...

... ... ...
... ... ...
... ... ...
6 4


output
... ... ...
... ... ...
... ... ...

... ... ...
... ... ...
... x.. ...

!!! ... ...
!!! ... ...
!!! ... ...


input
xoo x.. x..
ooo ... ...
ooo ... ...

x.. x.. x..
... ... ...
... ... ...

x.. x.. x..
... ... ...
... ... ...
7 4


output
xoo x!! x!!
ooo !!! !!!
ooo !!! !!!

x!! x!! x!!
!!! !!! !!!
!!! !!! !!!

x!! x!! x!!
!!! !!! !!!
!!! !!! !!!


input
o.. ... ...
... ... ...
... ... ...

... xxx ...
... xox ...
... ooo ...

... ... ...
... ... ...
... ... ...
5 5


output
o!! !!! !!!
!!! !!! !!!
!!! !!! !!!

!!! xxx !!!
!!! xox !!!
!!! ooo !!!

!!! !!! !!!
!!! !!! !!!
!!! !!! !!!


Note

In the first test case the first player made a move to lower left cell of central field, so the second player can put a chip only to cells of lower left field.

In the second test case the last move was done to upper left cell of lower central field, however all cells in upper left field are occupied, so the second player can put his chip to any empty cell.

In the third test case the last move was done to central cell of central field, so current player can put his chip to any cell of central field, which is already occupied, so he can move anywhere. Pay attention that this state of the game is unreachable.

题意:题是真的难读,写这么长,,还这么难理解!恶心的一币。翻译出来都还是不懂。。。。。看了别人博客就明白题意了,输入11行,包括空行,一共有9个小九宫格,

组成一个大九宫格,(就是我们玩的井字游戏看作九宫格),然后在个空格里可以下x或者O(空格是 . 符号),下了的不能在下了。在小九宫格里下的位置,把对应大九宫格的位置可以下的位置替换为"!",例如样例1,在小九宫格左下角下面下的,就把大九宫格的左下角. 替换为!。如果无法替换(替换的地方落子了)或者没有对应替换的地方,就把大九宫格全部的.替换为!,例如样例2和3.

题解:模拟替换的过程,但是难在字符串的处理和找替换的开始的行列,要找规律,用技巧。因为有空格,用gets读入行列会出错,不知道为什么。。。参考别人用getline。

还有就是不读入空格,一个一个直接读入里面的字符,最后输出自己加空格。

#include<bits/stdc++.h>
using namespace std;
int main()
{
string s[11];
int x,y,flag=0;
for(int i=0; i<11; i++) ///算上空行11行
getline(cin,s[i]);  ///g不知道为什么gets读入,会出问题,参考别人getline
cin>>x>>y;
x--,y--;   ///技巧处理x,y减去一在%3,用于找需要替换开始的地方
x%=3,y%=3; ///***
int m=x*3+x,n=y*3+y;///***
for(int i=m; i<m+3; i++)   ///算出的m,n就是小九宫格对应达大九宫格开始的行列
for(int j=n; j<n+3; j++)
{
if(s[i][j]=='.')  ///(s[I][j]!='x'&&s[i][j]!='o')如果是可以落子的地方,就换成!
{
s[i][j]='!';
flag=1;    ///如果没有可以替换的地方,就标记flag=1,全部九宫格可以落子的地方替换为!

}
}
if(!flag)       ///全部九宫格可以落子的地方替换为!
{
for(int i=0; i<11; i++)
for(int j=0; j<11; j++)
if(s[i][j]=='.')
s[i][j]='!';
}
for(int i=0; i<11; i++)
cout<<s[i]<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: