-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsq.php
87 lines (76 loc) · 2.54 KB
/
bsq.php
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
<?php
if(isset($argv[1])){
$check = fopen($argv[1].'.txt', 'r') || die("Unable to open file!");
$path = $argv[1].'.txt';
readPlate($path);
}else{
echo "Veuillez mettre un chemin de fichier texte en deuxième paramètre\n";
}
function readPlate($path){
$plan = fopen($path, 'r');
while(! feof($plan)) {
$myMap[] = trim(fgets($plan));
}
//transforms the map into a number of 0 and 1(easier to find the biggest square this way)
for($i = 1; $i < count($myMap); $i++){
for($j = 0; $j < strlen($myMap[$i]); $j++){
if($myMap[$i][$j] == 'o'){
$mapWithNumbers[$i][$j] = 0;
}
else{
$mapWithNumbers[$i][$j] = 1;
}
}
}
giveMeSolution($mapWithNumbers);
findThePosition($mapWithNumbers, $myMap);
for($i = 1; $i < count($myMap); $i++){
echo $myMap[$i]."\n";
}
}
function giveMeSolution(array &$mapWithNumbers){
for($i = 1; $i < count($mapWithNumbers); $i++){
for($j = 0; $j < count($mapWithNumbers[$i]); $j++){
if($i == 1 || $j == 0 ){
continue;
}
else if($mapWithNumbers[$i][$j] > 0){
$mapWithNumbers[$i][$j] = 1 + min([$mapWithNumbers[$i][$j - 1],
$mapWithNumbers[$i - 1][$j],
$mapWithNumbers[$i - 1][$j - 1]]);
}
}
}
}
function findThePosition(&$mapWithNumbers, &$myMap){
$max = 0;
$positon = [];
for($i = 1; $i < count($mapWithNumbers); $i++){
for($j = 0; $j < count($mapWithNumbers[$i]); $j++){
if($mapWithNumbers[$i][$j] >= $max){
if($mapWithNumbers[$i][$j] == $max && !empty($positon)){
if($positon[0][1] > $j){
$max = $mapWithNumbers[$i][$j];
$positon[0][0] = $i;
$positon[0][1] = $j;
continue;
}
else{
continue;
}
}
$max = $mapWithNumbers[$i][$j];
$positon[0][0] = $i;
$positon[0][1] = $j;
}
}
}
return drawMyCrossedMap($myMap, $positon, $max);
}
function drawMyCrossedMap(&$myMap, $positon, $occurence){
for($i = $positon[0][0]; $i > $positon[0][0] - $occurence; $i--){
for($j = $positon[0][1]; $j > $positon[0][1] - $occurence; $j--){
$myMap[$i][$j] = "X";
}
}
}