CodeIgniter Forums
I have a logical question one.. can any one tell the logic....... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: I have a logical question one.. can any one tell the logic....... (/showthread.php?tid=53167)



I have a logical question one.. can any one tell the logic....... - El Forum - 07-13-2012

[eluser]Unknown[/eluser]
Question 1
A neighborhood had been raided by Indian Army in search of Terrorist. They land on different buildings from a helicopter.
The roof tops of these buildings are connected to each other through a ladder.
Each soldier can control the buildings in straight lines (left-right and forward-backward) with respect to the building/position they have been dropped on.
They cannot control the diagonal buildings. There are 8 buildings in each row and column.

Input
Your program must read two arguments i.e. soldierCount and soldierPositions where
SoldierCount is the number of soldiers landed
and
SoldierPositions is an array of x and y coordinate positions of the soldiers {x,y} where x represents row and y represents column starting from {1,1}

Output
Your function GetBuildingCount should compute the total number of buildings controlled by soldiers and write it to 'output1' variable.

e.g. output1 for the above example should be 22.

Examples
When two soldiers are dropped at positions 5,5 and 5,3 respectively, then inputs to the function would be as follows

soldierCount = 2
soldierPositions = {{5,5}, {5,3}}

If you wish to specify inputs to your program - separate it by a Colon(Smile
E.g. The above inputs should be specified as 2:{{5,5},{5,3}}

Output:
output1 for the above example should be 22.



I have a logical question one.. can any one tell the logic....... - El Forum - 07-13-2012

[eluser]pzajdler[/eluser]
Great, what's the question? You want to know how to calculate number of buildings controlled by soldier? If so then:

For every soldier You control

8 buildings horizontally and 8 building vertically, based on his position You can create an array of buildings controlled by him
You do this for every soldier, then You merge those arrays simply removing duplicated elements.
Here You get 22, because they stay on the same row, so 6 of their buildings duplicate.
1: {5,1},{5,2},{5,3},{5,4},{5,5},{5,6},{5,7},{5,8},{1,5},{2,5},{3,5},{4,5},{5,5},{5,6},{5,7},{5,8}
2: {5,1},{5,2},{5,3},{5,4},{5,5},{5,6},{5,7},{5,8},{1,3},{2,3},{3,3},{4,3},{5,3},{6,3},{7,3},{8,3}

After adding them You'll get:
{5,1},{5,2},{5,3},{5,4},{5,5},{5,6},{5,7},{5,8},{1,5},{2,5},{3,5},{4,5},{5,6},{5,7},{5,8},{1,3},{2,3},{3,3},{4,3},{6,3},{7,3},{8,3} which gives You 22.

Smile

@DOWN
???? :>


I have a logical question one.. can any one tell the logic....... - El Forum - 07-13-2012

[eluser]InsiteFX[/eluser]
LOL



I have a logical question one.. can any one tell the logic....... - El Forum - 07-13-2012

[eluser]Aken[/eluser]
Do your own homework!


I have a logical question one.. can any one tell the logic....... - El Forum - 07-13-2012

[eluser]jmadsen[/eluser]
Or at least ask your teacher to assign you CodeIgniter-related homework you can ask us about, instead of general programming questions