Understanding this

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.


That's a solution for the problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdio>
#include <iostream>
#include <vector>

int main(){

    int n(0), m(0); scanf("%d %d\n", &n, &m);
    std::vector<std::string> board(n);

    for(int row = 0; row < n; row++){getline(std::cin, board[row]);}

    for(int row = 0; row < n; row++){
        for(int col = 0; col < m; col++){
            if(board[row][col] == '.'){
                if((row + col) % 2){board[row][col] = 'B';}
                else{board[row][col] = 'W';}
            }
            std::cout << board[row][col];
        }
        std::cout << std::endl;
    }

    return 0;
}


I could understand what was done there except for (i+j)%2 == 0 condition. Why? Another condition was (i+j)&1 which is bitwise but still why?
What you can tell about N, if N%2 == 0?


Bitwise AND:
The value 1 has the least significant bit as 1 and all the others as 0, does it not?
The least significant bit contributes +1 to the value, while the other bits add 2,4,8,...
The least significant bit is odd, while all the other are even.

The AND sets bit k to 1 if bit k in both operands is 1. Therefore, in N&1 all the other bits in result are always 0 and only the least significant bit can be either, depending on the least significant bit of N.

The result of N&1 is either 0 or 1.

Compiler does implicitly convert an integer an into bool for evaluating a condition. The value 0 converts to false. All others (the 1 included) convert to true.
I still don't know what's the reason (i+j) %2 == 0 was used, what's the use of this condition in this code.
N%2 == 0 means that N/2 has remainder of 0.
In other words:
K=N/2
->
N==2*K

When a number is even? Or odd?
Noooooo you don't get what I mean. I said what is the USE of (i+j) %2 == 0 in this part of the code:
if((row + col) % 2) //line 15
1. (i+j) %2 == 0 is the opposite of (row + col) % 2

2. What is the value of row+col in each square?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>

int main() {
  int n = 0;
  int m = 0;
  std::cin >> n >> m;
  for ( int row = 0; row < n; ++row ) {
    for ( int col = 0; col < m; ++col ) {
      std::cout << ' ' << std::setw(3) << (row + col);
    }
    std::cout << '\n';
  }
  return 0;
}

Which squares have odd value? Are they adjacent?
¿why don't you try to solve the problem before searching for solutions?


It simply creates a valid arrangement for a board made fully on good cells. Then you may add any bad cells anywhere and the arrangement continues being valid.
It's not like I didnt try lol @ne555 . I did try but the only diff between this code and mine was this small if condition that I am asking about. I am not a newbie to reach for solutions without attempts. I do that only when I give up xD
Topic archived. No new replies allowed.