Giter Site home page Giter Site logo

Comments (2)

dhern023 avatar dhern023 commented on September 28, 2024

@technusm1 You're almost there, but this code returns 0 for even valued n. You can see it for yourself if you print out the matrix and set each visited assignment to visited cells count.

void coutMatrix(std::vector< std::vector<int>> matrix, int rows, int cols)
{
    for (int i < 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            if (j != 0)
            {
                std::cout << "\t";
            }
            std::cout << matrix[i][j];
        }
        std::cout << "\n";
    }
}

//adjust these lines
        // Flip these two
	visited_cells_count += 1; // ------------------can be over n*n for even valued
	visited[sx][sy] = visited_cells_count; //------------------ optional to see the path (1 -> n*n)
	if (sx == ex && sy == ey) {
                coutMatrix(visited, n, n); // --------------- optional to see the path (1 -> n*n)
		if (visited_cells_count == n*n) {
			// The starting and ending points are same, and we have traversed all cells of the grid, so this counts as 1 path
			result = 1;
		} else {
			// Optimization 2: If all elements are not covered (i.e. visited_cells_count != n*n), the path is not valid, so we return 0
			result = 0;
		}
	}	

from cphb.

dhern023 avatar dhern023 commented on September 28, 2024

By the way, this problem is a subset of a larger problem, which is finding all the Hamiltonian paths on a grid graph.

from cphb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.