Skip to content

Commit d553857

Browse files
author
Carlos Leonard
committed
update
1 parent 4ff5085 commit d553857

12 files changed

+110
-49
lines changed

82_ClosestEnemy2.cpp

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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>
1516
using 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

25115
int 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
}

Debug/Fun Practice.Build.CppClean.log

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,9 @@
11
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\vc120.pdb
22
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\vc120.idb
3-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\80_starrating.obj
43
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\50_fooddistribution.obj
5-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\60_closestenemy.obj
6-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\61_largestfour.obj
7-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\62_questionmarks.obj
8-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\63_camelcase.obj
9-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\64_summultiplier.obj
10-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\65_stringmerge.obj
11-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\66_simpleevens.obj
12-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\67_onedecremented.obj
13-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\68_distinctcharacters.obj
14-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\69_snakecase.obj
15-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\6_longestword.obj
16-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\70_elementmerger.obj
17-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\71_asciiconversion.obj
18-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\72_gcf.obj
19-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\73_closestenemy2.obj
20-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\74_serialnumber.obj
21-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\75_stringperiods.obj
22-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\76_numberstream.obj
23-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\77_palindromeswapper.obj
24-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\78_removebrackets.obj
25-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\79_commandline.obj
26-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code37.obj
27-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code45.obj
28-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code50.obj
29-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code55.obj
30-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code56.obj
31-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code57.obj
32-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code58.obj
33-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code59.obj
34-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\test.obj
35-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.ilk
36-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\vc140.idb
37-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.pdb
38-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\vc140.pdb
4+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\83_grouptotals.obj
5+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\debug\fun practice.ilk
6+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\debug\fun practice.exe
397
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\debug\fun practice.pdb
408
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.tlog\cl.command.1.tlog
419
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.tlog\cl.read.1.tlog

Debug/Fun Practice.log

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
Build started 4/30/2018 11:50:08 AM.
1+
Build started 5/1/2018 2:13:57 PM.
22
1>Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" on node 2 (Build target(s)).
33
1>ClCompile:
4-
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt 83_GroupTotals.cpp
5-
83_GroupTotals.cpp
6-
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\83_grouptotals.cpp(34): warning C4018: '<' : signed/unsigned mismatch
7-
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\83_grouptotals.cpp(49): warning C4018: '<' : signed/unsigned mismatch
4+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt 82_ClosestEnemy2.cpp
5+
82_ClosestEnemy2.cpp
6+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\82_closestenemy2.cpp(36): warning C4018: '<' : signed/unsigned mismatch
7+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\82_closestenemy2.cpp(63): warning C4018: '<' : signed/unsigned mismatch
88
Link:
9-
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\83_GroupTotals.obj
9+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\82_ClosestEnemy2.obj
1010
Fun Practice.vcxproj -> C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe
1111
1>Done Building Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" (Build target(s)).
1212

1313
Build succeeded.
1414

15-
Time Elapsed 00:00:01.47
15+
Time Elapsed 00:00:01.34
-12.8 KB
Binary file not shown.
-830 Bytes
Binary file not shown.
-850 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.
-384 Bytes
Binary file not shown.
4 Bytes
Binary file not shown.

Debug/vc120.idb

-80 KB
Binary file not shown.

0 commit comments

Comments
 (0)