题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let index = 0 let input =[] rl.on('line', function (line) { soluiton(line) }); function soluiton(line:string){ let moves = line.split(";") f(0,0,0,moves) } var pat =/^[AWSD][0-9]+$/ function f(x,y,index,moves){ if(index>moves.length){ console.log([x,y].join(",")) return } let move = moves[index] if(!pat.test(move)){ return f(x,y,index+1,moves) } if(move[0]==="A"){ let distance =Number(move.slice(1)) return f(x-distance,y,index+1,moves) } if(move[0]==="D"){ let distance =Number(move.slice(1)) return f(x+distance,y,index+1,moves) } if(move[0]==="W"){ let distance =Number(move.slice(1)) return f(x,y+distance,index+1,moves) } if(move[0]==="S"){ let distance =Number(move.slice(1)) return f(x,y-distance,index+1,moves) } }#递归#