N, M = gets.split.map(&:to_i) mp = [] mp << "#" + "#" * M + "#" while($_=gets) do $_.chomp! #puts "1_=#{$_}" $_ = "#" + $_ + "#" #puts "2_=#{$_}" mp << $_ end mp << "#" + "#" * M + "#" #puts mp.to_s start_x = nil start_y = nil (1...N+1).each{|n| (1...M+1).each{|m| #puts "mp[#{n}][#{m}]=#{m[n][m]}" if(mp[n][m]=='.') then start_y = n start_x = m; break end } if start_x then break end } cur_x = start_x cur_y = start_y if cur_x == nil puts "NO" exit end directions = [[0,1],[1,0],[0,-1],[-1,0]] cur_d = nil (0...directions.size).each{|d| #puts "pre:d=#{d}, cur_x=#{cur_x}, cur_y=#{cur_y}" #puts "mp[cur_y+#{directions[d][1]}][cur_x+#{directions[d][0]}]" #puts "mp[#{cur_y+directions[d][1]}][#{cur_x+directions[d][0]}]" if(mp[cur_y+directions[d][1]][cur_x+directions[d][0]]=='.')then cur_d = start_d = d cur_x += directions[d][0] cur_y += directions[d][1] break end } if cur_d == nil puts "NO" exit end start2_x = cur_x start2_y = cur_y init = true used_right=false while true # puts "cur_d=#{cur_d}, cur_x=#{cur_x}, cur_y=#{cur_y}" if cur_x == start_x && cur_y == start_y then start_found = true end if cur_x == start2_x && cur_y == start2_y then if ! init mp[cur_y][cur_x] = '*' ok = true (1...N+1).each{|n| (1...M+1).each{|m| # puts "mp[#{n}][#{m}]=#{mp[n][m]}" if(mp[n][m]=='.') ok = false end } } if ok puts "YES" else puts "NO" end exit 0 end init = false else mp[cur_y][cur_x] = '*' end if used_right == false && mp[cur_y+directions[(cur_d+3)%4][1]][cur_x +directions[(cur_d+3)%4][0]] == '.' cur_d = (cur_d+3)%4 used_right = true elsif mp[cur_y+directions[cur_d][1]][cur_x +directions[cur_d][0]] == '.' # elsif mp[cur_y+directions[(cur_d+1)%4][1]][cur_x +directions[(cur_d+1)%4][0]] == '.' cur_d = (cur_d+1)%4 else puts "NO" exit 0 end cur_x += directions[cur_d][0] cur_y += directions[cur_d][1] end