Skip to Content
CoursesCSC102การควบคุมระบบไฟพิพิธภัณฑ์ด้วยอาเรย์ 3 มิติ

การควบคุมระบบไฟพิพิธภัณฑ์ด้วยอาเรย์ 3 มิติ

โจทย์

A museum’s lighting system is organized into floors, rooms, and light zones, represented as a 3D array. Each light can have a status: 1 (On) and 0 (Off). Write a program to turn off all lights on a given floor and count the total number of lights still on across the building.

Input:

  • The first line contains three integers:
    • F: Number of floors
    • R: Number of rooms per floor
    • Z: Number of light zones per room
  • The next F×RF \times R lines contain Z integers each, representing the light status in each room (1 for On, 0 for Off).
  • The last line contains a single integer, floor_num, representing the floor to turn off (1-based index).

Output:

  • The updated 3D array, where all lights on the specified floor are turned off.
  • The total number of lights was still on across the entire building.
InputOutput
2 2 3
1 0 1
1 1 0

0 1 1
1 0 0
2
1 0 1
1 1 0

0 0 0
0 0 0

Total Lights On: 4

Explanation:

InputOutput
2 2 3
1 0 1 // Floor 1, Room 1
1 1 0 // Floor 1, Room 2

0 1 1 // Floor 2, Room 1
1 0 0// Floor 2, Room 2
2 // Turn off all lights on Floor 2
1 0 1
1 1 0

0 0 0
0 0 0

Total Lights On: 4

โค้ด

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int floors = scanner.nextInt(); int rooms = scanner.nextInt(); int zones = scanner.nextInt(); int lightOnCount = 0; boolean[][][] lightStatus = new boolean[floors][rooms][zones]; for (int i = 0; i < floors; i++) { for (int j = 0; j < rooms; j++) { for (int k = 0; k < zones; k++) { boolean status = scanner.nextInt() == 1; lightStatus[i][j][k] = status; if (status) lightOnCount++; } } } int turnOffFloor = scanner.nextInt(); for (int j = 0; j < rooms; j++) { for (int k = 0; k < zones; k++) { boolean currentStatus = lightStatus[turnOffFloor - 1][j][k]; if (currentStatus) { lightStatus[turnOffFloor - 1][j][k] = false; lightOnCount--; } } } for (int i = 0; i < floors; i++) { for (int j = 0; j < rooms; j++) { for (int k = 0; k < zones; k++) { System.out.printf("%d ", lightStatus[i][j][k] ? 1 : 0); } System.out.println(); } System.out.println(); } System.out.printf("Total Lights On: %d", lightOnCount); } }

คำอธิบาย

1. การประกาศคลาสและรับข้อมูลพื้นฐาน

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int floors = scanner.nextInt(); // จำนวนชั้น int rooms = scanner.nextInt(); // จำนวนห้องต่อชั้น int zones = scanner.nextInt(); // จำนวนโซนไฟต่อห้อง

2. การเตรียมตัวแปรและโครงสร้างข้อมูล

int lightOnCount = 0; // นับจำนวนไฟที่เปิดอยู่ boolean[][][] lightStatus = new boolean[floors][rooms][zones]; // เก็บสถานะไฟ

3. การรับข้อมูลสถานะไฟเริ่มต้น

for (int i = 0; i < floors; i++) { // วนลูปตามชั้น for (int j = 0; j < rooms; j++) { // วนลูปตามห้อง for (int k = 0; k < zones; k++) { // วนลูปตามโซนไฟ boolean status = scanner.nextInt() == 1; // แปลง 1/0 เป็น true/false lightStatus[i][j][k] = status; // เก็บสถานะ if (status) lightOnCount++; // นับไฟที่เปิด } } }

4. การปิดไฟในชั้นที่ระบุ

int turnOffFloor = scanner.nextInt(); // รับหมายเลขชั้นที่ต้องการปิดไฟ for (int j = 0; j < rooms; j++) { // วนลูปตามห้อง for (int k = 0; k < zones; k++) { // วนลูปตามโซนไฟ boolean currentStatus = lightStatus[turnOffFloor - 1][j][k]; if (currentStatus) { // ถ้าไฟกำลังเปิดอยู่ lightStatus[turnOffFloor - 1][j][k] = false; // ปิดไฟ lightOnCount--; // ลดจำนวนไฟที่เปิด } } }

5. การแสดงผลสถานะไฟและจำนวนไฟที่เปิดอยู่

// แสดงสถานะไฟทั้งหมด for (int i = 0; i < floors; i++) { for (int j = 0; j < rooms; j++) { for (int k = 0; k < zones; k++) { System.out.printf("%d ", lightStatus[i][j][k] ? 1 : 0); } System.out.println(); // ขึ้นบรรทัดใหม่หลังจบแต่ละห้อง } System.out.println(); // เว้นบรรทัดหลังจบแต่ละชั้น } // แสดงจำนวนไฟที่ยังเปิดอยู่ System.out.printf("Total Lights On: %d", lightOnCount);

แผนภาพการทำงาน

โครงสร้างข้อมูล 3 มิติ

โครงสร้างพิพิธภัณฑ์: ชั้น 1 ชั้น 2 ชั้น 3 [ห้อง 1] [ห้อง 1] [ห้อง 1] [ห้อง 2] [ห้อง 2] [ห้อง 2] แต่ละห้อง: [โซน 1 โซน 2 โซน 3]

ตัวอย่างการทำงาน

ตัวอย่างที่ 1

Input: 2 2 3 // 2 ชั้น, 2 ห้อง/ชั้น, 3 โซน/ห้อง 1 0 1 // ชั้น 1 ห้อง 1 1 1 0 // ชั้น 1 ห้อง 2 0 1 1 // ชั้น 2 ห้อง 1 1 0 1 // ชั้น 2 ห้อง 2 1 // ปิดไฟชั้น 1 Output: 0 0 0 // ชั้น 1 (ถูกปิดทั้งหมด) 0 0 0 0 1 1 // ชั้น 2 (ยังคงสถานะเดิม) 1 0 1 Total Lights On: 4
ปรับปรุงล่าสุด