The use of this condition

DZY loves chessboard, and he enjoys playing with it.

He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.

You task is to find any suitable placement of chessmen on the given chessboard.

Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100).

Each of the next n lines contains a string of m characters: the j-th character of the i-th string is either "." or "-". A "." means that the corresponding cell (in the i-th row and the j-th column) is good, while a "-" means it is bad.

Output
Output must contain n lines, each line must contain a string of m characters. The j-th character of the i-th string should be either "W", "B" or "-". Character "W" means the chessman on the cell is white, "B" means it is black, "-" means the cell is a bad cell.

If multiple answers exist, print any of them. It is guaranteed that at least one answer exists.

Examples
input
1 1
.
output
B
input
2 2
..
..
output
BW
WB
input
3 3
.-.
---
--.
output
B-B
---
--B
Note
In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.

In the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output.

In the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are.



Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>


#define fl(n)    for(int i = 0; i < n; i++)

#define ll   long long
#define nl   endl
#define pb   push_back
#define mp   make_pair
#define PII  pair<int,int>

#define EPS  1e-9
#define INF  1e9


using namespace std;

char a[100+1][100+1];
int n, m;

bool isbad(int i, int j){
return (a[i][j] == '-');
}

int main()
{

    cin >> n >> m;

    for(int i = 0; i < n; i++)
    for(int j = 0; j < m; j++)
    cin >> a[i][j];


    int count = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(isbad(i,j)) continue;
            if((i+j) % 2 == 0) a[i][j] = 'B'; //I had "flags" instead of this condition before editing
            else a[i][j] = 'W';
        }
    }



    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cout << a[i][j];
        }
        cout << nl;
    }

    return 0;
}


In line 39 I used to have flags to switch between 'B' and 'W' but then this method of(i+j) %2 == 0, i don't get how it works exactly, I mean tracing it obviously gets the right answer but where does it come from? what does this condition even mean? (I obviously know it checks for the remainder of % 2 whether its 0 or 1 but that's not what I mean, I wannna know how does it work in THIS specific code and for this SPECIFIC question). Thanks in advance!
Last edited on
Chessboards alternate black and white squares on each row. Each row alternates from the previous row. Line 39 takes the sum of the row and the column (i+j) and determines if it is even or odd.

There are four conditions:
row odd, col odd: result even (black)
row odd, col even: result odd (white)
row even, col odd: result odd (white)
row even, col even: result even (black)
i is the row number and j is the column number.

The sum i+j is used to decide if a tile should be black or white. If the sum is even the tile is black. If the sum is odd the tile is white.

Here is an example 4x4 board showing the sum for each tile.

 0 | 1 | 2 | 3
---------------
 1 | 2 | 3 | 4 
---------------
 2 | 3 | 4 | 5
---------------
 3 | 4 | 5 | 6 

If you're standing on an even tile (=black) and move one up, down, left or right you will always land on an even odd tile (=white). If you're standing on an odd tile (=white) and move one up, down, left or right you will always land on an odd even tile (=black). This is exactly the check pattern that you want.
Last edited on
@Peter87
If you're standing on an even tile (=black) and move one up, down, left or right you will always land on an even tile (=white). If you're standing on an odd tile (=white) and move one up, down, left or right you will always land on an odd tile (=black). This is exactly the check pattern that you want.


You sure there are no mistakes here? it looks confusing to me :c
If I am standing on an even tile if I go left , right , up or down I should land on an odd one according to the illustration grid above, and if I am standing on an odd one, i'll always land on an even one in any direction. I hope I am not mistaken or am I missing something ?


Also @Abstraction
Chessboards alternate black and white squares on each row. Each row alternates from the previous row. Line 39 takes the sum of the row and the column (i+j) and determines if it is even or odd.

Is that something globally known about chess boards? the fact that if we number the colums and rows and add them together for each square and check for its evenness to know if it's black or white?
Last edited on
Kalcor wrote:
You sure there are no mistakes here?

Yes, I made a mistake. It's fixed now.
Is that something globally known about chess boards?

Yes. Additionally, the right most square (in a standard 8x8 board) in row 0 must always be white. Therefore, 0+7= odd (White).

Keep in mind that the evenness/oddness calculation in line 39 depends on numbering the rows and columns from 0. In standard algebraic chess notation, rows are numbered 1-8 and columns (files) are lettered A-H.
https://en.wikipedia.org/wiki/Chessboard
Thanks everybody!
Topic archived. No new replies allowed.