GridWalk CodeEval实现问题
GridWalk CodeEval implementation issue
这是我在CodeEval上遇到的一个问题。这是一个我已经解决了4个小时的问题。两年前,我在这里看到了另一篇CodeEval GridWalk Problem的帖子,但它对我没有任何帮助。如果有人遇到过这个问题,请阅读我的代码,因为我不知道我做错了什么。
有一只猴子可以在平面网格上行走。猴子可以一次向左、向右、向上或向下移动一个空间。也就是说,猴子可以从(x,y)到(x+1,y)、(x-1,y),(x,y+1)和(x,y-1)。猴子可以访问x坐标的绝对值的位数之和加上y坐标的绝对值数之和小于或等于19的点。例如,点(59,79)是不可访问的,因为5+9+7+9=30,大于19。另一个例子:点(-5,-7)是可访问的,因为abs(-5)+abs(-7)=5+7=12,小于19。如果从(0,0)开始,猴子可以访问多少点,包括(0,零)本身?输入样本:
此程序没有输入。输出样本:
打印猴子可以访问的点数。它应该打印为整数——例如,如果点数为10,则打印"10",而不是"10.0"或"10.00"等。
以文件(某些文件名)的形式提交您的解决方案。(py2|c|cpp|java|rb|pl|php|tcl|clj|js|scala|cs|m|py3|hs|go|bash|lua)或使用在线编辑器。
#include <stdio.h>
#include <math.h>
int z = 0, counter = 0, x = 0, y = 0, d = -1;
int verified(int x, int y);
int fill();
int main(void){
printf("%d",fill());
return 0;
}
//(x+1, y), (x-1, y), (x, y+1), and (x, y-1)
int fill(){
int i = 0;
while(d<1000000){ //I don't know how to make it so the program doesn't need a
d++; i++; //dimension for scanning. I have to end the loop somehow.
if(verified(x,y)){ //scan each point
//if x and y are accessible or "verified", increase point counter
counter++;
}else{;} // do nothing
if((y%2)!= 0){ //increment each point
y += i;
continue;
}else if ((x%2)!=0){
x += i;
continue;
}else if ((y%2) == 0){
y -= i;
continue;
}else if((x%2) == 0){
x -= i;
continue;
}
}
return counter; //return how many accessible points there are
}
int verified(int x , int y){
int r, digit = 0;
x = abs(x); y = abs(y); //make x and y absolute
z = x * pow(10, (int)log10(y)+1) + y;
//^append both values together(eg. x and y become xy)
while (z > 0) {
r = z % 10;
digit += abs(r); // add all the digits of the sum together
z /= 10;
}
if(digit <=19) //if the sum is less than or equal to 19, it's true
return 1;
else
return 0;
}
我对这个项目的输出是575199。这显然不是书面答复。
首先,您不需要做这样的事情z = x * pow(10, (int)log10(y)+1) + y;
,因为log10
函数的cast(int)总是向下取整,所以会有两种情况,当y是10的pow时,和当y不是时,这显然不适合这一部分。你可以通过一个接一个地计算数字的和来解决这个问题,先对x,然后对y,你不需要把它们组合起来。
第二,如果我没有错,你只能从一个可访问点移动到另一个可访点,所以这一部分又错了:
if((y%2)!= 0){ //increment each point
y += i;
continue;
}else if ((x%2)!=0){
x += i;
continue;
}else if ((y%2) == 0){
y -= i;
continue;
}else if((x%2) == 0){
x -= i;
continue;
}
只是盲目地增加x和y并不能保证点(x,y)是可到达的,应该使用BFS或DFS 来处理这一部分
我不确定下面的答案是否正确(也许你可以测试一下),但它展示了这个想法。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 600
#define LIMIT 19
char grid[SIZE][SIZE];
int sumdigs(int x) {
int sum = 0;
x = x < 0 ? -x : x;
while (x != 0) { sum += x % 10; x /= 10; }
return sum;
}
void walk(int x, int y) {
int xx = x + SIZE / 2;
int yy = y + SIZE / 2;
if (xx < 0 || xx >= SIZE || yy < 0 || yy >= SIZE) {
fprintf(stderr, "You need to make the grid larger.n");
exit(EXIT_FAILURE);
}
if (sumdigs(x) + sumdigs(y) > LIMIT)
return;
if (grid[xx][yy] == 0) {
grid[xx][yy] = 1;
walk(x, y+1);
walk(x, y-1);
walk(x+1, y);
walk(x-1, y);
}
}
int count_accessible_points() {
int cnt = 0;
for(int y=0; y<SIZE; ++y)
for(int x=0; x<SIZE; ++x)
if (grid[x][y]) ++cnt;
return cnt;
}
int main() {
walk(0, 0);
printf("%dn", count_accessible_points());
return 0;
}
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在用于格式4的arm模拟器中实现功能时的一个问题
- C++头文件和类实现出现问题
- 类继承,ENUM 与 AST 类实现的问题
- std::max() 函数与定点实现的比较中的问题
- 尝试从头开始实现Leetcode的FizzBuzz多线程问题。收到"libc++abi.dylib: terminating"错误
- 在我的四叉树实现中遇到问题
- 涉及指针和手动实现的矩阵类的问题
- 有关邻接矩阵实现的问题
- 在 C++ 中实现简单链表时出现问题,没有输出
- 基于 SFINAE 的特征实现问题与函数模板重载
- MCS 锁定实现的问题
- 在C++中实现类似 python "map"函数的问题:调用类成员函数
- 在我自己的堆栈中实现top的问题
- 关于C++从派生类调用在基类中实现的虚拟函数的问题
- C++中链表实现的问题
- c++在实现模板化嵌套类时遇到问题
- 简单的最大除数问题 c++ 实现抛出时间限制已超出
- 十进制到二进制的实现不能完全适用于我大学的检查器。问题或提示可能是什么
- 虚拟 CTOR 的克隆函数实现是否有问题