結果

問題 No.1434 Make Maze
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-06-08 16:16:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,771 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 91,776 KB
最終ジャッジ日時 2024-05-05 02:08:13
合計ジャッジ時間 6,676 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,120 KB
testcase_01 AC 44 ms
53,120 KB
testcase_02 AC 136 ms
91,776 KB
testcase_03 AC 138 ms
91,776 KB
testcase_04 AC 44 ms
53,376 KB
testcase_05 AC 43 ms
52,736 KB
testcase_06 AC 43 ms
53,248 KB
testcase_07 AC 43 ms
53,248 KB
testcase_08 AC 43 ms
53,248 KB
testcase_09 WA -
testcase_10 AC 43 ms
52,992 KB
testcase_11 AC 44 ms
53,248 KB
testcase_12 AC 69 ms
68,352 KB
testcase_13 AC 45 ms
54,144 KB
testcase_14 AC 69 ms
67,072 KB
testcase_15 AC 83 ms
73,472 KB
testcase_16 AC 72 ms
68,864 KB
testcase_17 AC 71 ms
67,968 KB
testcase_18 AC 78 ms
73,344 KB
testcase_19 AC 106 ms
83,840 KB
testcase_20 AC 44 ms
53,248 KB
testcase_21 AC 43 ms
52,864 KB
testcase_22 AC 43 ms
52,992 KB
testcase_23 AC 44 ms
53,120 KB
testcase_24 AC 99 ms
82,176 KB
testcase_25 AC 113 ms
86,528 KB
testcase_26 AC 73 ms
69,120 KB
testcase_27 AC 109 ms
81,536 KB
testcase_28 AC 72 ms
68,608 KB
testcase_29 AC 59 ms
63,872 KB
testcase_30 AC 78 ms
73,088 KB
testcase_31 AC 83 ms
74,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

h,w,x=map(int,input().split())
min_x=h+w-2
max_x=min_x+max((h-1)*(w//4)*2,(w-1)*(h//4)*2)
max_x_option = (h//2+1)*(w//2+1)
if (h//2+1)%2==0 and (h//2+1)%2==0:
    max_x_option-=1
max_x_option -= 1
max_x_option *= 2
#print(min_x,max_x,max_x_option)
if min_x <= x <= max_x and (x-min_x)%4==0:
  mat=[["#"]*w for _ in range(h)]
  for i in range(0,h,2):
    for j in range(0,w,2):
      mat[i][j]="x"
  if (h-1)*(w//4)*2>(w-1)*(h//4)*2:
    # 上下に振りながら右に行く
    # 壁にぶつかるまでは下または上に行く。ぶつかったときのみ右に行く
    seen=[[-1]*w for _ in range(h)]
    seen[0][0]=0
    for i in range(h):
      seen[i][0]=i
      mat[i][0]="."
    todo=[[i,0]]
    while todo:
      i,j=todo.pop()
      mat[i][j]="."
      if [i,j]==[h-1,w-1]:break
      if seen[i][j]+(h-i-1)+(w-j-1)==x:
        # ここからは最短距離を歩く
        # 右に行けるだけ右に行く
        while [i,j]!=[h-1,w-1]:
          if j+1<w:
            j+=1
          else:
            i+=1
          mat[i][j]="."
        break
      elif i+1<h and seen[i+1][j]==-1:
        # 下にいく
        seen[i+1][j]=seen[i][j]+1
        todo.append([i+1,j])
      elif 0<=i-1 and seen[i-1][j]==-1:
        # 上にいく
        seen[i-1][j]=seen[i][j]+1
        todo.append([i-1,j])
      else:
        # 右に行く
        seen[i][j+1]=seen[i][j]+1
        mat[i][j+1]="."
        seen[i][j+2]=seen[i][j]+2
        todo.append([i,j+2])
    for i in range(h):
      for j in range(w):
        if mat[i][j]=="x":
          mat[i][j]="."
          if i+1<h:mat[i+1][j]="."
          if 0<=i-1:mat[i-1][j]="."
  else:
    # 左右に振りながら上に行く
    # 壁にぶつかるまでは右または左に行く。ぶつかったときのみ下に行く
    seen=[[-1]*w for _ in range(h)]
    seen[0][0]=0
    for i in range(w):
      seen[0][i]=i
      mat[0][i]="."
    todo=[[0,i]]

    while todo:
      i,j=todo.pop()
      mat[i][j]="."
      if [i,j]==[h-1,w-1]:break
      if seen[i][j]+(h-i-1)+(w-j-1)==x:
        # ここからは最短距離を歩く
        # 下に行けるだけ下に行く
        while [i,j]!=[h-1,w-1]:
          if i+1<h:
            i+=1
          else:
            j+=1
          mat[i][j]="."
        break
      elif j+1<w and seen[i][j+1]==-1:
        # 右にいく
        seen[i][j+1]=seen[i][j]+1
        todo.append([i,j+1])
      elif 0<=j-1 and seen[i][j-1]==-1:
        # 左にいく
        seen[i][j-1]=seen[i][j]+1
        todo.append([i,j-1])
      else:
        # 下に行く
        seen[i+1][j]=seen[i][j]+1
        mat[i+1][j]="."
        seen[i+2][j]=seen[i][j]+2
        todo.append([i+2,j])
    for i in range(h):
      for j in range(w):
        pass
        if mat[i][j]=="x":
          mat[i][j]="."
          if j+1<w:mat[i][j+1]="."
          if 0<=j-1:mat[i][j-1]="."
  for x in mat:
    print("".join(x))
elif max_x < x <= max_x_option:
  mat=[["#"]*w for _ in range(h)]
  for i in range(0,h,2):
    for j in range(0,w,2):
      mat[i][j]="x"
  # ジグザグで下に行き、上に上がる。
  # 上下に振りながら右に行く
  # 壁にぶつかるまでは下または上に行く。ぶつかったときのみ右に行く
  seen=[[-1]*w for _ in range(h)]
  seen[0][0]=0
  for i in range(h//2):
    if i&1:
      seen[i*2][2]=i*3
      seen[i*2][1]=i*3+1
      seen[i*2][0]=i*3+2
      seen[i*2+1][0]=i*3+2
      mat[i*2+1][0]="."
    else:
      seen[i*2][0]=i*3
      seen[i*2][1]=i*3+1
      seen[i*2][2]=i*3+2
      seen[i*2+1][2]=i*3+2
      mat[i*2+1][2]="."
    mat[i*2][2]="."
    mat[i*2][1]="."
    mat[i*2][0]="."
  seen[h-1][2]=seen[h-2][2]+1
  seen[h-1][3]=seen[h-1][2]+1
  seen[h-1][4]=seen[h-1][3]+1
  mat[h-1][0]="."
  mat[h-1][1]="."
  mat[h-1][2]="."
  mat[h-1][3]="."
  mat[h-1][4]="."
  todo=[[h-1,4]]
  while todo:
    i,j=todo.pop()
    mat[i][j]="."
    if [i,j]==[h-1,w-1]:break
    if seen[i][j]+(h-i-1)+(w-j-1)==x:
      # ここからは最短距離を歩く
      # 右に行けるだけ右に行く
      while [i,j]!=[h-1,w-1]:
        if j+1<w:
          j+=1
        else:
          i+=1
        mat[i][j]="."
      break
    elif i+1<h and seen[i+1][j]==-1:
      # 下にいく
      seen[i+1][j]=seen[i][j]+1
      todo.append([i+1,j])
    elif 0<=i-1 and seen[i-1][j]==-1:
      # 上にいく
      seen[i-1][j]=seen[i][j]+1
      todo.append([i-1,j])
    else:
      # 右に行く
      seen[i][j+1]=seen[i][j]+1
      mat[i][j+1]="."
      seen[i][j+2]=seen[i][j]+2
      todo.append([i,j+2])
  for i in range(h):
    for j in range(w):
      if mat[i][j]=="x":
        mat[i][j]="."
        if i+1<h:mat[i+1][j]="."
        if 0<=i-1:mat[i-1][j]="."
  for x in mat:
    print("".join(x))
else:
  print(-1)
0