简体   繁体   中英

Trying to pass a 2D array of strings into a function

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;
                }
            }
        }

    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM