ARC031 B - 埋め立て
https://atcoder.jp/contests/arc031/tasks/arc031_2
提出
code: python
a = list(input()) for _ in range(10)
print(a)
# oの置き換えはどうするか、100通り試す?
# 島が四つでもokの場合がある
# スタートをどこにするか
# 通過したところをxにして全部xになっていたらOK
解答
code: python
import copy
init_field = list(list(input()) for _ in range(10))
un_stepped_field = list(list(False for _ in range(10)) for _ in range(10))
def count_continent(field):
continent_count = 0
for line in field:
continent_count += line.count("o")
return continent_count
def count_stepped_field(pre_counted_field):
stepped_count = 0
for line in pre_counted_field:
stepped_count += line.count(True)
return stepped_count
def search(y, x):
# マップ外
if x < 0 or x >= 10 or y < 0 or y >= 10:
return
# 海
if fieldyx == "x":
return
# 探索済み
if stepped_fieldyx:
return
stepped_fieldyx = True
# ここで四方に探索
search(y+1, x)
search(y-1, x)
search(y, x+1)
search(y, x-1)
for i in range(10):
for j in range(10):
stepped_field = copy.deepcopy(un_stepped_field)
field = copy.deepcopy(init_field)
# 埋立地作る
if fieldji == "x":
fieldji = "o"
# 探索開始
search(j, i)
# 探索したマスの数が陸地の数と一致すれば題意を満たす
if count_stepped_field(stepped_field) == count_continent(field):
print("YES")
exit()
print("NO")
テーマ
#dfs
蟻本 2-1 Lake Counting
メモ
【Python】ARC 031 B 埋め立て
提出
AC
code: python
import copy
land = list(input()) for _ in range(10)
def resolve(land):
visited = [False * 10 for _ in range(10)]
def dfs(nowi, nowj):
nexti = 0, 0, 1, -1
nextj = 1, -1, 0, 0
for n in range(4):
if nowi + nextin < 0 or nowi + nextin > 9 or nowj + nextjn < 0 or nowj + nextjn > 9 or visited[nowi + nextin][nowj + nextjn] or land[nowi + nextin][nowj + nextjn] == "x":
continue
else:
visited[nowi + nextin][nowj + nextjn] = True
dfs(nowi + nextin, nowj + nextjn)
flag = True
for i in range(10):
if not flag:
break
for j in range(10):
if landij == "o":
flag = False
dfs(i, j)
break
target = 0
res = 0
for i in range(10):
for j in range(10):
if landij == "o":
target += 1
if visitedij:
res += 1
return target == res
for i in range(10):
for j in range(10):
landc = copy.deepcopy(land)
if landcij == "x":
landcij = "o"
if resolve(landc):
print("YES")
exit()
print("NO")