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 4329830
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:52:38+00:00 2026-05-21T09:52:38+00:00

Can someone please illuminate why I am getting the following error with the following

  • 0

Can someone please illuminate why I am getting the following error with the following code?

Execution error: Your program had this runtime error:
        Exceeded memory or invalid memory reference. The program ran for
        0.000 CPU seconds before the error. It used 3048 KB of memory. 
    ------ Data ------
    7 4 
    11 6 11 6 3 10 6 
    7 9 6 13 5 15 5 
    1 10 12 7 13 7 5 
    13 11 10 8 10 12 13 
    ----------------------------

Code:

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int castle[50][50];
int components[50][50];
bool visited[50][50];
int N, M;

void flood(int y, int x, int component) {
    if(visited[y][x]) return;
    visited[y][x] = true;
    components[y][x] = component;
    if(x>0 && !((castle[y][x] & 1) == 1)) { //West
        cout<<"castle["<<y<<"]["<<x<<"] just";
        cout<<"flooded west\n";
        flood(y, x-1, component);
    }
    if(y>0 && !((castle[y][x] & 2) == 2)) { //North
        cout<<"castle["<<y<<"]["<<x<<"] just";
        cout<<"flooded north\n";
        flood(y-1, x, component);
    }
    if(x<M && !((castle[y][x] & 4) == 4)) { //East
        cout<<"castle["<<y<<"]["<<x<<"] just";
        cout<<"flooded east\n";
        flood(y, x+1, component);
    }
    if(y<N && !((castle[y][x] & 8) == 8)) { //South
        cout<<"castle["<<y<<"]["<<x<<"] just";
        cout<<"flooded south\n";
        flood(y+1, x, component);
    }
}

int main() {
    ifstream inp("castle.in");
    ofstream out("castle.out");
    memset(components, -2, sizeof(components));
    memset(visited, false, sizeof(visited));
    inp >> M >> N;
    for(int y=0; y<N; y++) {
        for(int x=0; x<M; x++) {
            inp >> castle[y][x];
        }
    }

    int curComponent = 0;
    for(int y=0; y<N; y++) {
        for(int x=0; x<M; x++) {
            if(visited[y][x]) continue;
            flood(y,x,++curComponent); //first component will be 1
//          cout << "curComponent = " << curComponent <<"\n";
        }
    }
    int roomAreas[curComponent+2]; //need an extra one since curComponent actually starts at 1
    memset(roomAreas, 0, sizeof(roomAreas));
    int greatestArea = 0;
    for(int y=0; y<N; y++) {
        for(int x=0; x<M; x++) {
            cout << "components[" << y <<"][" << x <<"] = " << components[y][x] << "\n";
            roomAreas[components[y][x]]++;
            if(roomAreas[components[y][x]] > greatestArea) greatestArea = roomAreas[components[y][x]];
        }
    }
    int greatestCombined = 0, bestx=0, besty=0;
    char bestDir='N'; //Means none -> error
    for(int y=0; y<N; y++) {
        for(int x=0; x<M; x++) {
        /*
            if(x>0 && components[y][x] != components[y][x-1] && roomAreas[components[y][x]] + roomAreas[components[y][x-1]] > greatestCombined) { //West
                greatestCombined = roomAreas[components[y][x]] + roomAreas[components[y][x-1]];
                bestx = x;
                besty = y;
                bestDir = 'W';
            }   
        */
            if(y>0 && components[y][x] != components[y-1][x] && roomAreas[components[y][x]] + roomAreas[components[y-1][x]] > greatestCombined) { //North
                greatestCombined = roomAreas[components[y][x]] + roomAreas[components[y-1][x]];
                bestx = x;
                besty = y;
                bestDir = 'N';
            }   
            if(x<M && components[y][x] != components[y][x+1] && roomAreas[components[y][x]] + roomAreas[components[y][x+1]] > greatestCombined) { //East
                greatestCombined = roomAreas[components[y][x]] + roomAreas[components[y][x+1]];
                bestx = x;
                besty = y;
                bestDir = 'E';
            }
        /*          
            if(y<N && components[y][x] != components[y+1][x] && roomAreas[components[y][x]] + roomAreas[components[y+1][x]] > greatestCombined) { //South
                greatestCombined = roomAreas[components[y][x]] + roomAreas[components[y+1][x]];
                bestx = x;
                besty = y;
                bestDir = 'S';
            }   
        */
        }
    }
    out << curComponent << "\n" << greatestArea << "\n" << greatestCombined << "\n" << besty+1 << " " << bestx+1 << " " << bestDir << "\n";
    return 0;
}

Thanks so much!

  • 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-21T09:52:39+00:00Added an answer on May 21, 2026 at 9:52 am

    Possibly

    if(x<M && ...
    

    should be

    if(x<M-1 && ...
    

    Since in that part, it can recursively flood(x+1). That could mean passing in x equal to M, which is overflowing the bounds to the east.

    Similarly for the y flooding south check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explaing why the string is not splitted in the following code
Can someone please try and explain this code for me, I don't really understand
Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Can someone please help me in this minute error. I wrote this, (which is
Can someone please tell what this strange error is Mod_python error: PythonHandler django.core.handlers.modpython Traceback
Can someone please explain to me the below code. This behavior has been like
Can someone please derive a concrete example from the following: http://www.urdalen.com/blog/?p=210 ..that shows how
Can someone please help me on this find specific record Edit through MS access
Can someone please explain me this SQL query. This shows me the popular stores,
Can someone please explain what the following javascript statement is doing? var default_hide =

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.