# Switching on the Lights works until 6th case

title. Here is my code (similar to solution on USACO.Guide):

``````#include <bits/stdc++.h>
using namespace std;

bool visitable(int x, int y, vector<vector<bool>> arr, int n) {
vector<int> hor = {0, 0, 1, -1};
vector<int> ver = {1, -1, 0, 0};
for (int i = 0; i < 4 ; i++) {
if (x+hor[i]<0 || x+hor[i]>=n || y+ver[i]<0 || y+ver[i]>=n) continue;
if (arr[x+hor[i]][y+ver[i]]) return true;
}
return false;
}

void kinda_floodfill(int curr_x, int curr_y, vector<vector<bool>> &visited, vector<vector<bool>> &light_on, vector<vector<vector<pair<int, int>>>> connections, int n, int &res) {
//cout << curr_x << " " << curr_y << "\n";
visited[curr_x][curr_y] = 1;
for (pair<int, int> neighbor: connections[curr_x][curr_y]) {
if (light_on[neighbor.first][neighbor.second] == 0) {
res++;
light_on[neighbor.first][neighbor.second] = 1;
}
if (!visited[neighbor.first][neighbor.second] && visitable(neighbor.first, neighbor.second, visited, n)) kinda_floodfill(neighbor.first, neighbor.second, visited, light_on, connections, n, res);
}
vector<int> hor = {0, 0, 1, -1};
vector<int> ver = {1, -1, 0, 0};
for (int i = 0; i < 4 ; i++) {
if (curr_x+hor[i]<0 || curr_x+hor[i]>=n || curr_y+ver[i]<0 || curr_y+ver[i]>=n) continue;
if (light_on[curr_x+hor[i]][curr_y+ver[i]] && !visited[curr_x+hor[i]][curr_y+ver[i]]) kinda_floodfill(curr_x+hor[i], curr_y+ver[i], visited, light_on, connections, n, res);
}
}

int main() {
ifstream fin("lightson.in"); ofstream fout("lightson.out");
int n, m;
fin >> n >> m;
vector<vector<bool>> light_on(n, vector<bool> (n));
vector<vector<bool>> visited(n, vector<bool> (n));
vector<vector<vector<pair<int, int>>>> connections (n, vector<vector<pair<int, int>>> (n));
for (int i = 0 ; i < m ; i++) {
int a, b, x, y;
fin >> a >> b >> x >> y;
connections[a-1][b-1].push_back(make_pair(x-1, y-1));
}
light_on[0][0] = 1;
int res = 1;
kinda_floodfill(0, 0, visited, light_on, connections, n, res);
fout << res;
return 0;
} text by 4 spaces``````