2017-03-16 63 views
1

我想解決一個問題,我在網站https://open.kattis.com/problems/coast上找到。 Tl; dr版本的問題是,對於給定的景觀地圖,我應該打印出海岸線的長度(沒有內島)。海岸長度,kattis

我收到0/26分,但我不知道爲什麼,我測試過了,而且據我檢查,它工作。我認爲它不能編譯,但如果是這樣的話,爲什麼?它爲我編譯完美。

#include <stdio.h> 


int edgeCount(int, int, char*); 
int topToBottomCount(int, int, char*); 
int leftToRightCount(int, int, char*); 
int removingInsides(int, int, char*); 

int main() 
{ 
    int n = 0; // number of strings 
    int m = 0; // strings lenghts 
    //printf("Enter N(number of strings) x M(strings lenght): "); 
    scanf("%d", &n); 
    scanf("%d", &m); 

    char coast[1024]; 
    for(int i = 0; i < n; i++){ 
     scanf("%s", coast+i*m); // adding strings to char coast[1024], making array of ones and zeroes // e.g we are adding 3x4 strings - 111100001111 
    }                         // it can also be looked as 1111 
                                  //  0000  - matrix 
    int coasts = edgeCount(n, m, coast);                     //  1111 
    coasts += topToBottomCount(n, m, coast); 
    coasts += leftToRightCount(n, m, coast); 
    coasts -= removingInsides(n, m, coast); 

    printf("%d - coasts\n", coasts); 

    return 0; 
} 

int edgeCount(int n, int m, char *coast){ // if 1 is placed at the edge of the "map", it is 1 coast (2 if it is at corner) 
    int edgeCoast = 0; 

    for(int i = 0; i < m; i++){  // top edges 
     if(coast[i] == '1') 
      edgeCoast++; 
    } 

    for(int i = m*n - m; i < m*n; i++){ // bottom edges (m*n - m = first char in the last string, it can be also looked as the last row in matrix) 
     if(coast[i] == '1') 
      edgeCoast++; 
    } 

    for(int i = 0; i <m*n; i+=m){ // left side edges (first column in matrix) 
     if(coast[i] == '1') 
      edgeCoast++; 
    } 

    for(int i = m-1; i < m*n; i+=m){ // right side edges (last column in matrix) 
     if(coast[i] == '1') 
      edgeCoast++; 
    } 

    return edgeCoast; 
} 

int topToBottomCount(int n, int m, char *coast){ 
    int coasts = 0; 
    for(int i = 0; i < m*n - m; i++){ // we start from first char in "matrix", and move to the (m*n - m = 2nd last "row") 
     if(coast[i]^coast[i+m]) // we are checking if zero is placed above one or via versa 
      coasts++; 
    } 

    return coasts; 
} 

int leftToRightCount(int n, int m, char* coast){ 
    int coasts = 0; 
    int p = m-1; 
    for(int i = 0; i < n*m; i++){  // we start from the first charr, and we are going trough whole matrix, but the last column 
     if(i == p){      // p = m - 1 (last char in first row) 
      p+=m;      // p+=m (last char in next column, and so on) 
      continue;     // we move to next iteration 
     } 

     if(i == m*n - 1)    //if we are at last char in matrix, we break out from loop 
      break; 

     if(coast[i]^coast[i+1]) 
      coasts++; 
    } 

    return coasts; 
} 

int removingInsides(int n, int m, char* coast){ // Lakes and islands in lakes are not contributing to the sea coast. we are checking if they exist. 
    int innerCoasts = 0; 
    for(int i = m + 1; i < n*m - m - 1; i ++){ 
     if(coast[i] == '0' && coast[i]^coast[i-1] && coast[i]^coast[i+1] && coast[i]^coast[i-m] && coast[i]^coast[i+m]) // char has to be 0, and to hist left, right, above and under there has to be 1 
      innerCoasts++; 
    } 

    return innerCoasts * 4; // *4 because we added 4 coasts before for each island. 
} 
+0

這是C代碼,而不是C++。請勿標記垃圾郵件。只使用實際應用於您帖子的標籤,而不要只添加看似相似的隨機標籤。這裏的標籤具有特定的含義並且相關。如果您不知道自己編碼的語言是什麼,請離開鍵盤直到找出它。 –

+0

不會再發生 – Nosorog

+0

'char coast [1024];'不能滿足全部要求:'1≤N,M≤1000'。例如,N = 3,M = 500會炸燬你的代碼。而且你沒有考慮每個字符串中的NUL終止符。 – kaylum

回答

0

我試着編譯你的代碼,使用GCC C++編譯器(4.9.2)。它編譯得很好,我使用the link you provided中的示例問題對它進行了測試。它吐出了正確的答案。

然而,當我嘗試使用GCC Ç編譯器(也v 4.9.2)編譯,它失敗'for' loop initial declarations are only allowed in C99 or C11 mode,這是由this SO question說明。我認爲你的任務是使用C編譯器進行分級的,而且由於這個錯誤你的程序編譯失敗了。

+0

是的,我已經糾正了foor循環,它現在有效,但removeInsides函數似乎是現在的概率,當我相互之間有兩個或兩個以上的島嶼,但我會管理,我猜。 感謝您的評論 – Nosorog

相關問題