In this HackerEarth Valid Chess Board problem solution, you are given a tiled chart paper of N rows and M columns. There are a total of N x M tiles in it. Each tile is colored either black or white. Now, you need to count how many ways are there to cut valid chessboards of size 8 x 8 out of this chart paper.


HackerEarth Valid Chess Board problem solution



HackerEarth Valid Chess Board problem solution.

#include<bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define endl "\n"
#define eps 0.00000001
LL pow(LL a,LL b,LL m){ a%=m;LL x=1,y=a;while(b > 0){if(b%2 == 1){x=(x*y);if(x>m) x%=m;}y = (y*y);if(y>m) y%=m;b /= 2;}return x%m;}
LL gcd(LL a,LL b){if(b==0) return a; else return gcd(b,a%b);}
LL gen(LL start,LL end){LL diff = end-start;LL temp = rand()%start;return temp+diff;}
using namespace std;
char a[1001][1001];
int f(int x1,int y1,int x2,int y2) {
for(int i = x1; i <= x2; i++) {
for(int j = y1 + 1; j <= y2; j++) {
if(a[i][j] == a[i][j - 1]) {
return 0;
}
}
}
for(int j = y1; j <= y2; j++) {
for(int i = x1 + 1; i <= x2; i++) {
if(a[i][j] == a[i - 1][j]) {
return 0;
}
}
}
return 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
int ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(i + 8 <= n && j + 8 <= m) {
ans = ans + f(i , j , i + 7 , j + 7);
}
}
}
cout << ans;
}


Second solution

#include <bits/stdc++.h>

using namespace std;

const int N = 1E3 + 5;
string mat[N];

bool f(int x, int y, int n, int m) {
for(int i = x; i < x + 8; i ++) {
for(int j = y; j < y + 7; j ++) {
if(mat[i][j] == mat[i][j + 1] || (i < x + 7 && mat[i][j] == mat[i + 1][j]))
return 0;
}
}
return 1;
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i ++)
cin >> mat[i];
int cnt = 0;
for(int i = 0; i < n; i ++) {
for(int j = 0; j < m; j ++) {
if(i + 8 <= n && j + 8 <= m && f(i, j, n, m))
cnt ++;
}
}
cout << cnt;
return 0;
}