Why does this simple BFS give a runtime error on test case 21?

Revision en3, by mheideme1, 2017-04-21 23:50:57

I'm trying to solve the problem "Islands" of the following contest

http://codeforces.com/gym/101291/attachments/download/5256/20162017-acmicpc-pacific-northwest-regional-contest-div-2-en.pdf

This is my simple BFS solution, which produces a runtime error on testcase 21. Does anybody have an explanation?



#include <iostream> #include <queue> using namespace std; void check_position(int i, int k, char** grid, queue<pair<int,int> >& q){ if(grid[i][k]=='L' || grid[i][k]=='C'){ grid[i][k] = 'X'; q.push(pair<int,int>(i,k)); } } void bfs(int i, int k, char** grid, int n, int m){ queue<pair<int,int> > q; grid[i][k]='X'; q.push(pair<int,int>(i,k)); pair<int,int> temp; while(!q.empty()){ temp = q.front(); q.pop(); if (temp.first>0) check_position(temp.first-1,temp.second, grid, q); if (temp.second>0) check_position(temp.first,temp.second-1, grid, q); if (temp.second<m-1) check_position(temp.first,temp.second+1, grid, q); if (temp.first<n-1) check_position(temp.first+1,temp.second, grid, q); } } int main(){ int n,m; cin>>n>>m; char** grid = new char* [n]; for (int i=0;i<n;i++) grid[i] = new char[m]; for (int i=0;i<n;i++) cin>>grid[i]; int island_count = 0; for (int i=0;i<n;i++){ for (int k=0;k<m;k++){ if (grid[i][k]=='L'){ island_count++; bfs(i,k,grid,n,m); } } } cout<<island_count<<"\n"; }

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English mheideme1 2017-04-21 23:50:57 5
en2 English mheideme1 2017-04-21 23:50:22 25
en1 English mheideme1 2017-04-21 23:49:09 1521 Initial revision (published)