0% found this document useful (0 votes)
11 views3 pages

Mirror Maze

The document contains a C++ program that simulates movement through a grid with mirrors, determining the maximum loop length that can be formed. It uses depth-first search (DFS) to explore the grid, tracking visited states to avoid infinite loops. The program reads grid dimensions and values from input, processes the grid, and outputs the maximum loop length found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Mirror Maze

The document contains a C++ program that simulates movement through a grid with mirrors, determining the maximum loop length that can be formed. It uses depth-first search (DFS) to explore the grid, tracking visited states to avoid infinite loops. The program reads grid dimensions and values from input, processes the grid, and outputs the maximum loop length found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <vector>

using namespace std;

int M, N;

vector<vector<char>> grid;

vector<vector<vector<bool>>> visited;

// These are the directions to be used for movement: right, down, left, and up

const vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

// After hitting a mirror, this function will be used to calculate the next direction

int getNextDirection(int direc, char mirror) {

if (mirror == '/') {

if (direc == 0) return 3;

if (direc == 1) return 2;

if (direc == 2) return 1;

if (direc == 3) return 0;

} else if (mirror == '\\') {

if (direc == 0) return 1;

if (direc == 1) return 0;

if (direc == 2) return 3;

if (direc == 3) return 2;

return direc;

int dfs(int x, int y, int direc) {

int startX = x, startY = y, startDirec = direc, count = 0;


while (true) {

if (x < 0 || x >= M || y < 0 || y >= N) return 0; // Not a valid loop, maybe

if (visited[x][y][direc]) break; // Look for a loop

visited[x][y][direc] = true;

count++;

if (grid[x][y] == '/' || grid[x][y] == '\\') {

direc = getNextDirection(direc, grid[x][y]);

x += directions[direc].first;

y += directions[direc].second;

return (x == startX && y == startY && direc == startDirec) ? count : 0;

int findMaxLoop() {

int maxLength = 0;

visited = vector<vector<vector<bool>>>(M, vector<vector<bool>>(N, vector<bool>(4, false)));

for (int i = 0; i < M; ++i) {

for (int j = 0; j < N; ++j) {

if (grid[i][j] == '/' || grid[i][j] == '\\') {

for (int direc = 0; direc < 4; ++direc) {

if (!visited[i][j][direc]) {

maxLength = max(maxLength, dfs(i, j, direc));

}
}

return maxLength;

int main() {

cin >> M >> N;

grid = vector<vector<char>>(M, vector<char>(N));

for (int i = 0; i < M; ++i) {

for (int j = 0; j < N; ++j) {

cin >> grid[i][j];

int result = findMaxLoop();

cout << result << endl;

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy