Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
jenningsloy318 merged 4 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Aug 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: python solution, DFS
  • Loading branch information
sunlight0602 committed Aug 3, 2024
commit 0d7f8b03150a2114946192ef181fa8e00d5c5bf8
56 changes: 56 additions & 0 deletions problems/kamacoder/0103.水流问题.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,62 @@ public class Main {
```

### Python
```Python
first = set()
second = set()
directions = [[-1, 0], [0, 1], [1, 0], [0, -1]]

def dfs(i, j, graph, visited, side):
if visited[i][j]:
return

visited[i][j] = True
side.add((i, j))

for x, y in directions:
new_x = i + x
new_y = j + y
if (
0 <= new_x < len(graph)
and 0 <= new_y < len(graph[0])
and int(graph[new_x][new_y]) >= int(graph[i][j])
):
dfs(new_x, new_y, graph, visited, side)

def main():
global first
global second

N, M = map(int, input().strip().split())
graph = []
for _ in range(N):
row = input().strip().split()
graph.append(row)

# 是否可到达第一边界
visited = [[False] * M for _ in range(N)]
for i in range(M):
dfs(0, i, graph, visited, first)
for i in range(N):
dfs(i, 0, graph, visited, first)

# 是否可到达第二边界
visited = [[False] * M for _ in range(N)]
for i in range(M):
dfs(N - 1, i, graph, visited, second)
for i in range(N):
dfs(i, M - 1, graph, visited, second)

# 可到达第一边界和第二边界
res = first & second

for x, y in res:
print(f"{x} {y}")


if __name__ == "__main__":
main()
```

### Go

Expand Down

AltStyle によって変換されたページ (->オリジナル) /