Skip to content

Commit 3c3acc5

Browse files
committed
ajout correction exos sables mouvants
1 parent 88c2728 commit 3c3acc5

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function ContestResponse(){
2+
//implement your code here using input array
3+
let [H, L] = input.shift().split` `.map(Number);
4+
let tableau = input.map(v => v.replace(/\./g, 0).split``);
5+
6+
for(let y = 0; y < H; y++)
7+
for(let x = 0; x < L; x++)
8+
{
9+
if(tableau[y][x] == "#")
10+
{
11+
let voisinHaut = isValidPos(tableau, x, y - 1) ? tableau[y-1][x] : 0;
12+
let voisinGauche = isValidPos(tableau, x - 1, y) ? tableau[y][x-1] : 0;
13+
tableau[y][x] = Math.min(voisinHaut, voisinGauche) + 1;
14+
} else {
15+
tableau[y][x] = 0;
16+
}
17+
}
18+
19+
let max = 0;
20+
21+
for(let y = H - 1; y > -1; y--)
22+
for(let x = L - 1; x > -1; x--)
23+
{
24+
if(tableau[y][x] != 0)
25+
{
26+
let voisinDroite = isValidPos(tableau, x + 1, y) ? tableau[y][x+1] : 0;
27+
let voisinBas = isValidPos(tableau, x, y + 1) ? tableau[y+1][x] : 0;
28+
let profondeur = Math.min(voisinDroite, voisinBas) + 1;
29+
if(profondeur <= tableau[y][x])
30+
{
31+
tableau[y][x] = profondeur;
32+
if(profondeur > max)
33+
{
34+
max = profondeur;
35+
}
36+
}
37+
}
38+
}
39+
40+
console.log(max);
41+
}
42+
43+
function isValidPos(tableau2D, x, y)
44+
{
45+
let tailleYMax = tableau2D.length;
46+
let tailleXMax = tableau2D[0].length;
47+
if(x >= 0 && x < tailleXMax && y >= 0 && y < tailleYMax)
48+
{
49+
return true;
50+
}
51+
return false;
52+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
function ContestResponse(){
2+
//implement your code here using input array
3+
console.error("debut");
4+
let [H, L] = input.shift().split` `.map(Number);
5+
let tableau = input.map(v => v.replace(/\./g, 0).split``);
6+
let change = true;
7+
let i = 0;
8+
while(change)
9+
{
10+
change = false;
11+
for(let y = 0; y < H; y++)
12+
{
13+
for(let x = 0; x < L; x++)
14+
{
15+
if(tableau[y][x] == "#")
16+
{
17+
let voisins = getCoordsVoisins(x, y, false);
18+
let proches = voisins.map(v => {
19+
let valide = isValidPos(tableau, v[1], v[0]);
20+
if(valide)
21+
{
22+
return tableau[v[1]][v[0]];
23+
} else {
24+
return 0;
25+
}
26+
}).filter(v => v != "#");
27+
if(proches.length)
28+
{
29+
let profondeur = (Math.min(...proches));
30+
if(profondeur <= i)
31+
{
32+
tableau[y][x] = (Math.min(...proches) + 1);
33+
change = true;
34+
}
35+
}
36+
}
37+
}
38+
}
39+
i++;
40+
}
41+
console.error(JSON.stringify(tableau.flatMap(v => v)));
42+
console.log(Math.max(...tableau.flatMap(v => v)));
43+
}
44+
45+
function isValidPos(tableau2D, x, y)
46+
{
47+
let tailleYMax = tableau2D.length;
48+
let tailleXMax = tableau2D[0].length;
49+
if(x >= 0 && x < tailleXMax && y >= 0 && y < tailleYMax)
50+
{
51+
return true;
52+
}
53+
return false;
54+
}
55+
56+
function getCoordsVoisins(x, y, diagonales=true)
57+
{
58+
let coords = [];
59+
coords.push([x+1, y]);
60+
coords.push([x, y+1]);
61+
coords.push([x, y-1]);
62+
coords.push([x-1, y]);
63+
if(diagonales)
64+
{
65+
coords.push([x-1, y-1]);
66+
coords.push([x+1, y+1]);
67+
coords.push([x-1, y+1]);
68+
coords.push([x+1, y-1]);
69+
}
70+
return coords;
71+
}

0 commit comments

Comments
 (0)