Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7598337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:22:59+00:00 2026-05-30T22:22:59+00:00

I made this program that should flood fill the matrix but something went wrong.

  • 0

I made this program that should flood fill the matrix but something went wrong. Here’s the code:

queue<Point> Qu;    
int n,m;
cin>>n>>m;
int mat[n][m];
for(int i=0;i<n;++i)    
    for(int j=0;j<m;++j)
        cin>>mat[i][j];        

Point N,W,S,E,bgn;
bgn.x=0;
bgn.y=0;
Qu.push(bgn);
while(!Qu.empty()){
    N.x=Qu.front().x-1; N.y=Qu.front().y;
    S.x=Qu.front().x+1; S.y=Qu.front().y; 
    E.x=Qu.front().x; E.y=Qu.front().y+1; 
    W.x=Qu.front().x; W.y=Qu.front().y-1;

    if(mat[N.x][N.y]==0){mat[N.x][N.y]=2;Qu.push(N);} 
    if(mat[S.x][S.y]==0){mat[S.x][S.y]=2;Qu.push(S);} 
    if(mat[E.x][E.y]==0){mat[E.x][E.y]=2;Qu.push(E);} 
    if(mat[W.x][W.y]==0){mat[W.x][W.y]=2;Qu.push(W);}    
    Qu.pop();
}


for(int i=0;i<n;++i){    
    for(int j=0;j<m;++j)
        cout<<mat[i][j]<<" ";        
    cout<<endl;
} 

Point is a struct I defined earlier in code and it contains only x and y as integers. The program fills the matrix correctly if it’s empty e.g. :
If i enter

3 3
0 0 0
0 0 0
0 0 0

I get the output :

2 2 2
2 2 2
2 2 2

But if i enter:

3 3
0 0 1
0 1 0
0 0 1

I get

2 2 1
2 1 2
2 2 1

instead of

2 2 1
2 1 0
2 2 1

If I check the coordinates after every pop, I can notice that it goes out of boundaries ( e.g. it returns the coordinates 1 -1 and it shouldn’t do it).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:23:00+00:00Added an answer on May 30, 2026 at 10:23 pm

    You set the coords of N,W,S and E, even if they are invalid; for example if you start at (0,0) and do this:

    N.x=Qu.front().x-1; 
    N.y=Qu.front().y;
    ...
    if(mat[N.x][N.y]==0) {
        mat[N.x][N.y]=2;
        Qu.push(N);
    } 
    

    N will be (-1,0). Instead you only check a direction if you are not at the border of the matrix. For example for N you could do:

    if(Qu.front().x > 0) {
        N.x=Qu.front().x-1; 
        N.y=Qu.front().y;
        if(mat[N.x][N.y]==0) {
            mat[N.x][N.y]=2;
            Qu.push(N);
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made this code with jQuery to fade images ( but not the one
I have made a program that continuously monitors a log file. But I don't
I'm trying to fix a bug in this code that was made for windows
I have a program i frequently use that is made with .NET. This program
I made a program in XNA 4.0 that should simulate an assembly line process.
I made this little Cocoa app with a WebView that displays Google Maps in
Hi I made this site a while ago in my table days but have
So I'll admit that this is a homework assignment, but I'm not asking you
Why does this program correctly display a message box, but does not set the
NOTE: This is the simple version of my previous question that SHOULD have been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.