首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写重复一段代码的函数,而不调用相同的函数

问如何编写重复一段代码的函数,而不调用相同的函数
EN

Stack Overflow用户
提问于 2019-05-12 03:28:51
回答 1查看 40关注 0票数 0

我正在为一个学校项目用Java编写扫雷程序。我有一段代码可以工作,但速度很慢。该函数应该打开空单元格旁边的单元格(常规扫雷程序中的函数在单元格附近打开)。问题是,这个函数必须自己打开才能工作。我希望有人能帮助我。

代码语言:javascript
复制
void floodFill() {
        revealed = true; // Function is started and cell is revealed
        // This block has to play through for every empty field
        if (nr == 0) { //Checks if empty cell
            if (revealed) { //Checks if already opened, if not, it will just draw it
                for (int xoff = -1; xoff <= 1; xoff++) { //Checks surroundings
                    int celli = i + xoff;
                    if (celli < 0 || celli >= row)
                        continue;
                    for (int yoff = -1; yoff <= 1; yoff++) { //Checks surroundings
                        int cellj = j + yoff;
                        if (cellj < 0 || cellj >= col)
                            continue;
                        if (!grid[celli][cellj].revealed) {
                            grid[celli][cellj].revealed = true;
                            //Here it should play the block through with the new values
                            //Should be like "grid[celli][cellj].floodFill();",
                            //but should not open the function again
                        }
                    }
                }
            }
        }
        draw(); //Draws everything in a frame
    }

将会非常感谢你的帮助,提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-05-12 04:02:56

我猜您可以在每次显示一个空的磁贴时调用相同的方法,并递归地创建您的方法。你应该不会有运行时的问题,因为(如果我错了,请纠正我)你的T(n)将小于O(3^n),并且tiles的数量(可能)不会太多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56093599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档