2017-03-16 66 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

相关问题