Skip to content

Commit

Permalink
ABC082 D問題
Browse files Browse the repository at this point in the history
  • Loading branch information
Linus-MK committed Oct 6, 2020
1 parent 0e35c7b commit 8965e87
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
56 changes: 56 additions & 0 deletions ABC_076-125/abc082d.py
@@ -0,0 +1,56 @@
#
# セグメント木の問題(蟻本p.156) と少し似ていたので、
# やや分かりにくいが、これは縦横分離できる問題である。
# ある地点までのTの個数の偶奇を見ると、進むのがx軸(横)方向かy軸(縦)方向かが確定する。
# 最初は正方向で、それ以外のFの塊は正負を自由に選べる。
# あとは縦横独立に、目標の座標に到達できるかを考えれば良い。
# 全通りを総当たりで考える→×(TLE)
# DP。直前の座標からa進むかa戻るかの動きをしたものが、次の到達可能点になる。

# PyPyでACにはなるが、appendで配列を順次追加するのはTLEになりやすいので避けるべき。最初に2次元配列を作るのが良い

s = input()
s = s + '-' # ループ最後の処理を簡単にするための番兵
forward = s.count('F')

x = [[False] * (forward*2+1)]
y = [[False] * (forward*2+1)]

x[0][forward] = True
y[0][forward] = True

direction = 0 # 0がx、1がy
count_len = 0
first = True # 最初はx軸正の方向
for char in s:
if char == 'F':
count_len += 1
else:
if first:
x.append([False] * (forward*2+1))
x[-1][forward + count_len] = True
first = False

elif direction == 0:
x.append([False] * (forward*2+1))
for pos in range(forward*2+1):
if (0 <= pos-count_len and x[-2][pos-count_len] == True) or \
(pos+count_len <= forward*2 and x[-2][pos+count_len] == True):
x[-1][pos] = True
else:
y.append([False] * (forward*2+1))
for pos in range(forward*2+1):
if (0 <= pos-count_len and y[-2][pos-count_len] == True) or \
(pos+count_len <= forward*2 and y[-2][pos+count_len] == True):
y[-1][pos] = True

if char == 'T':
direction = 1 - direction # 方向転換

count_len = 0

xx, yy = list(map(int, input().split()))
if 0 <=forward+xx <= forward*2 and x[-1][forward+xx] and 0 <=forward+yy <= forward*2 and y[-1][forward+yy]:
print('Yes')
else:
print('No')
6 changes: 6 additions & 0 deletions lesson.md
Expand Up @@ -126,3 +126,9 @@ ABC119 C問題 Synthetic Kadomatsu
以下の構造を各操作の性質から見抜くのが必要。あとは「あからさまに少ない数値が制約条件に登場しているので全探索で行けないか疑う」こと。
複数の操作を好きな順で好きな回数実行できる
→操作Xを全部やって、その後操作Bをやる、と考えても一般性を失わない

---

2020年10月6日
縦横分離できる、x軸方向とy軸方向を独立に考えられる問題
* ABC082 D問題 FT Robot

0 comments on commit 8965e87

Please sign in to comment.