Funky Chess Board doubt

what’s wrong in my code?

#define ull unsigned long long
#define MOD 1000000007
#define ll long long
#define pb push_back
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define pii pair<ll, ll>
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define counting(v, m) count(v.begin(), v.end(), m)
#define sorted(v) sort(v.begin(), v.end())
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void yoho(vector<vector<bool>> &v1, vector<vi> v, ll x, ll y, ll n)
{
    if ((x >= n || y >= n || x < 0 || y < 0)){
        return;
    }
    if(v1[x][y] || v[x][y] == 0){
        return;
    }
    v1[x][y] = true;
    yoho(v1, v, x - 2, y - 1, n);
    yoho(v1, v, x - 2, y + 1, n);
    yoho(v1, v, x - 1, y - 2, n);
    yoho(v1, v, x - 1, y + 2, n);
    yoho(v1, v, x + 1, y - 2, n);
    yoho(v1, v, x + 1, y + 2, n);
    yoho(v1, v, x + 2, y - 1, n);
    yoho(v1, v, x + 2, y + 1, n);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t, n, m, c, k, q;
    string s;
    cin >> n;
    vector<vi> v = vector<vi>(n, vi(n, 0));
    vector<vector<bool>> v1 = vector<vector<bool>>(n, vector<bool>(n, false));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> v[i][j];
            if(v[i][j]==0){v1[i][j]=true;}
        }
    }
    c = 0;
    m=0;
    k=0;
    while(k<n){
        if(m==n){
            m=0;
            k++;
        }
        if(v[m][k]==0){m++;}
        else{break;}
    }
    yoho(v1,v,m,k,n);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (!v1[i][j] && v[i][j]==1)
            {
                c++;
            }
        }
    }
    cout << c << endl;
    return 0;
}

hi @piyush42soni_45ae618b01096aab,

This problem can be done using “recursion/dfs and similar” fashion. once you visit a cell, then visit all the cells that can be visited from this cell and also visit the cells that can be further visited from the next reachable cells.

refer this -->

1 Like