Java Program to Find the Number of 'X' Total Shapes29 Mar 2025 | 5 min read In this section, we will solve a problem in which we have to count 'X' shapes in a 2D matrix. The letters in the matrix are either 'X,' or 'O,' where the 'X' represent the part of the form and the 'O' represent space. The aim is to count the number of distinct linked "X" shapes-a shape is defined as a group of adjacent "X"s-that are present. Neighbours that are adjacent are those that are vertical and horizontal, but not diagonal. Problem StatementGiven a 2D matrix where each cell is either 'X' or 'O', the objective is to determine how many distinct groups of connected 'X's (shapes) are present. For example, in the matrix: There are three distinct 'X' shapes:
ApproachIn order to resolve this problem, one can apply DFS or BFS method for the matrix and count the number of linked 'X's Only. Any unexplored 'X' will be considered as the starting point for a new shape; the 'X's will be drawn in DFS or BFS; counter of shapes will be incremented.
DFS Algorithm
File Name: XShapeCounter.java Output: Number of 'X' shapes: 3 ExplanationWith 'X' cells denoting a portion of a form and 'O' cells denoting empty space in a 2D character grid, the XShapeCounter class counts different 'X' shapes. The countXShapes() method iterates over the grid, tracking processed cells using a visited matrix. The dfs() method initiates a Depth-First Search (DFS) to search all related 'X' cells in four directions (up, down, left, and right) and mark them as visited when an unvisited 'X' is located. The number of shapes is increased in proportion to the number of DFS calls, which each represent a single form. Only legitimate, unexplored 'X' cells are examined thanks to the isValid() helper function, which also verifies boundary conditions. The primary method outputs the overall count of different 'X' forms and tests the software on a sample grid. ConclusionIn conclusion, the Depth-First Search (DFS) method used by the XShapeCounter application effectively counts the number of unique "X" forms in a 2D grid. The software investigates all linked 'X's in four directions (up, down, left, and right), iterating around the grid and marking visited 'X' cells such that each form is tallied only once. The problem of counting related components in a matrix may be solved in an understandable and efficient manner using this method. The visited matrix guarantees that no cell is handled more than once, improving efficiency, and the algorithm is designed to handle common edge circumstances like empty grids. This method may be expanded to address related issues with linked elements in matrices or grids. Next TopicJava Code for DES |
In Jackson, Tree Model Node is one of the most important concepts which we are going to discuss in this section. We will use Tree Model Node for various conversions and for adding, modifying, and removing nodes. Let's understand how we can create a Node, transform a...
32 min read
In order to maintain the stability and dependability of the system, it's critical to handle mistakes and exceptions gracefully when developing software. The concepts of fail-safe and fail-fast are frequently used to handle errors. Both strategies have benefits and drawbacks, and knowing the distinctions between them...
3 min read
In this section, we will learn everything about the , i.e., what a console is, how we can use the console, how we can implement output in the console, how we can take input using the console, etc. What is a Console? To run a program, we might...
18 min read
Representing linear equations in matrix form is essential in linear algebra, widely applied across scientific and engineering disciplines. This approach consolidates a system of linear equations into matrices: for coefficients and constants. By doing so, matrix operations like inversion, multiplication, and determinant calculation become viable methods...
6 min read
Java is one of the most popular programming languages in the world, known for its versatility and wide range of applications. One of the most powerful features of Java is its collection framework, which includes classes and interfaces for managing collections of objects. One of the...
4 min read
A super prime is a prime number that occupies a prime position in the sequence of all prime numbers. For example, in the list {2, 3, 5, 7, 11}, the second prime (3) and the third prime (5) are super primes. Identifying super primes involves both...
9 min read
In this section, we will discuss what is method hiding in Java, method hiding factors (MHF), and the differences between method hiding and method overriding. Also, implement the method hiding concept in a Java program. To understand the method hiding concept in Java, first, we will understand...
3 min read
As we all know, while solving any CP problems, the very first step is collecting input or reading input. A common mistake we all make is spending too much time on writing code and compile-time as well. In Java, it is recommended to use BufferedReader over...
4 min read
The finalize() method is a protected method of the java.lang.Object class. It is used to perform cleanup operations (such as closing open files, releasing memory, or shutting down connections) on an object before it is removed from memory. Method Signature: protected void finalize() throws Throwable { ...
5 min read
When working with strings in Java, developers often need to break them down into smaller components for processing or analysis. This process, known as tokenization, can be achieved using various techniques. One such technique is utilizing the countTokens() method provided by the StringTokenizer class. In this...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India