Comments (2)
@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.
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)
- A few humble suggestions
- Chapter 03: Typo at lines 90-91 HOT 1
- sheduling HOT 1
- Updated pdf HOT 2
- Citation of this book HOT 1
- Chapter 13: Bellman-Ford negative cycle example
- Is this book available in print? HOT 2
- Update PDF link on https://cses.fi/book/index.php
- Time complexity to generate table for logK lookup of K succesor of node n HOT 1
- Search Code on Page 47 is unnecessarily confusing
- Chapter 15: Minor Typo in Footnote HOT 1
- Update definition of "Maximum subarray problem"
- Can't compile with `latexmk`
- Bellman-Ford example has a typo HOT 1
- Errors in Chapter 13 HOT 1
- 21.3 Logic needs an explanation
- [Suggestion] Add a 5th number of the arithmetic sum example
- Since this book is paired with cses.fi, more string algorithms should be mentioned (Aho Corasick, suffix automaton, KMP)
- Bubble sort code typo
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cphb.