結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー Tsuneo YoshiokaTsuneo Yoshioka
提出日時 2015-06-20 00:27:11
言語 Ruby
(3.3.0)
結果
AC  
実行時間 84 ms / 5,000 ms
コード長 1,004 bytes
コンパイル時間 34 ms
コンパイル使用メモリ 11,480 KB
実行使用メモリ 15,356 KB
最終ジャッジ日時 2023-09-21 10:24:35
合計ジャッジ時間 2,583 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,136 KB
testcase_01 AC 79 ms
15,248 KB
testcase_02 AC 79 ms
15,152 KB
testcase_03 AC 81 ms
15,168 KB
testcase_04 AC 79 ms
15,356 KB
testcase_05 AC 76 ms
15,160 KB
testcase_06 AC 84 ms
15,300 KB
testcase_07 AC 76 ms
15,160 KB
testcase_08 AC 80 ms
15,036 KB
testcase_09 AC 77 ms
15,124 KB
testcase_10 AC 78 ms
15,156 KB
testcase_11 AC 78 ms
15,336 KB
testcase_12 AC 81 ms
15,180 KB
testcase_13 AC 81 ms
15,180 KB
testcase_14 AC 81 ms
15,100 KB
testcase_15 AC 80 ms
15,272 KB
testcase_16 AC 80 ms
15,188 KB
testcase_17 AC 81 ms
15,096 KB
testcase_18 AC 82 ms
15,156 KB
testcase_19 AC 81 ms
15,100 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:2: warning: assigned but unused variable - cur_y
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}"
    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(3,3)
puts "No"
0