View Code of Problem 3499

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <math.h>
#include <vector>
using namespace std;
const int N = 5;
int visit(int, int);
int maze[N][N] = {0};
int startI = 0, startJ = 0;
int endI = 4, endJ = 4;
int success = 0;

struct point
{
    int x;
    int y;
};
vector<point> result;
int main(void)
{
    int i, j;

    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            cin >> maze[i][j];
        }
    }

    if (visit(startI, startJ) == 0)
    {
        /* code */
        cout << "Not Found\n"
             << endl;
    }
    else
    {
        for (int i = 0; i < result.size(); i++)
        {
            struct point p = result.at(i);
            cout << "(" << p.x << "," << p.y << ")" << endl;
        }
    }
    system("pause");
    return 0;
}
int visit(int i, int j)
{
    if (i < 0 || i >= N || j < 0 || j >= N)
    {
        /* code */
        return 0;
    }

    maze[i][j] = 1;
    struct point p;
    p.x = i;
    p.y = j;
    result.push_back(p);

    if (i == endI && j == endJ)
    {
        success = 1;
    }

    if (success != 1 && maze[i][j + 1] == 0)
    {

        visit(i, j + 1);
    }
    if (success != 1 && maze[i + 1][j] == 0)
    {

        visit(i + 1, j);
    }
    if (success != 1 && maze[i][j - 1] == 0)
    {
        visit(i, j - 1);
    }
    if (success != 1 && maze[i - 1][j] == 0)
    {

        visit(i - 1, j);
    }
    if (success != 1)
    {
        result.pop_back();
        maze[i][j] = 0;
    }

    return success;
}

Double click to view unformatted code.


Back to problem 3499