How to compare 3 dimensional arrays within O(0)?

71 Views Asked by At

So I tried to make a solving algorithm for a rubiks cube that makes use of a 3 dimensional array however since a 3 dimensional array's values can not be compared to another array's values, my if statement always return False.

My code is currently this however the debug part always returns false as it can not correctly be compared. This will also run aprox 2*27^10 times so the comparison has to be fast. And using a list and a for loop wont work fast enough for this.

Current code:

public void Solving(int[,,] Tbs) // 3d array cube that has to be solved
{
    bool found = false;


   List<char> movements = new() {'F','U','L', 'R','D', 'B' };
    Queue<int[,,]> activeFromSramble = new();
    Queue<int[,,]> activeFromSolved = new();

    if (DefaultCube() == DefaultCube()) // for debug purposes defaultcube has all have the same values inside
        Console.WriteLine("Works");
    else
    {

        Console.WriteLine("Broken");
    }
    Dictionary<object, object> fromSC = new();
    Dictionary<int[,,], int[,,]> fromSOL = new();
    activeFromSramble.Enqueue(Tbs);
    activeFromSolved.Enqueue(DefaultCube());
    fromSC.Add(Tbs, Tbs);
    fromSOL.Add(DefaultCube(),DefaultCube());
    while (found == false)
    {
        Console.WriteLine("It");
        int[,,] oldCube = activeFromSramble.Dequeue();
        if (fromSOL.ContainsKey(oldCube))
        {
            found = true;
                        
            Console.WriteLine("FOUND");
            
        }
        foreach (char c in movements)
        {
            
                for (int i = 1; i < 4; i++)
                {
                    int[,,] newCube = PermCube(oldCube, c, i);
                    if (fromSC.ContainsKey(newCube)) // of hij in eigen lijst staat met key
                        continue;

                fromSC.Add(newCube, oldCube); 
                    activeFromSramble.Enqueue(newCube);
                   
                }
        }
        
        oldCube = activeFromSolved.Dequeue();
        if (fromSC.ContainsKey(oldCube))
        {
            found = true;
            Console.WriteLine("FOUND");
                        
        }
        foreach (char c in movements)
        {
            for (int i = 1; i < 4; i++)
            {

                    int[,,] newCube = PermCube(oldCube, c, i);
                    if (fromSOL.ContainsKey(newCube))
                    {
                        continue;
                    }
                    
                    {
                        activeFromSolved.Enqueue(newCube);
                        fromSOL.Add(newCube, oldCube);
                    }
            }
        }

    }


    
}



1

There are 1 best solutions below

2
On

You cannot compare the data in variable sized arrays in constant time. Some possible approaches:

  1. Use a hash. This lets you amortize the time to generate the hash over many comparisons. Use a strong hash that has a negligible chance of collisions. Consider using an immutable data structure to ensure the hash always matches the data.
  2. Use another data representation. Figure out the minimum amounts of bits you actually need, and store these as compact as possible. This should reduce the comparison time as much as possible.
  3. Use a fast hash, but fallback to checking the actual data if the hashes match.