The “Number of Laser Beams in a Bank” problem involves calculating the number of laser beams that can be formed between security devices in a bank. Each row in the bank represents a set of security devices, and only rows with at least one device can form beams with each other. A beam can be formed between each device in one row and each device in another row.
Problem Statement
Given a 2D grid representing the bank, where:
'0'
represents an empty room,'1'
represents a security device,
Calculate the total number of laser beams that can be formed. A laser beam can be formed between two devices in different rows, and the number of beams between two rows is the product of the number of devices in those rows.
C# Implementation
Here’s a simple implementation of the solution:
using System;
public class LaserBeamsCounter
{
public static int CountLaserBeams(string[] bank)
{
int previousDevices = 0; // Count of devices in the previous row
int totalBeams = 0; // Total number of laser beams
foreach (var row in bank)
{
int currentDevices = 0;
// Count the number of devices ('1's) in the current row
foreach (char cell in row)
{
if (cell == '1')
{
currentDevices++;
}
}
// If there are devices in the current row, calculate beams
if (currentDevices > 0)
{
totalBeams += previousDevices * currentDevices; // Calculate beams with previous row
previousDevices = currentDevices; // Update for the next iteration
}
}
return totalBeams; // Return the total number of beams
}
public static void Main(string[] args)
{
string[] bank = new string[]
{
"011001",
"000000",
"010100",
"001000"
};
int result = CountLaserBeams(bank);
Console.WriteLine($"The total number of laser beams is: {result}");
}
}
Explanation
-
Data Structure:
- The bank is represented as an array of strings (
string[]
), where each string corresponds to a row and each character represents a room ('0'
or'1'
).
- The bank is represented as an array of strings (
-
CountLaserBeams Method:
- This method iterates through each row of the bank to count devices and calculate the total number of laser beams.
-
Variables:
previousDevices
: Keeps track of the number of security devices in the previous row.totalBeams
: Accumulates the total number of beams formed.
-
Row Iteration:
- For each row, the method counts how many devices (
'1'
) are present. - If the current row has devices:
- It calculates the number of beams formed with the previous row using the formula:
previousDevices * currentDevices
. - Updates
previousDevices
for the next iteration.
- It calculates the number of beams formed with the previous row using the formula:
- For each row, the method counts how many devices (
-
Return Value:
- After processing all rows, the method returns the total number of laser beams.
-
Main Method:
- In the
Main
method, we define a sample bank and call theCountLaserBeams
method, then print the result.
- In the
Usage
You can modify the bank
variable in the Main
method to test different configurations. This implementation efficiently counts the number of laser beams based on the devices available in the bank rows.