0

I've been working on the USACO problem Castle for a day now, but I can't seem to get over this bug in my code. Apparently, my conversion for the first argument in the function is wrong for findPath. I'm trying to pass a reference to a 2D array of strings into my function recursively. Any advice would be appreciated.

using namespace std;


int m, n;

string walls(int a){

    // west north east south

    if(a == 1){
        return "1000";
    }
    else if(a == 2){
        return "0100";
    }
    else if(a == 3){
        return "1100";
    }
    else if(a == 4){
        return "0010";
    }
    else if(a == 5){
        return "1010";
    }
    else if(a == 6){
        return "0110";
    }
    else if(a == 7){
        return "1110";
    }
    else if(a == 8){
        return "0001";
    }
    else if(a == 9){
        return "1001";
    }
    else if(a == 10){
        return "0101";
    }
    else if(a == 11){
        return "1101";
    }
    else if(a == 12){
        return "0011";
    }
    else if(a == 13){
        return "1011";
    }
    else if(a == 14){
        return "0111";
    }
    else{
        return "1111";
    }

}
//checking if a square isn't just four walls

bool isOpen(string s){

    if(s.find('0') != string::npos){
        return true;
    }
    return false;
}

bool isRight(string x, string y){

    //if there's a east well open for x, y must have a west wall open

    if((x[2] == '0')&&(y[0] == '0')){

        return true;
    }
    return false;
}
bool isDown(string x, string y){

    //if there's a south wall open for x, y must have a north wall open

    if((x[3] == '0')&&(y[1] == '0')){
        return true;
    }
    return false;
}
//size of the room that you're finding the path in

int roomSize = 0;
string seenCoords;



int findPath(string modules[n][m], int i, int j){

    if((i < n)&&(j < m)){

        if(isOpen(modules[i][j])){
            //use dashes to keep track of ij coords
            //13-15-23-
            //13 15 and 23 are considered ij coords

            //checking whether square i,j has been seen before
            if(seenCoords.find(to_string(i) + to_string(j) + "-") == string::npos){
                //add the coords to the ones you've already seen
                seenCoords += to_string(i) + to_string(j) + "-";

                roomSize++;

                if(isRight(modules[i][j], modules[i+1][j])){
                    roomSize += findPath(modules, i+1, j);
                }
                if(isDown(modules[i][j], modules[i][j+1])){
                    roomSize += findPath(modules, i, j+1);
                }




            }
        }

    }
    //last char in string is the room size
    return roomSize;



}

int main(){ 

    ifstream fin("castle.in");
    ofstream fout("castle.out");
    fin >> m >> n;
    string modules[n][m];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            int a;
            fin >> a;
            modules[i][j] = walls(a);
        }
    }

    int roomCount, largestRoom = 0;

    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            //if it's not just four walls
            if(isOpen(modules[i][j])){
                //if you haven't seen the square i, j before
                if(seenCoords.find(to_string(i) + to_string(j) + "-") == string::npos){
                    //recurse on the square and find all the paths off it

                    roomSize = findPath(modules, i, j);
                    roomCount++;
                    if(roomSize > largestRoom){
                        largestRoom = seenCoords[seenCoords.length()-1];

                    }
                    roomSize = 0;
                }
            }
            else{
                //if it's not open it's just made of four walls
                roomCount++;
                if(largestRoom < 1){
                    largestRoom = 1;
                }
            }
        }

    }
}
Dijkgraaf
  • 9,324
  • 15
  • 34
  • 48
  • 1
    Possible duplicates: https://www.google.com/search?q=Stackoverflow+c%2B%2B+2d+array+string+passing&ie=utf-8&oe=utf-8 – Thomas Matthews Nov 02 '17 at 23:33
  • Possible references: ["Stackoverflow c++ convert to binary string"](https://www.google.com/search?q=Stackoverflow+c%2B%2B+convert+to+binary+string&ie=utf-8&oe=utf-8) – Thomas Matthews Nov 02 '17 at 23:37
  • You may want to convert your `if-else-if` ladder to `switch` statement, so it matches existing examples better. – Thomas Matthews Nov 02 '17 at 23:38
  • See also: std::bitset. – Thomas Matthews Nov 02 '17 at 23:40
  • Have you located the bug, using a debugger or system output? Start with that. That said, if you come across something like your big block of else-ifs, find a way to make it smaller. In your case, work with the modulo operator. (Also I don't like the way the input file is encoded, but it's not like as if you can do anything about that. But if you ever devise a format on your own, don't go like that.) – Aziuth Nov 03 '17 at 00:49
  • I'm currently getting this error statement: USACOtraining_Castle.cpp:131:27: error: cannot initialize a parameter of type 'string (*)[*]' with an lvalue of type 'string (*)[m]' roomSize += findPath(modules, i, j+1); ^~~~~~~ USACOtraining_Castle.cpp:111:21: note: passing argument to parameter 'modules' here int findPath(string modules[n][m], int i, int j){ ^ – Michael Wong Nov 03 '17 at 02:51
  • Passing 2D arrays to a function: https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – kishore Nov 24 '17 at 04:32

1 Answers1

0

I would recommend passing the pointer to the array by itself. If you do not want to pass the pointer i.e. change your original one, then I would recommend using a 2d vector which will copy itself when passed into a function.

NL628
  • 410
  • 6
  • 20