- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试解决我在网站 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/
我正在尝试解决我在网站 https://open.kattis.com/problems/coast 上发现的一个问题. Tl;dr 版本的问题是,对于给定的景观 map ,我应该打印出海岸线的长度(
我正在尝试解决这个问题( https://open.kattis.com/problems/anotherbrick )。 当我提交最终代码时,我不断收到运行时错误。我不确定我的代码有什么问题。 #
我是编程新手。我正在学习 XOR 以尝试在 Kattis 上解决类作业问题的 oddmanout 问题。 我正在尝试的是在一系列数字中找到奇怪的人。除一个号码外,每个号码都有一对。我必须找到没有的号码
This是我指的问题。快速总结: 输入:一个整数时间T;银行关闭的时间(以分钟为单位)和一组 c 和 t 表示此人携带的现金数量(整数)和从现在开始的时间(以分钟为单位)如果没有送达,此人将离开。服务
我目前正在 Kattis 做一些测试,但我被卡住了 with this one .到目前为止,我编写的代码在 Visual Studio 代码中使用 console.logging 时为我提供了最后一
我是一名 PHP 初学者,我正在尝试解决 Modulo Kattis 问题,当我在终端中测试我的代码时,它运行良好,但当我提交我的解决方案时,我得到“错误答案”。 问题(https://open.ka
请注意,此问题是作业。 通过下面的代码,我在Kattis网站上输入了这个问题的数据。代码可以编译,但是,我在“聚会人数#:”printf 之后出现段错误,我不确定为什么。请帮忙! 在 Kattis 中
我正在尝试解决描述的设备问题here .我有一个解决方案,但它需要超过 2 秒的时间限制。我试图优化我的代码以提高速度,但无法在 2 秒的限制内完成。 import sys import math f
我一直在尝试解决 Kattis 上的最小生成树问题。 ( https://open.kattis.com/problems/minspantree ) 第一个测试运行良好,第二个给出未指定的运行时错误
我收到了一些编程任务,需要完成这些任务才能进入工作面试的下一步。让他们都接受期待一个,描述:https://academicwork.kattis.com/problems/pebblesolitai
在试图找到阶乘 val 的最后一位的 Main 类中,为什么 public static void main(String[] args) { int testcases = sc.n
问题如下: Dick is d=12 years old. When we say this, we mean that it is at least twelve and not yet thirt
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我是一名优秀的程序员,十分优秀!