gpt4 book ai didi

海岸长度,kattis

转载 作者:太空宇宙 更新时间:2023-11-04 04:24:46 25 4
gpt4 key购买 nike

我正在尝试解决我在网站 https://open.kattis.com/problems/coast 上发现的一个问题. Tl;dr 版本的问题是,对于给定的景观 map ,我应该打印出海岸线的长度(没有内岛)。

我收到 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.
}

最佳答案

我尝试使用 GCC C++ 编译器 (4.9.2) 编译您的代码。它编译得很好,我使用 the link you provided 中的示例问题对其进行了测试.它吐出正确答案。

但是,当我尝试使用 GCC C 编译器(也是 v 4.9.2)进行编译时,它失败并显示 'for' loop initial declarations are only allowed in C99 or C11 mode,由 this SO question 解释.我认为你的作业是使用 C 编译器评分的,由于这个错误,你的程序编译失败了。

关于海岸长度,kattis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42824154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com