1421B.md (2958B)
1 Source: https://codeforces.com/problemset/problem/1421/B 2 3 B. Putting Bricks in the Wall 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Pink Floyd are pulling a prank on Roger Waters. They know he doesn't like [walls](https://www.youtube.com/watch?v=YR5ApYxkU-U), he wants to be able to walk freely, so they are blocking him from exiting his room which can be seen as a grid. 22 23 Roger Waters has a square grid of size $n\times n$ and he wants to traverse his grid from the upper left ($1,1$) corner to the lower right corner ($n,n$). Waters can move from a square to any other square adjacent by a side, as long as he is still in the grid. Also except for the cells ($1,1$) and ($n,n$) every cell has a value $0$ or $1$ in it. 24 25 Before starting his traversal he will pick either a $0$ or a $1$ and will be able to only go to cells values in which are equal to the digit he chose. The starting and finishing cells ($1,1$) and ($n,n$) are exempt from this rule, he may go through them regardless of picked digit. Because of this the cell ($1,1$) takes value the letter 'S' and the cell ($n,n$) takes value the letter 'F'. 26 27 For example, in the first example test case, he can go from ($1, 1$) to ($n, n$) by using the zeroes on this path: ($1, 1$), ($2, 1$), ($2, 2$), ($2, 3$), ($3, 3$), ($3, 4$), ($4, 4$) 28 29 The rest of the band (Pink Floyd) wants Waters to not be able to do his traversal, so while he is not looking they will invert at most two cells in the grid (from $0$ to $1$ or vice versa). They are afraid they will not be quick enough and asked for your help in choosing the cells. Note that you cannot invert cells $(1, 1)$ and $(n, n)$. 30 31 We can show that there always exists a solution for the given constraints. 32 33 Also note that Waters will pick his digit of the traversal after the band has changed his grid, so he must not be able to reach ($n,n$) no matter what digit he picks. 34 35 Input 36 37 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 50$). Description of the test cases follows. 38 39 The first line of each test case contains one integers $n$ ($3 \le n \le 200$). 40 41 The following $n$ lines of each test case contain the binary grid, square ($1, 1$) being colored in 'S' and square ($n, n$) being colored in 'F'. 42 43 The sum of values of $n$ doesn't exceed $200$. 44 45 Output 46 47 For each test case output on the first line an integer $c$ ($0 \le c \le 2$) — the number of inverted cells. 48 49 In $i$-th of the following $c$ lines, print the coordinates of the $i$-th cell you inverted. You may not invert the same cell twice. Note that you cannot invert cells $(1, 1)$ and $(n, n)$. 50 51 Example 52 53 Input 54 55 ``` 56 3 57 4 58 S010 59 0001 60 1000 61 111F 62 3 63 S10 64 101 65 01F 66 5 67 S0101 68 00000 69 01111 70 11111 71 0001F 72 ``` 73 74 Output 75 76 ``` 77 1 78 3 4 79 2 80 1 2 81 2 1 82 0 83 ``` 84 85 Note 86 87 For the first test case, after inverting the cell, we get the following grid: 88 89 ``` 90 S010 91 0001 92 1001 93 111F 94 ```