题目:B. Tic-Tac-Toe
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.ide
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.post
样例:
Examples
Input
... ... ...
... ... ...
... ... ...学习
... ... ...
... ... ...
... x.. ...ui
... ... ...
... ... ...
... ... ...
6 4
Output
... ... ...
... ... ...
... ... ... this
... ... ...
... ... ...
... x.. ... spa
!!! ... ...
!!! ... ...
!!! ... ... rest
Input
xoo x.. x..
ooo ... ...
ooo ... ...code
x.. x.. x..
... ... ...
... ... ...orm
x.. x.. x..
... ... ...
... ... ...
7 4
Output
xoo x!! x!!
ooo !!! !!!
ooo !!! !!! ip
x!! x!! x!!
!!! !!! !!!
!!! !!! !!!
x!! x!! x!!
!!! !!! !!!
!!! !!! !!!
Input
o.. ... ...
... ... ...
... ... ...
... xxx ...
... xox ...
... ooo ...
... ... ...
... ... ...
... ... ...
5 5
Output
o!! !!! !!!
!!! !!! !!!
!!! !!! !!!
!!! xxx !!!
!!! xox !!!
!!! ooo !!!
!!! !!! !!!
!!! !!! !!!
!!! !!! !!!
提示:
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.
题目大意:就是两个下棋,会一开始在输入的时候只要是‘.’就是空着的,一共大的范围有9个小的9宫格且总体形式和小的同样,以后会输入两个数x,y,表示在总体里的位置,而后这里输入的x,y表示在总体范围的哪一行哪一列,这个决定下一步的能够走那里,决定法则为,这个最后一步在小9宫格中的那个位置就对应下一步在大的总体的9宫格的那块区域,这个区域就是下一步能够走的。若是这个区域已经没有空余的即无‘.’,则下一步能够随便下即只要有‘.’就能够下;
观看别人后思考获得的思路:在题目大意读懂以后,就先解决一开始整个9宫格的输入问题,由于中间有空格因此不能单纯的直接就整个字符串输入,而要将其分开输入&ai,&ai,&ai,别忘了对于字符串输入的是不用专门从头开始输入而且这种输入用%s时还能够经过这个空格隔开,因此对于字符串中有空格输入但不记录到字符串中的时候,能够经过这样分开输入。以后要处理的就是把输入的x,y对应在小范围的那个位置找出,经过每次减3来找到在小区间的位置,在从这个位置对于大区间的范围依次找‘.’并将其转化为‘!’。还要专门设置一个标记变量,来判断其是否存在空余的位置能够下便是否有‘.’。以后在全部if语句判断完以后,在专门设立一个if来判断其是否在原有的位置有空余,这里就是经过标记变量进行判断,之因此放在最后是由于这是全部状况的共性。若是没有空余再讲剩下的‘.’变成‘!’便可;输出看好输出格式就能够了;(这种复杂格式的输入输出,每每其判断条件都是比较好想到的,虽然不是规律性的,但必定是有所联系的,像这题的大9宫格和小9宫格,因此其在思考的时候先看是否有联系)
新的技巧:学习到对于分段在一行中输入字符串的方法&ai,&ai,&ai,以及对于判断的时候记得用标记变量来进行;
别人代码:
int main(void) { char a[9][9]; int i,j,x,y,flag=1; for(i=0;i<9;i++) scanf("%s%s%s",&a[i][0],&a[i][3],&a[i][6]); scanf("%d%d",&x,&y); while(x>3) x-=3; while(y>3) y-=3; if(x==1&&y==1) for(i=0;i<3;i++) for(j=0;j<3;j++) if(a[i][j]=='.') {a[i][j]='!';flag=0; } if(x==2&&y==1) for(i=3;i<6;i++) for(j=0;j<3;j++) if(a[i][j]=='.') {a[i][j]='!';flag=0;} if(x==3&&y==1) for(i=6;i<9;i++) for(j=0;j<3;j++) if(a[i][j]=='.') {a[i][j]='!';flag=0;} if(x==1&&y==2) for(i=0;i<3;i++) for(j=3;j<6;j++) if(a[i][j]=='.') {a[i][j]='!';flag=0;} if(x==2&&y==2) for(i=3;i<6;i++) for(j=3;j<6;j++) if(a[i][j]=='.') {a[i][j]='!';flag=0;} if(x==3&&y==2) for(i=6;i<9;i++) for(j=3;j<6;j++) if(a[i][j]=='.') {a[i][j]='!';flag=0;} if(x==1&&y==3) for(i=0;i<3;i++) for(j=6;j<9;j++) if(a[i][j]=='.') {a[i][j]='!';flag=0;} if(x==2&&y==3) for(i=3;i<6;i++) for(j=6;j<9;j++) if(a[i][j]=='.') {a[i][j]='!';flag=0;} if(x==3&&y==3) for(i=6;i<9;i++) for(j=6;j<9;j++) if(a[i][j]=='.') {a[i][j]='!';flag=0;} if(flag) {for(i=0;i<9;i++) for(j=0;j<9;j++) if(a[i][j]=='.') a[i][j]='!'; } for(i=0;i<9;i++) { if(i%3==0&&i) printf("\n"); for(j=0;j<9;j++) { if(j%3==0&&j) printf(" "); printf("%c",a[i][j]); } printf("\n"); } return 0;
}