Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3635626
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:55:40+00:00 2026-05-19T00:55:40+00:00

I need do find a cycle beginning and ending at given point. It is

  • 0

I need do find a cycle beginning and ending at given point. It is not guaranteed that it exists.
I use bool[,] points to indicate which point can be in cycle. Poins can be only on grid. points indicates if given point on grid can be in cycle.
I need to find this cycle using as minimum number of points.
One point can be used only once.
Connection can be only vertical or horizontal.

Let this be our points (red is starting point):

removing dead ImageShack links

I realized that I can do this:

while(numberOfPointsChanged)
{
    //remove points that are alone in row or column
}

So i have:

removing dead ImageShack links

Now, I can find the path.

removing dead ImageShack links

But what if there are points that are not deleted by this loop but should not be in path?

I have written code:

class MyPoint
{
    public int X { get; set; }
    public int Y { get; set; }
    public List<MyPoint> Neighbours = new List<MyPoint>();
    public MyPoint parent = null;
    public bool marked = false;
}

    private static MyPoint LoopSearch2(bool[,] mask, int supIndexStart, int recIndexStart)
    {
        List<MyPoint> points = new List<MyPoint>();

        //here begins translation bool[,] to list of points
        points.Add(new MyPoint { X = recIndexStart, Y = supIndexStart });

        for (int i = 0; i < mask.GetLength(0); i++)
        {
            for (int j = 0; j < mask.GetLength(1); j++)
            {
                if (mask[i, j])
                {
                    points.Add(new MyPoint { X = j, Y = i });
                }
            }
        }

        for (int i = 0; i < points.Count; i++)
        {
            for (int j = 0; j < points.Count; j++)
            {
                if (i != j)
                {
                    if (points[i].X == points[j].X || points[i].Y == points[j].Y)
                    {
                        points[i].Neighbours.Add(points[j]);
                    }
                }
            }
        }
        //end of translating

        List<MyPoint> queue = new List<MyPoint>();
        MyPoint start = (points[0]); //beginning point
        start.marked = true; //it is marked
        MyPoint last=null;   //last point. this will be returned
        queue.Add(points[0]);


        while(queue.Count>0)
        {
            MyPoint current = queue.First(); //taking point from queue
            queue.Remove(current);           //removing it

            foreach(MyPoint neighbour in current.Neighbours) //checking Neighbours
            {
                if (!neighbour.marked) //in neighbour isn't marked adding it to queue
                {
                    neighbour.marked = true;
                    neighbour.parent = current;
                    queue.Add(neighbour);
                }
                //if neighbour is marked checking if it is startig point and if neighbour's parent is current point. if it is not that means that loop already got here so we start searching parents to got to starting point
                else if(!neighbour.Equals(start) && !neighbour.parent.Equals(current))
                {                        
                    current = neighbour;
                    while(true)
                    {
                        if (current.parent.Equals(start))
                        {
                            last = current;
                            break;
                        }
                        else
                            current = current.parent;

                    }
                    break;
                }
            }
        }

        return last;            
    }

But it doesn’t work. The path it founds contains two points: start and it’s first neighbour.
What am I doing wrong?

EDIT:
Forgot to mention… After horizontal connection there has to be vertical, horizontal, vertical and so on…
What is more in each row and column there need to be max two points (two or none) that are in the cycle. But this condition is the same as “The cycle has to be the shortest one”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:55:40+00:00Added an answer on May 19, 2026 at 12:55 am

    That is what I have done. I don’t know if it is optimised but it does work correctly. I have not done the sorting of the points as @marcog suggested.

    private static bool LoopSearch2(bool[,] mask, int supIndexStart, int recIndexStart, out List<MyPoint> path)
        {
            List<MyPoint> points = new List<MyPoint>();
            points.Add(new MyPoint { X = recIndexStart, Y = supIndexStart });
    
            for (int i = 0; i < mask.GetLength(0); i++)
            {
                for (int j = 0; j < mask.GetLength(1); j++)
                {
                    if (mask[i, j])
                    {
                        points.Add(new MyPoint { X = j, Y = i });
                    }
                }
            }
    
            for (int i = 0; i < points.Count; i++)
            {
                for (int j = 0; j < points.Count; j++)
                {
                    if (i != j)
                    {
                        if (points[i].X == points[j].X || points[i].Y == points[j].Y)
                        {
                            points[i].Neighbours.Add(points[j]);
                        }
                    }
                }
            }
    
            List<MyPoint> queue = new List<MyPoint>();
            MyPoint start = (points[0]);
            start.marked = true;
            queue.Add(points[0]);
    
            path = new List<MyPoint>();
    
            bool found = false;
    
            while(queue.Count>0)
            {
                MyPoint current = queue.First();
                queue.Remove(current);
    
                foreach (MyPoint neighbour in current.Neighbours)
                {
                    if (!neighbour.marked)
                    {
                        neighbour.marked = true;
                        neighbour.parent = current;
                        queue.Add(neighbour);
                    }
                    else
                    {
                        if (neighbour.parent != null && neighbour.parent.Equals(current))
                            continue;
    
                        if (current.parent == null)
                            continue;
    
                        bool previousConnectionHorizontal = current.parent.Y == current.Y;
                        bool currentConnectionHorizontal = current.Y == neighbour.Y;
    
                        if (previousConnectionHorizontal != currentConnectionHorizontal)
                        {
                            MyPoint prev = current;
    
                            while (true)
                            {
                                path.Add(prev);
                                if (prev.Equals(start))
                                    break;
                                prev = prev.parent;
                            }
    
                            path.Reverse();
    
                            prev = neighbour;
    
                            while (true)
                            {
                                if (prev.Equals(start))
                                    break;
                                path.Add(prev);                                
                                prev = prev.parent;
                            }
    
                            found = true;
                            break;
                        }                      
                    }
                    if (found) break;
                }
                if (found) break;
            }
    
            if (path.Count == 0)
            {
                path = null;
                return false;
            }
            return true;   
        }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find/create an application that will create employee web usage reports from
I need to find out how to format numbers as strings. My code is
I need to find a bottleneck and need to accurately as possible measure time.
I need to find the PID of the current running process on a Linux
I need to find occurrences of (+) in my sql scripts, (i.e., Oracle outer
I need to find a way to crawl one of our company's web applications
I need to find out the time a function takes for computing the performance
I need to find occurrences of ~ 25 000 words within a text. What
I need to find all occurrences of a function call in a C++ file
I need to find out what ports are attached to which processes on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.