2017-11-02 30 views
0

私はUSACOの問題に取り組んでいますが、私のコードでこのバグを乗り越えることはできません。どうやら、関数の最初の引数の変換はfindPathにとって間違っています。私は文字列の2次元配列への参照を関数に再帰的に渡そうとしています。アドバイスをいただければ幸いです。文字列の2次元配列を関数に渡そう

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

    } 
} 
+1

可能な重複:https://www.google.com/search?q=Stackoverflow+c%2B%2B+2d+array+string+passing&ie=utf-8&oe=utf-8 –

+0

参照:["Stackoverflow C++バイナリ文字列に変換する "](https://www.google.com/search?q=Stackoverflow+c%2B%2B+convert+to+binary+string&ie=utf-8&oe=utf-8) –

+0

'if-else-if'ラダーを' switch'ステートメントに変換するので、既存の例とよく似ています。 –

答えて

0

ポインタを配列に渡すことをお勧めします。ポインタを渡したくない、つまりオリジナルのポインタを変更したくない場合は、関数に渡すときに自身をコピーする2dベクトルを使用することをお勧めします。

関連する問題