1

I want to ask for help regarding my code for this problem in kattis.

https://open.kattis.com/problems/10kindsofpeople

When I tried to make the 'brr' array as a single dimension array it worked just fine. But when it is in a multi-dimension array, the program successfully runs, but it stops immediately. It won't even let me input. Can someone help me to find the problem?

#include <bits/stdc++.h>
using namespace std;
int r,c;
char arr[1005][1005];
void cari(int brr[][1005], int r1, int c1, int r2, int c2, bool &cek, char type){
    if(r1 == r2 && c1 == c2){
        if(type == arr[r2][c2]) cek = 1;
        return;
    }else if (r1 >= 1 && r1 <= r && c1 >= 1 && c1 <= c && type == arr[r1][c1] && brr[r1][c1] != 1){
        brr[r1][c1] = 1;
        cari(brr,r1-1, c1,r2,c2,cek,type);
        cari(brr,r1,c1+1,r2,c2,cek,type);
        cari(brr,r1,c1-1,r2,c2,cek,type);
        cari(brr,r1+1,c1,r2,c2,cek,type);
    }
}

int main (){
    cin >> r >> c;
    for(int i = 1; i <= r; i++){
        string s;
        cin >> s;
        for(int j = 1; j <= c; j++){
            arr[i][j] = s[j - 1];
        }
    }
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        int r1,c1,r2,c2;
        int brr[1005][1005];
        bool cek = 0;
        cin >> r1 >> c1 >> r2 >> c2;
        cari(brr,r1,c1,r2,c2,cek,arr[r1][c1]);
        if(cek && arr[r1][c1] == '0') cout << "binary" << endl;
        else if(cek && arr[r1][c1] == '1') cout << "decimal" << endl;
        else cout << "neither" << endl;
    }
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Prenagen24
  • 11
  • 1
  • Stack overflow I would guess. `int brr[1005][1005];` is a large array. – john Jul 05 '22 at 08:03
  • 4
    [Don't include that header file](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Don't use global variables. Don't hard-code sizes of arrays unless you really know that's the exact size needed. Don't add dummy elements to your arrays. Don't use one-based indexing or arrays. And don't use so-called "competition" or "judge" sites to learn programming. – Some programmer dude Jul 05 '22 at 08:03
  • 1
    As for your problem, local variables are typically located on the stack. And the stack is a limited resource. On Windows it's only a single MiB. Your array (of arrays) `brr` is using around 4MiB (`1005 * 1005 * sizeof(int)`, and `sizeof(int)` is typically equal to `4`). – Some programmer dude Jul 05 '22 at 08:05
  • @Someprogrammerdude Thank you for the explanation. But I was wondering, I'm now preparing for a competitive programming competition and my mentor said that sites like kattis, codeforces, etc are good for practice, why did you not suggest to use them? – Prenagen24 Jul 05 '22 at 08:15
  • 2
    @Prenagen24: Competitive programming sites are not fine for **learning** programming (No learning resources) and don't promote good practice (there are no error handling) (shown solution use generally bad practice (as include non standard header, MACRO, using `namespace std;`)) (to have the feeling to win some time). They actually rely on mathematical tricks or some algorithms (as naive ways tends to fails). they generally don't provide the learning resources. – Jarod42 Jul 05 '22 at 08:27
  • Once you have experience with a few programming languages, and a couple of years computer science education, such sites might be good when you have some time over to spend with useless brain-teasers. There's really not much else they're good for, even through they unfortunately are used by lazy teachers or lazy interviewers. – Some programmer dude Jul 05 '22 at 09:09
  • Nothing wrong with competitive programming any more than code golf. Good wholesome fun and keeps kids out of trouble. But the problems probably shouldn't be used for hiring application programmers as much as they are. That's probably why so much resentment. – ggorlen Jul 19 '22 at 03:54

0 Answers0