LoginSignup
0
0

More than 5 years have passed since last update.

オフラインリアルタイムどう書くE18の問題をRubyで解く

Posted at

問題はこちら
http://nabetani.sakura.ne.jp/hena/orde18twintri/
処理遅いです。

def make_tri(pat)
  pat =~ /(\d+),(\d+)([RLTB])(\d+)/
  x = $1.to_i
  y = $2.to_i
  dx,dy = {'R'=>[-1,0],'L'=>[1,0],'T'=>[0,1],'B'=>[0,-1]}[$3]
  h = $4.to_i
  tri = [[[x,y]]]
  (h-1).times{|i|
    w = tri[-1].clone
    if dy == 0
      w.unshift([w[0][0],w[0][1]-1])
      w.push([w[-1][0],w[-1][1]+1])
    else
      w.unshift([w[0][0]-1,w[0][1]])
      w.push([w[-1][0]+1,w[-1][1]])
    end
    tri << w.map{|x,y| [x+dx,y+dy]}
  }
  tri.flatten(1)
end

def solve(q)
  a,b = q.split('/')
  a_t = make_tri(a)
  b_t = make_tri(b)
  (a_t&b_t).size
end

DATA.readlines.each do |line|
  no,q,a = line.strip.split(/\s+/)
  ans = solve(q)
  print no + "\t" + ans.to_s
  puts ans == a.to_i ? ' o' : ' x'
end
__END__
0   7,0R6/3,1B5 15  
1   1,6L4/4,9R9 4   
2   0,2R4/1,3B4 3   
3   1,2L4/1,2L5 16  
4   3,2L5/5,6B4 8   
5   4,1B3/6,3B4 4   
6   4,4R7/4,3R5 20  
7   4,5R9/0,7T3 7   
8   4,7T9/1,6T3 1   
9   4,8B7/3,7L4 10  
10  5,3L3/9,8L4 0   
11  5,6B4/4,4R2 3   
12  5,6B4/8,5R4 8   
13  5,8B9/5,2L2 4   
14  6,1L5/7,1T2 3   
15  7,2B4/7,2T4 1   
16  7,3T9/9,6L6 11  
17  8,0R6/8,1R7 30  
18  0,4R7/4,6R10    36  
19  10,4L4/9,1T6    9   
20  2,2T7/6,7T10    4   
21  2,7R4/1,6L8 2   
22  3,0R10/1,2T7    7   
23  3,5T2/3,6B10    2   
24  4,7R10/8,2T8    6   
25  6,8B10/4,5B6    36  
26  9,2B7/1,1B10    6   
27  9,3R14/2,4R1    1   
28  3,0R10/0,6B10   54  
29  4,10T8/4,10T8   64  
30  1,5T10/1,20B10  56  
31  15,16L4/5,12L12 4   
32  12,11T18/7,18R18    34  
33  15,16T14/5,12L15    44  
34  5,10L40/22,22B10    100 
35  46,34T34/34,29T14   30  
36  52,75L12/88,69T54   0   
37  67,83B70/99,48T14   52  
38  291,11T120/258,54B130   424 
39  62,170L139/133,172R21   441 
40  98,189B116/183,127R27   240 
41  646,684B96/435,690R772  0   
42  113,668L866/581,859L852 158404  
43  309,321B162/137,420B423 15750   
44  5474,6459R9089/8177,150R5120    376996  
45  2399,1640B2451/1718,2100L1623   221334  
46  5621,8460T7612/2715,5697L8851   861192  
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0