結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー simansiman
提出日時 2016-03-25 03:42:19
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,372 bytes
コンパイル時間 79 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-04-09 23:45:38
合計ジャッジ時間 2,966 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 97 ms
12,416 KB
testcase_02 AC 92 ms
12,288 KB
testcase_03 AC 90 ms
12,160 KB
testcase_04 AC 90 ms
12,288 KB
testcase_05 AC 90 ms
12,160 KB
testcase_06 AC 90 ms
12,160 KB
testcase_07 AC 92 ms
12,160 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 92 ms
12,160 KB
testcase_11 AC 92 ms
12,416 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 91 ms
12,160 KB
testcase_15 AC 90 ms
12,160 KB
testcase_16 WA -
testcase_17 AC 91 ms
12,288 KB
testcase_18 AC 91 ms
12,160 KB
testcase_19 AC 91 ms
12,416 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Yukicoder
  attr_accessor :check_field

  DY = [-1, 0, 0, 1]
  DX = [ 0,-1, 1, 0]

  def initialize
    @field = [
      [ 1, 2, 3, 4],
      [ 5, 6, 7, 8],
      [ 9,10,11,12],
      [13,14,15, 0]
    ]
    @check_field = Array.new(4){ Array.new(4, false) }

    @current = []

    4.times do |y|
      list = gets.chomp.split.map(&:to_i)
      @current << list

      if list.index(0)
        @curY = y
        @curX = list.index(0)
      end
    end

    loop do
      check_field[@curY][@curX] = true

      if check
        puts "Yes"
        break
      end
      unless move
        puts "No"
        break
      end
    end
  end

  def move
    4.times do |i|
      ny = @curY + DY[i]
      nx = @curX + DX[i]

      if is_inside?(ny,nx) && !check_field[ny][nx] && @current[ny][nx] != @field[ny][nx]
        @current[@curY][@curX], @current[ny][nx] = @current[ny][nx], @current[@curY][@curX]
        @curY = ny
        @curX = nx
        return true
      end
    end

    false
  end

  def exist_diff?(y, x)
    4.times do |i|
      ny = y + DY[i]
      nx = x + DX[i]

      if is_inside?(ny,nx) && @current[ny][nx] != @field[ny][nx]
        return true
      end
    end

    false
  end

  def check
    @curY == 3 && @curX == 3 && !exist_diff?(@curY, @curX)
  end

  def is_inside?(y, x)
    0 <= y && y < 4 && 0 <= x && x < 4
  end
end

Yukicoder.new
0