結果

問題 No.401 数字の渦巻き
ユーザー miraxial
提出日時 2016-07-22 22:59:26
言語 Ruby
(3.4.1)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 571 bytes
コンパイル時間 47 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-11-06 09:14:22
合計ジャッジ時間 3,568 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n=gets.to_i
a=Array.new(n,nil).map{Array.new(n,nil)}
s=[0,0]
d=0
(n**2).times do |i|
  a[s[0]][s[1]]=i+1
  if d==0
    if s[1]+1==n || a[s[0]][s[1]+1]
      d=1
      s[0]+=1
    else
      s[1]+=1
    end
  elsif d==1
    if s[0]+1==n || a[s[0]+1][s[1]]
      d=2
      s[1]-=1
    else
      s[0]+=1
    end
  elsif d==2
    if s[1]-1==-1 || a[s[0]][s[1]-1]
      d=3
      s[0]-=1
    else
      s[1]-=1
    end
  else
    if s[0]-1==-1 || a[s[0]-1][s[1]]
      d=0
      s[1]+=1
    else
      s[0]-=1
    end
  end
end
a.each do |e|
  puts e.map{|i|"%03d"%i}*" "
end
0