-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA11.js
More file actions
80 lines (70 loc) · 1.79 KB
/
A11.js
File metadata and controls
80 lines (70 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const fs = require('fs');
const path = require('path');
const input = fs.readFileSync(path.join(__dirname,'/A11.txt')).toString().trim().split("\n");
//console.log(input);
const n = +input.shift();
const k = +input.shift();
let graph = Array.from(Array(n+1),()=>Array(n+1).fill(0));
for(let i=0; i<k;i++){
let tmp = input.shift().split(" ");
let a = +tmp[0];
let b = +tmp[1];
graph[a][b] = 1;
}
const l = +input.shift();
let info = [];
for(let i=0; i<l;i++){
let tmp = input.shift().split(" ");
info.push([+tmp[0].trim(),tmp[1].trim()]);
}
console.log(graph);
console.log(info);
let dx = [0,1,0,-1];
let dy = [1,0,-1,0];
function rotate(direction,c){
if(c==="L"){
direction = (direction-1)%4;
}else{
direction = (direction+1)%4;
}
return direction;
}
function simulte(){
let x =1;
let y = 1;
let direction = 0;
let time = 0;
let index = 0;
let q = [[x,y]];
graph[x][y] = 2;
while(true){
let nx = dx[direction]+x;
let ny = dy[direction]+y;
if(nx>=1 && nx<=n && ny>=1 && ny<=n && graph[nx][ny]!==2){
if(graph[nx][ny]===0){
graph[nx][ny] = 2;
q.push([nx,ny]);
let tmp = q.shift();
let px = tmp[0];
let py = tmp[1];
graph[px][py] = 0;
}
if(graph[nx][ny]===1){
graph[nx][ny] = 2;
q.push([nx,ny]);
}
}else{
time++;
break;
}
x = nx;
y = ny;
time++;
if(index<l && time === info[index][0]){
direction = rotate(direction,info[index][1]);
index++;
}
}
return time;
}
console.log(simulte());