-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
executable file
·147 lines (129 loc) · 3.17 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def getN():
return int(input())
def getNM():
return map(int, input().split())
def getList():
return list(map(int, input().split()))
def getArray(intn):
return [int(input()) for i in range(intn)]
def input():
return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
ns = []
while len(ns) < rantime:
n = random.randint(ran1, ran2)
if not n in ns:
ns.append(n)
return sorted(ns)
def rand_query(ran1, ran2, rantime):
r_query = []
while len(r_query) < rantime:
n_q = rand_ints_nodup(ran1, ran2, 2)
if not n_q in r_query:
r_query.append(n_q)
return sorted(r_query)
from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right
import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7
#############
# Main Code #
#############
# ABC037 D - 経路
H, W = getNM()
maze = []
for i in range(H):
m = getList()
maze.append(m)
memo = [[-1] * W for i in range(H)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
# ぐるぐる回るやつはdfs
def dfs(x, y):
if memo[y][x] != -1:
return memo[y][x]
res = 1
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < W and 0 <= ny < H and maze[ny][nx] > maze[y][x]:
res += dfs(nx, ny)
res %= mod
memo[y][x] = res
return res
ans = 0
for i in range(H):
for j in range(W):
ans += dfs(j, i)
ans %= mod
print(ans % mod)
maze = [
[9,9,9,9,9,9,9,9,9,9,9,9],
[9,0,0,0,9,0,0,0,0,0,0,9],
[9,0,9,0,0,0,9,9,0,9,9,9],
[9,0,9,9,0,9,0,0,0,9,0,9],
[9,0,0,0,9,0,0,9,9,0,9,9],
[9,9,9,0,0,9,0,9,0,0,0,9],
[9,0,0,0,9,0,9,0,0,9,1,9],
[9,0,9,0,0,0,0,9,0,0,9,9],
[9,0,0,9,0,9,0,0,9,0,0,9],
[9,0,9,0,9,0,9,0,0,9,0,9],
[9,0,0,0,0,0,0,9,0,0,0,9],
[9,9,9,9,9,9,9,9,9,9,9,9]
]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
ans = 100000
def dfs(x, y, depth):
global ans
if maze[y][x] == 1:
ans = min(ans, depth)
maze[y][x] = 2
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if maze[ny][nx] < 2:
dfs(nx, ny, depth + 1)
maze[y][x] = 0
dfs(1, 1, 0)
print(ans)
m = int(input())
n = int(input())
maze = []
for i in range(n):
maze.append(list(map(int, input().split())))
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
ans = 0
# 最長距離を求める
def dfs(x, y, depth):
global ans
maze[y][x] = 0
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < m and 0 <= ny < n and maze[ny][nx] == 1:
dfs(nx, ny, depth + 1)
maze[y][x] = 1
ans = max(ans, depth)
for i in range(m):
for j in range(n):
if maze[j][i] == 1:
dfs(i, j, 1)
# TLEにならないんですかこれ
print(ans)