Fading Coder

One Final Commit for the Last Sprint

Python Algorithm Practice for Blue Bridge Cup Competition - Set 2

1. Minefield Crossing Approach: BFS traversal using a queue. import sys n = int(input()) grid = [input().split() for _ in range(n)] visited = [[0] * n for _ in range(n)] directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] queue = [] end_x, end_y = 0, 0 for i in range(n): for j in range(n): if grid[i][j]...

Heuristic Merging and Tree Heuristic Merging: A Comprehensive Guide

Heuristic Merging and Tree Heuristic Merging: A Comprehensive Guide Core Concepts Fundamental Knowledge: Heuristic Merging (DSU) Heuristic algorithms are optimizations based on human experience and intuition. The classic example of heuristic merging is the union-find data structure's union by size/r...

Solution to AtCoder Beginner Contest 446

D - Max Straight This problem can be solved efficient using a hash map. Initially, I overcomplicated it by thinking about sorting and finding the longest increasing subsequence. However, a simple approach using a map workss perfectly. #include <bits/stdc++.h> using namespace std; int main() {...

Matrix Flood Fill Algorithm for Closed Regions

Problem Description Given an n×n matrix composed of 0s and 1s, where 1s form a closed boundary, the task is to identify and fill all 0s enclosed within this boundary with 2s. A 0 is considered enclosed if it cannot reach the matrix boundary by moving only through adjacent 0s (up, down, left, right)....

Algorithmic Problem Set Solutions: From Basics to Advanced Data Structures

Problem 1: Unique Element Extraction The task requires reading a sequence of integers and printing each distinct value exactly once in the order of their first appearance. A hash-based container provides an efficient way to track visited numbers. #include <iostream> #include <unordered_set&...

Efficiently Counting PAT Substrings in a String

Given a string composed exclusively of 'P', 'A', and 'T' characters, determine the total number of contiguous 'PAT' substrings. Each 'PAT' substring consists of a 'P' flolowed by an 'A' followed by a 'T', with characters in sequence but not necessarily adjacent. The result must be modulo 1000000007...

Linked List Operations: Swapping Adjacent Nodes, Removal, Intersection, and Cycle Detection

Swapping Adjacent Nodes in a Linked List The core idea involves using a dummy head node to simplify edge cases and ensure consistent operations on the first node. When swapping node pairs, careful attention must be paid to preserving references before reassigning pointers. Key considerations: Use a...

Solving the Binary Land Problem Using Breadth-First Search

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...

Competitive Programming Solutions: Matrix Construction and Combinatorial Problems

To construct an n×n matrix where no two adjacent elements differ by exactly 1, we can implement a column-based shifting approach. For odd-numbered columns, shift all elements upward by one position, moving the overflow element to the bottom. This construction works for all cases except n=1, where n...

Sorting Algorithms Overview

Bubble Sort **1. Core Concept:**Starting from the beginning of an unordered sequence, compare adjacent elements and swap them if needed to place larger (or smalller) values at the end of the sorted portion. This process continues until all elements are properly ordered. 2. Execution Flow****The bubb...