-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.js
190 lines (178 loc) · 7.08 KB
/
AI.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// test AI program.
class AI {
isInside(x, y, W, H) {
return 0<=x && x<W && 0<=y && y<H;
}
getDist(x1, y1, x2, y2) {
return Math.abs(x1-x2) + Math.abs(y1-y2);
}
iwashiMove(iwashiMap, player, maps, H, W) {
const dx = [0, 1, 0, -1, 0];
const dy = [-1, 0, 1, 0, 0];
let nextIwashiMap = new Array(H);
for (let i = 0; i < H; i++) {
nextIwashiMap[i] = new Array(W).fill(0);
}
for (let i = 0; i < H; i++) {
for (let j = 0; j < W; j++) {
if (maps[i][j] === '#') {
continue;
} else if (nextIwashiMap[i][j] > 0) {
nextIwashiMap[i][j] += iwashiMap[i][j];
continue;
}
let distanceMap = new Array(H);
for (let k = 0; k < H; k++) {
distanceMap[k] = new Array(W).fill(H * W);
}
let queue = [];
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
if (maps[y][x] === '#') continue;
if (i === y && j === x) continue;
if (iwashiMap[y][x] > 0) {
distanceMap[y][x] = 0;
queue.push({y, x});
}
}
}
if (player.paralyzed === 0) {
distanceMap[player.y][player.x] = 0;
queue.push({y: player.y, x: player.x});
}
while (queue.length > 0) {
let pos = queue.shift();
for(let k = 0; k < 4; k++) {
let nx = pos.x + dx[k];
let ny = pos.y + dy[k];
if (!this.isInside(ny, nx)) continue;
if (maps[ny][nx] === '#') continue;
if (distanceMap[ny][nx] > distanceMap[pos.y][pos.x] + 1) {
distanceMap[ny][nx] = distanceMap[pos.y][pos.x] + 1;
queue.push({x: nx, y: ny});
}
}
}
for (let k = 0; k < 5; k++) {
let ni = i + dy[k];
let nj = j + dx[k];
if (!this.isInside(ni, nj)) continue;
if (maps[ni][nj] === '#') continue;
if (distanceMap[i][j] > distanceMap[ni][nj] || k === 4) {
nextIwashiMap[ni][nj] += iwashiMap[i][j];
break;
}
}
}
}
return nextIwashiMap;
}
run(input) {
input = input.split("\n");
const dx = [0, 1, 0, -1, 0];
const dy = [1, 0, -1, 0, 0];
const str = "SENWX";
var arr = input[0].split(" ");
var H = Number(arr[0]), W = Number(arr[1]), T = Number(arr[2]), N = Number(arr[3]);
arr = input[1].split(" ");
var player = {
x: Number(arr[1]),
y: Number(arr[0]),
paralyzed: Number(arr[2])
};
var maps = new Array(H);
for(let i = 0; i < H; i++) {
maps[i] = input[i + 2];
}
var iwashi = new Array(N);
for(let i = 0; i < N; i++) {
let line = input[i + 2 + H].split(" ");
iwashi[i] = {
x: parseInt(line[0]),
y: parseInt(line[1]),
t: parseInt(line[2])
};
}
var iwashiMap = new Array(H);
for(let i = 0; i < H; i++) iwashiMap[i] = new Array(W).fill(0);
var iwashiIdx = 0;
iwashi.sort((a, b) => (a.t-b.t));
while(iwashiIdx < N && iwashi[iwashiIdx].t === 0) {
iwashiMap[iwashi[iwashiIdx].y][iwashi[iwashiIdx].x]++;
iwashiIdx++;
}
let ret = "";
let turn = 0;
while(turn++ < T) {
if(player.paralyzed > 0) {
ret += "A";
}
else {
// 各マスからスコアマップを作成
let scoreMap = new Array(H);
for(let i = 0; i < H; i++) scoreMap[i] = new Array(W).fill(0);
for(let i = 0; i < H; i++) {
for(let j = 0; j < W; j++) {
if(iwashiMap[i][j] > 0) {
let iwashiValue = iwashiMap[i][j] * 3;
if(iwashiValue > 5) {
iwashiValue = -10;
}
let addMap = new Array(H);
for(let i = 0; i < H; i++) addMap[i] = new Array(W).fill(0);
let queue = [{x: j, y: i}];
addMap[i][j] = iwashiValue;
while(queue.length > 0) {
let pos = queue.shift();
for(let k = 0; k < 4; k++) {
let nx = pos.x + dx[k];
let ny = pos.y + dy[k];
if(!this.isInside(nx, ny, W, H)) continue;
if(maps[ny][nx] === "#") continue;
if(addMap[ny][nx] !== 0) continue;
addMap[ny][nx] += addMap[pos.y][pos.x] * 0.5;
queue.push({x: nx, y: ny});
}
}
for(let y = 0; y < H; y++) {
for(let x = 0; x < W; x++) {
scoreMap[y][x] += addMap[y][x];
}
}
}
}
}
// 一番スコアが高い方向へ移動
let to = 4;
for(let i = 4; i >= 0; i--) {
let nx = player.x + dx[i];
let ny = player.y + dy[i];
if(!this.isInside(nx, ny, W, H)) {
continue;
}
if(maps[ny][nx] === "#") continue;
if(scoreMap[ny][nx] >= scoreMap[player.y + dy[to]][player.x + dx[to]]) {
to = i;
}
}
ret += str[to];
player.x += dx[to];
player.y += dy[to];
}
// iwashimove
iwashiMap = this.iwashiMove(iwashiMap, player, maps, H, W);
if(player.paralyzed > 0) {
player.paralyzed--;
}
else if(0 < iwashiMap[player.y][player.x] && iwashiMap[player.y][player.x] <= 5){
iwashiMap[player.y][player.x] = 0;
}
else if(iwashiMap[player.y][player.x] > 5) {
player.paralyzed += 5;
iwashiMap[player.y][player.x] = 0;
}
}
return ret;
}
}
module.exports = new AI();