Python 中的康威生命游戏 - 竞争性编程 - 如何优化

发布于 2025-01-10 04:01:05 字数 1529 浏览 0 评论 0 原文

我正在解决 生命游戏问题.com/ieeextreme-practice/task/d12a7784af1a3d3f6d88601c81a4bb81/" rel="nofollow noreferrer">csacademy 并且我无法在更大的投入上击败时间。对优化代码有帮助吗?

我尝试更改一些内容,例如使用 np.array() 而不是 list,并且不将原始输入转换为 1 和 0(原始输入是“*”和“-”,需要以这种方式打印)。

from copy import deepcopy
import numpy as np
aliveToDead = {0, 1, 4, 5, 6, 7, 8}
deadToAlive = {3}


def countNeighbors(M, n, m, i, j):
    s = M[i, (j + 1) % m] + M[i, j - 1] + M[(i + 1) % n, j] + M[i - 1, j]
    s += M[i - 1, j - 1] + M[(i + 1) % n, (j + 1) % m] + M[i - 1, (j + 1) % m] + M[(i + 1) % n, j - 1]
    return s


def gameOfLife(mat, n, m, C):
    cells = deepcopy(mat)
    for c in range(C):
        for i in range(n):
            for j in range(m):
                neighbors = countNeighbors(mat, n, m, i, j)
                if mat[i, j] == 1 and neighbors in aliveToDead:
                    cells[i, j] = 0
                elif mat[i, j] == 0 and neighbors in deadToAlive:
                    cells[i, j] = 1
        mat = deepcopy(cells)
    return mat


def buildList(n):
    return np.array([[0 if x == '-' else 1 for x in input()] for i in range(n)])


def printResult(mat):
    mat = mat.astype(str)
    mat[mat == "1"] = '*'
    mat[mat == "0"] = '-'
    for row in mat:
        print(*row, sep="")


def main():
    n, m, c = map(int, input().split())
    mat = buildList(n)
    result = gameOfLife(mat, n,  m, c)
    printResult(result)


if __name__ == "__main__":
    main()

I am solving Game of Life problem on csacademy and I can't manage to beat the time on larger inputs. Any help on optimizing the code?

I tried changing things, like using np.array() instead of list, and not converting the original input to 1s and 0s (original is '*' and '-', and needs to be printed that way).

from copy import deepcopy
import numpy as np
aliveToDead = {0, 1, 4, 5, 6, 7, 8}
deadToAlive = {3}


def countNeighbors(M, n, m, i, j):
    s = M[i, (j + 1) % m] + M[i, j - 1] + M[(i + 1) % n, j] + M[i - 1, j]
    s += M[i - 1, j - 1] + M[(i + 1) % n, (j + 1) % m] + M[i - 1, (j + 1) % m] + M[(i + 1) % n, j - 1]
    return s


def gameOfLife(mat, n, m, C):
    cells = deepcopy(mat)
    for c in range(C):
        for i in range(n):
            for j in range(m):
                neighbors = countNeighbors(mat, n, m, i, j)
                if mat[i, j] == 1 and neighbors in aliveToDead:
                    cells[i, j] = 0
                elif mat[i, j] == 0 and neighbors in deadToAlive:
                    cells[i, j] = 1
        mat = deepcopy(cells)
    return mat


def buildList(n):
    return np.array([[0 if x == '-' else 1 for x in input()] for i in range(n)])


def printResult(mat):
    mat = mat.astype(str)
    mat[mat == "1"] = '*'
    mat[mat == "0"] = '-'
    for row in mat:
        print(*row, sep="")


def main():
    n, m, c = map(int, input().split())
    mat = buildList(n)
    result = gameOfLife(mat, n,  m, c)
    printResult(result)


if __name__ == "__main__":
    main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦毁影碎の 2025-01-17 04:01:05

这个数学解决方案似乎有助于通过更多测试,但仍有一些失败。

def gameOfLife(mat, n, m, C):
    cells = deepcopy(mat)
    loop = n*m*4*3*5
    while loop % 16:
        loop *= 2
    num_iter = C % loop
    for c in range(num_iter):
        for i in range(n):
            for j in range(m):
                neighbors = countNeighbors(mat, n, m, i, j)
                if mat[i, j] == 1:
                    cells[i, j] = aliveToDead[neighbors]
                else:
                    if neighbors==3:
                        cells[i, j] = 1
                    else:
                        cells[i, j] = 0
        mat,cells = cells,mat
    return mat

This mathematical solution seems to help pass more tests, but there are still a few that fail.

def gameOfLife(mat, n, m, C):
    cells = deepcopy(mat)
    loop = n*m*4*3*5
    while loop % 16:
        loop *= 2
    num_iter = C % loop
    for c in range(num_iter):
        for i in range(n):
            for j in range(m):
                neighbors = countNeighbors(mat, n, m, i, j)
                if mat[i, j] == 1:
                    cells[i, j] = aliveToDead[neighbors]
                else:
                    if neighbors==3:
                        cells[i, j] = 1
                    else:
                        cells[i, j] = 0
        mat,cells = cells,mat
    return mat
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文