Code in C++:
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
void check(int i,int j);
int main()
{
int n;
cin >> n;
int a[n][n];
int b[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = 0;
}
}
b[0][0] = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; i++)
{
cin >> a[i][j];
}
}
check(0, 0);
if (b[n - 1][n - 1] == 1)
cout << "yesssss";
else
cout << "no!!!!";
}
void check(int i, int j)
{
if((i - 1) >= 0 &&
(i - 1) <= (n - 1) &&
(abs(a[i][j] - a[i - 1][j]) >= 10) &&
b[i - 1][j] == 0)
{
b[i - 1][j] = 1;
check(i - 1, j);
}
if((i + 1) >= 0 &&
(i + 1) <= (n-1) &&
(abs(a[i][j] - a[i + 1][j]) >= 10) &&
b[i + 1][j] == 0)
{
b[i + 1][j] = 1;
check(i + 1, j);
}
if((j + 1) >= 0 &&
(j + 1) <= (n - 1) &&
(abs(a[i][j] - a[i][j + 1]) >= 10) &&
b[i][j + 1] == 0)
{
b[i][j + 1] = 1;
check(i, j + 1);
}
if((j - 1) >= 0 &&
(j-1) <= (n - 1) &&
(abs(a[i][j] - a[i][j - 1]) >= 10) &&
b[i][j - 1] == 0)
{
b[i][j - 1] = 1;
check(i, j - 1);
}
}
My code has a function named check
inside which integer variable n
and arrays a
and b
. I want them to be made available to this function. If i declare global variable, then I can't use cin
outside the main
function.
How can I make these variables available to my check
function?