Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Implementation of Minesweeper Game
Next article icon

A Number Link Game

Last Updated : 09 Feb, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

The Game : Consider an n × n array of squares. Some of the squares are empty, some are solid, and some non-solid squares are marked by integers 1, 2, 3, … Each integer occupies exactly two different squares on the board. The task of the player is to connect the two occurrences of each integer on the board by a simple path using horizontal and vertical movements alone. No two different paths are allowed to intersect one another. No path may include any solid square (solid squares are forbidden to appear on any path). Finally, all non-solid squares must be filled by the paths.

The Algorithm : To prepare a valid random puzzle with a given board size n × n, we first generate random simple mutually non-intersecting paths on the board. If a few isolated squares remain outside all the generated paths, mark these isolated squares as solid (forbidden). We then supply the endpoints of the paths and the list of the solid squares as the puzzle.
Thus we first generate a solution, and then work out the puzzle from the solution. The paths and the solid squares partition the n × n board. We use a union-find data structure to generate this partition. The data structure deals with the subsets of the set of n^2 squares on the board.

PseudoCode:

  1. Locate squares (i, j) and (k, l) randomly on the board such that:
    (a) (i, j) and (k, l) are neighbors of one another, and
    (b) neither (i, j) nor (k, l) belongs to any path generated so far. If no such pair of squares is found on the entire board, return FAILURE     /* Here, (i, j) and (k, l) are the first two squares on the new path to be constructed. */
  2. Make a union of the two union-find trees containing (i, j) and (k, l).
  3. Repeat so long as the current path can be extended:
    Rename (i, j) = (k, l).
    Locate a random neighboring square (k, l) of (i, j) such that:
    (a) (k, l) does not belong to any path generated so far (including the current one)
    (b) the only neighbor (k, l) has on the partially constructed current path is (i, j).
  4. If no such neighbor (k, l) can be found, the path cannot be extended further, so break the loop
  5. Otherwise, make the union of the two union-find trees to which (i, j) and (k, l) belong.
  6. Set the endpoint flags of the two squares that are at the beginning and at the end of the new path.
  7. Return SUCCESS

Complete Running Code of the Article.



Next Article
Implementation of Minesweeper Game

V

Vaibhav Aggarwal
Improve
Article Tags :
  • Project

Similar Reads

  • Random Number Memory Game in C
    In this article, will design a simple number memory game in the C programming language. It is a simple memory number game where a random number is displayed and is hidden after some time. The task is to guess that displayed number to continue the game. How to Play This Game: Press 1 on your keyboard
    2 min read
  • Hangman Game in C++
    With friends, many of us had played the hangman game where one player gave a hint to the other to make a correct guess of a particular thing. Here a simple representation of the game 'Hangman' is done using C++ programming language. The player will guess the fruit's name and the Computer will give a
    6 min read
  • Implementation of Minesweeper Game
    Remember the old Minesweeper? We play on a square board and we have to click on the board on the cells which do not have a mine. And obviously, we don’t know where mines are. If a cell where a mine is present is clicked then we lose, else we are still in the game. There are three levels for this gam
    15+ min read
  • Pin Ball Game using HTML CSS & JavaScript
    Pinball is a classic arcade game that has been around since the 1930s. It involves the player using flippers to hit a ball around a table aiming to score points by hitting various targets and obstacles. PrerequisitesHTMLCSSJavaScriptApproachThe pinball game operates by moving a ball within a confine
    4 min read
  • Project Idea | LinkBook
    LinkBook aims to provide users a platform to store, manage, share and discover web URLs. Motivation Facebook is one way to share links with one's friends, however, it does not provide an option to manage those links for future reference. LinkBook provides the users with the option to store their lin
    2 min read
  • Project Idea | (LinkBook)
    LinkBook aims to provide users a platform to store, manage, share and discover web URLs. Motivation Facebook is one way to share links with one's friends, however, it does not provide an option to manage those links for future reference. LinkBook provides the users with the option to store their lin
    2 min read
  • Project Idea | Breakout game in Python
    Project Title: Breakout game in Python Introduction: The project 'Breakout game in Python' is proposed to recreate a classical game of Breakout by using the Python language. It is meant to be a fully functional game with scores, lives, game over, and high score management. The high scores can also b
    2 min read
  • Number Guessing Game Using Python Streamlit Library
    The game will pick a number randomly from 1 to 100 and the player needs to guess the exact number guessed by calculating the hint. The player will get 7 chances to guess the number and for every wrong guess game will tell you if the number is more or less or you can take a random hint to guess the n
    5 min read
  • Twenty-one Game using MEAN Stack
    Twenty-one Game is a popular card game played in casinos worldwide. In this article, we'll explore how to create a Twenty-one game also known as the BlackJack game using the MEAN stack (MongoDB, Express.js, Angular, and Node.js). By building a Twenty-one game, we'll see the various aspects of web de
    8 min read
  • Create Bingo Game Using Java
    Bingo, a game of enduring appeal characterized by chance and anticipation, has consistently captivated players across generations. With its uncomplicated rules and element of unexpected delight, Bingo emerges as an ideal selection for diverse occasions. If your curiosity leads you into the realm of
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences