0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ABC385Bをjavascriptで解いた

Posted at

今日の問題

↑押してください。

自分の回答(javascript)

function Main(input){
  min = 1e18
  max = -1e18
  let abc = "abcdefghijklmnopqrstuvwxyz".split("")
  let ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")
  let f = 3
  if(f == 0){
    input = parseInt(input.trim())
  }
  if(f == 1){
    input = input.trim().split("\n").map((a)=>parseInt(a))
  }
  if(f == 2){
    input = input.trim().split("\n").map((a)=>a.split(" ").map((b)=>parseInt(b)))
  }
  if(f == 3){
    input = input.trim().split("\n")
  }
  //ここより上は定型文です。
  let [h,w,x,y] = input.shift().split(" ").map((a)=>parseInt(a))
  let t = input.pop().split("")
  s = input.map((a)=>a.split(""))
  x--
  y--
  map = new Map()
  map.set("U",[-1,0])
  map.set("D",[1,0])
  map.set("L",[0,-1])
  map.set("R",[0,1])
  set = new Set()
  count = 0
  for(let i = 0;i<t.length;i++){
    //console.log(map.get(s[i]),s[i])
    //console.log(x+1,y+1)
    ny = y + map.get(t[i])[1]
    nx = x + map.get(t[i])[0]
    if(0<=ny&&ny<w&&0<=nx&&nx<h&&s[nx][ny] != "#"){
      y = ny
      x = nx
      if(!set.has(ny+" "+nx)&&s[nx][ny] == "@"){
        count ++
        set.add(ny+" "+nx)
      }
    }
  }
  x ++
  y ++
  console.log(x,y,count)
}
Main(require("fs").readFileSync(0, "utf8"));

工夫した点

一度訪れたマスはsetで管理して2重でカウントしないようにしています。いつもとxとyが逆であるため注意が必要です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?