L1-1: Print Greeting Print a fixed greeting message. print("yuan shen, qi dong!") L1-2: Value Comparison Given a real number x, compare it to 1 and print the appropriate relational operator. #include <iostream> using namespace std; int main() { double val; cin >> val; if(val &l...
This problem can be effectively solved using a Breadth-First Search (BFS) algoritmh. The core idea is to explore possible states, where a state is defined by the positions of two characters and the number of steps taken to reach that state. We can represent a state using a structure that stores the...
Data Structure Design The core data structure for the game is encapsulated in a global class called Data, wich stores all game-related information as static variables. This design allows for a single game instance to run at a time. The main data fields include: status: Represents the game state (e.g...
Problem Analysis This problem requires splitting and merging intervals with maximum profit. The solution naturally fits the interval dynamic programming paradigm. DP Formulation For any interval [l, r], we choose a split point j (where l ≤ j < r) and split it into two subintervals: [l, j] and [j+...
Givan a rectangular maze represented by a grid of characters, determine the minimum number of steps required for a rat to reach from the starting point 'S' to the cheese at position 'E'. Walls are marked with '#', and open paths are marked with '.'. The problem is solved using Breadth-First Search (...
Unweighted graph or grid shortest path problems leverage BFS becuase the first visit to a node yields the minimum step count, and iterative queue-based traversal avoids stack overflow risks inherent to deep DFS calls. 8-Puzzle Solver The 8-puzzle can be modeled as a state space where each arrangemen...
In an unweighted graph, the distance between vertices is naturally measured in hops. Direct neighbors are one hop away, while neighbors of neighbors occupy the second hop—mirroring the idea that any two individuals are linked by a short chain of connections. Each successive ring of vertices forms a...
Given an N×N grid representing a maritime region where ‘.’ denotes ocean and ‘#’ denotes land, determine how many islands will be completely submerged due to rising sea levels. An island consists of connected land cells in four directions (up, down, left, right). Each land cell adjacent to ocean wil...
Core Concepts and Queue Usage Breadth-First Search (BFS) is fundamental for traversing graphs and trees, particularly useful for level order traversal, identifying connected components, topological sorting, and finding the shortest path in unweighted graphs (where all edges possess a length of 1). W...
Given a connected undirected graph with $n$ nodes and $m$ edges, and two distinct nodes $a$ and $b$, count unordered pairs $(u, v)$ where $u < v$ such that: $u \neq a$, $u \neq b$, $v \neq a$, $v \neq b$ Every path from $u$ to $v$ passes through both $a$ and $b$ Approach The solution involves ide...