結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー Tsuneo YoshiokaTsuneo Yoshioka
提出日時 2015-06-19 23:40:34
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,031 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 11,472 KB
実行使用メモリ 15,368 KB
最終ジャッジ日時 2023-09-21 10:19:27
合計ジャッジ時間 3,280 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
15,236 KB
testcase_01 AC 84 ms
15,052 KB
testcase_02 AC 87 ms
15,204 KB
testcase_03 AC 89 ms
15,052 KB
testcase_04 AC 84 ms
15,196 KB
testcase_05 AC 86 ms
15,280 KB
testcase_06 AC 88 ms
15,144 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 91 ms
15,144 KB
testcase_13 AC 90 ms
15,168 KB
testcase_14 AC 90 ms
15,272 KB
testcase_15 AC 90 ms
15,192 KB
testcase_16 AC 88 ms
15,272 KB
testcase_17 AC 96 ms
15,272 KB
testcase_18 AC 94 ms
15,276 KB
testcase_19 AC 91 ms
15,232 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

@target=STDIN.read.split("\n").map{|l| l.split.map(&:to_i);}
cur_y=nil,cur_x=nil
(0...4).each{|r|
    (0...4).each{|c|
        if @target[r][c]==0
            cur_y = r
            cur_x = c
        end
    }
}
@ma=Array.new(4).map{Array.new(4)}
(0..15).each{|i|
    @ma[i/4][i%4]=i+1
}
@ma[3][3]=0

@used=Array.new(16,false)
def go(cur_y, cur_x)
    #puts "cur_y=#{cur_y}, cur_x=#{cur_x}"
    #puts "ma=#{@ma}, target=#{@target}"
    if @ma==@target
        puts "Yes"
        exit 0
    end
    dx=[0,1,0,-1];
    dy=[1,0,-1,0];
    (0...4).each{|i|
        y2=cur_y+dy[i]
        x2=cur_x+dx[i]
        if(y2<0 || y2>3)
            next
        end
        if(x2<0 || x2>3)
            next
        end
        if(@used[@ma[y2][x2]]==true)
            next
        end
        @used[@ma[y2][x2]]=true
        @ma[cur_y][cur_x],@ma[y2][x2] = @ma[y2][x2],@ma[cur_y][cur_x]
        go(y2,x2)
        @ma[cur_y][cur_x],@ma[y2][x2] = @ma[y2][x2],@ma[cur_y][cur_x]
        @used[@ma[y2][x2]]=false
    }
end
go(cur_y,cur_x)
puts "No"
0