@@ -12,25 +12,118 @@ For this input your program should return 2 because the closest enemy (2) is 2 s
1212
1313#include < iostream>
1414#include < string>
15+ #include < vector>
1516using namespace std ;
1617
18+ /*
19+ during the traversal, we extract the locations of our source
20+ alongside the multiple destinations if any are found
21+ if no destinations are in the matrix, we return 0
22+ else we compare source to current destination
23+ we analyze both their distance and keep track of number of steps
24+ after we finish analyzing each distance, we return the lowest steps gathered
25+ */
26+ int ClosestEnemyII (string strArr[], int size)
27+ {
28+ vector < vector<int > > destinations; // stores our destination indexes
29+ int sourceRow;
30+ int sourceCol;
31+ int minimalSteps = size * 100 ;
1732
18- // NOT FINISHED
33+ // traverse the input array to extract its values
34+ for (int x = 0 ; x < size; x++)
35+ {
36+ for (int y = 0 ; y < strArr[x].length (); y++)
37+ {
38+ // conditions to store critical locations
39+ if (strArr[x][y] == ' 1' )
40+ {
41+ sourceRow = x;
42+ sourceCol = y;
43+ }
44+ else if (strArr[x][y] == ' 2' )
45+ {
46+ vector <int > temp;
47+ temp.push_back (x);
48+ temp.push_back (y);
49+
50+ destinations.push_back (temp);
51+ }
52+ }
53+ }
1954
20- string ClosestEnemyII (string strArr[], int size)
21- {
55+ // check if any destinations were found
56+ if (destinations.size () == 0 )
57+ {
58+ return 0 ;
59+ }
60+ else
61+ {
62+ // iterate through our destinations and gather their distance from source
63+ for (int x = 0 ; x < destinations.size (); x++)
64+ {
65+ // current enemy location
66+ int targetRow = destinations[x][0 ];
67+ int targetCol = destinations[x][1 ];
68+
69+ // calculating their distance
70+ int rowDifference = targetRow - sourceRow;
71+ int colDifference = targetCol - sourceCol;
72+ if (rowDifference < 0 )
73+ {
74+ rowDifference *= -1 ;
2275
76+ // conditions to check wrapping around is more optimal
77+ if (rowDifference > size - (sourceRow- targetRow))
78+ {
79+ colDifference = size - (sourceRow - targetRow);
80+ }
81+ }
82+ else if (rowDifference > size - (targetRow - sourceRow))
83+ {
84+ rowDifference = size - (targetRow - sourceRow);
85+ }
86+
87+ if (colDifference < 0 )
88+ {
89+ colDifference *= -1 ;
90+
91+ // conditions to check wrapping around is more optimal
92+ if (colDifference > size - (sourceCol - targetCol))
93+ {
94+ colDifference = size - (sourceCol - targetCol);
95+ }
96+ }
97+ else if (colDifference > size - (targetCol - sourceCol))
98+ {
99+ colDifference = size - (targetCol - sourceCol);
100+ }
101+
102+ int currentSteps = colDifference + rowDifference;
103+
104+ // keep track of the lowest distance
105+ if (currentSteps < minimalSteps)
106+ {
107+ minimalSteps = currentSteps;
108+ }
109+ }
110+ }
111+
112+ return minimalSteps;
23113}
24114
25115int main ()
26116{
27117 string A[] = { " 0000" , " 1000" , " 0002" , " 0002" };
28118 string B[] = { " 000" , " 100" , " 200" };
29119 string C[] = { " 0000" , " 2010" , " 0000" , " 2002" };
30-
120+ string D[] = { " 0000" , " 0010" , " 0000" , " 0000" };
121+ string E[] = { " 01000" , " 00020" , " 00000" , " 00002" , " 02002" };
122+
31123 cout << ClosestEnemyII (A, sizeof (A) / sizeof (A[0 ])) << endl; // 2
32124 cout << ClosestEnemyII (B, sizeof (B) / sizeof (B[0 ])) << endl; // 1
33125 cout << ClosestEnemyII (C, sizeof (C) / sizeof (C[0 ])) << endl; // 2
34-
126+ cout << ClosestEnemyII (D, sizeof (D) / sizeof (D[0 ])) << endl; // 0
127+ cout << ClosestEnemyII (E, sizeof (E) / sizeof (E[0 ])) << endl; // 1
35128 return 0 ;
36129}
0 commit comments