Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int sizeM = 5;
- int matrix[sizeM][sizeM] {
- 0, 1, 0, 0, 0,
- 0, 1, 0, 0, 0,
- 0, 1, 0, 0, 0,
- 0, 0, 0, 0, 0,
- 0, 1, 0, 0, 0
- };
- void printMatrix(){
- cout << "==============================" << endl;
- for (int i = 0; i < sizeM; i++)
- {
- for (int j = 0; j < sizeM; j++)
- {
- cout << matrix[i][j] << " ";
- }
- cout << endl;
- }
- cout << "==============================" << endl;
- }
- void visit(int x, int y){
- matrix[x][y] = 2;
- }
- bool canVisit(int x, int y){
- return x < sizeM
- && y < sizeM
- && matrix[x][y] == 0;
- }
- bool findPath(int cX, int cY, int eX, int eY){
- if (cX == eX && cY == eY)
- {
- return true;
- }
- visit(cX, cY);
- if (canVisit(cX+1, cY) && findPath(cX+1, cY, eX, eY))
- {
- return true;
- }
- if (canVisit(cX, cY+1) && findPath(cX, cY+1, eX, eY)){
- return true;
- }
- return false;
- }
- int main(){
- printMatrix();
- cout << "Is there a path? " << findPath(0, 0, 4, 2) << endl;
- printMatrix();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement