I am working on a flood fill task, but my code is giving incorrect answers on all but the sample input. The problem statement is here: http://usaco.org/index.php?page=viewproblem2&cpid=716
I have tried many things, including prining out intermediate steps (you can see the commented-out code) and generating my own test cases, all of which gave the correct answer. However, I get wrong answer on almost all official test cases. Can anybody find what is wrong with my code?
My code is below:
#include <iostream>
#include <vector>
#include <fstream>
#define cif cows[i].first
#define cis cows[i].second
#define cjf cows[j].first
#define cjs cows[j].second
using namespace std;
int n, k, r;
int grid[102][102][4];
int paints[102][102];
bool seen[102][102];
// 0 -> n, 1 -> e, 2 -> s, 3 -> w
vector<pair<int, int> > look(int row, int col) {
vector<pair<int, int> > adj;
if (row - 1 >= 1 && grid[row][col][0] != 1) {
if (!seen[row-1][col]) adj.push_back(make_pair(row-1, col));
}
if (row + 1 <= n && grid[row][col][2] != 1) {
if (!seen[row+1][col]) adj.push_back(make_pair(row+1, col));
}
if (col - 1 >= 1 && grid[row][col][3] != 1) {
if (!seen[row][col-1]) adj.push_back(make_pair(row, col-1));
}
if (col + 1 <= n && grid[row][col][1] != 1) {
if (!seen[row][col+1]) adj.push_back(make_pair(row, col+1));
}
return adj;
}
void search(int row, int col, int paint) {
seen[row][col] = true;
paints[row][col] = paint;
for (pair<int, int> p : look(row, col)) {
search(p.first, p.second, paint);
}
}
int main() {
ofstream fout("countcross.out");
ifstream fin("countcross.in");
for (int i = 1; i <= 101; i++) {
for (int j = 1; j <= 101; j++) {
seen[i][j] = false;
}
}
fin >> n >> r >> k;
for (int i = 0; i < r; i++) {
int a, b, x, y;
fin >> a >> b >> x >> y;
if (a > x) {
grid[a][b][0] = 1;
grid[x][y][2] = 1;
}
if (x > a) {
grid[a][b][2] = 1;
grid[x][y][0] = 1;
}
if (b > y) {
grid[a][b][3] = 1;
grid[x][y][1] = 1;
}
if (y > b) {
grid[a][b][1] = 1;
grid[x][y][3] = 1;
}
}
/*
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k < 4; k++) {
cout << grid[i][j][k];
}
cout << " ";
}
cout << "\n";
}
cout << "\n";
*/
vector<pair<int, int> > cows;
for (int i = 0; i < k; i++) {
int a, b; fin >> a >> b;
cows.push_back(make_pair(a, b));
}
int paint = 0;
search(1, 1, paint);
paint++;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (!seen[i][j]) {search(i, j, paint); paint++;}
}
}
// check for distant pairs
int ans = 0;
for (int i = 0; i < k; i++) {
for (int j = i + 1; j < k; j++) {
if (paints[cif][cis] != paints[cjf][cjs]) {ans++;}
}
}
/*
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << paints[i][j] << " ";
}
cout << "\n";
}
*/
fout << ans << "\n";
return 0;
}