Python 密码部队:377-A迷宫,为什么这个密码不起作用?

Python 密码部队:377-A迷宫,为什么这个密码不起作用?,python,algorithm,graph-theory,depth-first-search,graph-traversal,Python,Algorithm,Graph Theory,Depth First Search,Graph Traversal,我试图在Codeforces中解决一个问题: 从社论中,我了解到我只需要执行DFS来解决这个问题。以下是确切的词语: 从任何空闲单元启动BFS或DFS。当迷宫连接起来时,这个 搜索将访问所有的空闲单元格。但我们可以在必要时停止搜索 它访问 - 无钾细胞。很明显,这些是 - k细胞是 相互连接。剩余的k细胞可以转化为 墙 我对此的看法是: n, m, k = map(int, input().split()) arr = [list(input()) for _ in range(n)] # T

我试图在Codeforces中解决一个问题:

从社论中,我了解到我只需要执行DFS来解决这个问题。以下是确切的词语:

从任何空闲单元启动BFS或DFS。当迷宫连接起来时,这个 搜索将访问所有的空闲单元格。但我们可以在必要时停止搜索 它访问 - 无钾细胞。很明显,这些是 - k细胞是 相互连接。剩余的k细胞可以转化为 墙

我对此的看法是:

n, m, k = map(int, input().split())
arr = [list(input()) for _ in range(n)]  # The maze

if k == 0:  # if no wall needed, print maze as it is and exit
    for row in arr:
        for element in row:
            print(element, end="")
        print("")
    exit(0)

x, y = -1, -1  # indices to start DFS with, x and y can point to any cell which is '.' (empty)
to_visit = -k  # the number of connected components of graph we need to visit(remaining '.' cells will be marked as 'X')
for i in range(n):
    for j in range(m):
        if arr[i][j] == '.':
            to_visit += 1
            x = i
            y = j


s = [(x, y)]  # a stack for performing DFS
while to_visit > 0 and len(s) > 0:
    i, j = s.pop()
    arr[i][j] = '?'  # make it anything other than '.', '#' and 'X', to mark it visited.
    #                  so we can later remaining '.' to 'X' and change '?' to '.'
    to_visit -= 1
    # top
    if i > 0 and arr[i - 1][j] == '.':
        s.append((i - 1, j))
    # bottom
    if i < n - 1 and arr[i + 1][j] == '.':
        s.append((i + 1, j))
    # left
    if j > 0 and arr[i][j - 1] == '.':
        s.append((i, j - 1))
    # right
    if j < m - 1 and arr[i][j + 1] == '.':
        s.append((i, j + 1))


for row in arr:
    for element in row:
        if element == '?':
            print('.', end="")
        elif element == '.':
            print('X', end="")
        else:
            print(element, end="")
    print("")
n,m,k=map(int,input().split())
arr=[list(input())for uuin range(n)]#迷宫
如果k==0:#如果不需要墙,按原样打印迷宫并退出
对于arr中的行:
对于行中的元素:
打印(元素,结束=“”)
打印(“”)
出口(0)
x、 y=-1,-1#以开始DFS的索引,x和y可以指向任何为“.”的单元格(空)
to_visit=-k#我们需要访问的图的连接组件数(剩余的“.”单元格将标记为“X”)
对于范围(n)中的i:
对于范围内的j(m):
如果arr[i][j]=='.'
访问次数+=1
x=i
y=j
s=[(x,y)]#用于执行DFS的堆栈
在访问>0和len>0时:
i、 j=s.pop()
arr[i][j]=“?”#将其设置为“.”、“#”和“X”以外的任何内容,以标记它已访问。
#因此,我们可以稍后将“.”改为“X”,并将“?”改为“.”
访问-=1
#顶
如果i>0且arr[i-1][j]='.'
s、 附加((i-1,j))
#底部
如果i0且arr[i][j-1]='.'
s、 附加((i,j-1))
#对
如果j
这段代码在codeforces的测试用例10中给出了错误的答案,但由于codeforces网站的性质,我无法访问此测试用例

我一直试图解决这个问题,但还是无法让它被接受

我可以在网站上看到其他人的解决方案,但为什么我的代码不起作用


注意:这不是家庭作业问题,也不是任何当前正在进行的竞赛的一部分。

在堆栈中添加元素(即s)时,应使用不同的符号标记该单元格,以便不能在堆栈中再次添加该单元格。否则,它会导致同一单元格作为空空间的计数超过一次。通过在代码中进行这些更改,所有测试用例都通过了

检查以下代码(已接受):

n,m,k=map(int,input().split())
arr=[list(input())for uuin range(n)]#迷宫
如果k==0:#如果不需要墙,按原样打印迷宫并退出
对于arr中的行:
对于行中的元素:
打印(元素,结束=“”)
打印(“”)
出口(0)
x、 y=-1,-1#以开始DFS的索引,x和y可以指向任何为“.”的单元格(空)
to_visit=-k#我们需要访问的图的连接组件数(剩余的“.”单元格将标记为“X”)
对于范围(n)中的i:
对于范围内的j(m):
如果arr[i][j]=='.'
访问次数+=1
x=i
y=j
s=[(x,y)]#用于执行DFS的堆栈
在访问>0和len>0时:
i、 j=s.pop()
arr[i][j]=“?”#将其设置为“.”、“#”和“X”以外的任何内容,以标记它已访问。
#因此,我们可以稍后将“.”改为“X”,并将“?”改为“.”
访问-=1
#顶
如果i>0且arr[i-1][j]='.'
s、 附加((i-1,j))
arr[i-1][j]='@'
#底部
如果i0且arr[i][j-1]='.'
s、 附加((i,j-1))
arr[i][j-1]='@'
#对
如果j
非常感谢,伙计!我现在看到了。我的手机被重新访问了,to_的访问比应该的时间要早。@RohitRawat,不客气。帮助你是我的荣幸!
n, m, k = map(int, input().split())
arr = [list(input()) for _ in range(n)]  # The maze

if k == 0:  # if no wall needed, print maze as it is and exit
    for row in arr:
        for element in row:
            print(element, end="")
        print("")
    exit(0)

x, y = -1, -1  # indices to start DFS with, x and y can point to any cell which is '.' (empty)
to_visit = -k  # the number of connected components of graph we need to visit(remaining '.' cells will be marked as 'X')
for i in range(n):
    for j in range(m):
        if arr[i][j] == '.':
            to_visit += 1
            x = i
            y = j


s = [(x, y)]  # a stack for performing DFS
while to_visit > 0 and len(s) > 0:
    i, j = s.pop()
    arr[i][j] = '?'  # make it anything other than '.', '#' and 'X', to mark it visited.
    #                  so we can later remaining '.' to 'X' and change '?' to '.'
    to_visit -= 1
    # top
    if i > 0 and arr[i - 1][j] == '.':
        s.append((i - 1, j))
        arr[i-1][j] = '@'
    # bottom
    if i < n - 1 and arr[i + 1][j] == '.':
        s.append((i + 1, j))
        arr[i+1][j] = '@'
    # left
    if j > 0 and arr[i][j - 1] == '.':
        s.append((i, j - 1))
        arr[i][j-1] = '@'
    # right
    if j < m - 1 and arr[i][j + 1] == '.':
        s.append((i, j + 1))
        arr[i][j+1] = '@'


for row in arr:
    for element in row:
        if element == '?':
            print('.', end="")
        elif element == '.' or element == '@':
            print('X', end="")
        else:
            print(element, end="")
    print("")