Codeforces Round #454 Div. 2 B

題目: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,以及對於判斷的時候記得用標記變量來進行;

別人代碼:

include<stdio.h>

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;

}

相關文章
相關標籤/搜索