using System;
using System.Collections;
class GFG{
static int ROW = 3;
static int COL = 3;
static int[] dRow = { 0, 1, 0, -1 };
static int[] dCol = { -1, 0, 1, 0 };
static bool isValid(bool[,] vis, int row, int col)
{
if (row < 0 || col < 0 ||
row >= ROW || col >= COL)
return false;
if (vis[row,col])
return false;
return true;
}
static void DFS(int row, int col,
int[,] grid, bool[,] vis)
{
Stack st = new Stack();
st.Push(new Tuple<int, int>(row, col));
while (st.Count > 0)
{
Tuple<int, int> curr = (Tuple<int, int>)st.Peek();
st.Pop();
row = curr.Item1;
col = curr.Item2;
if (!isValid(vis, row, col))
continue;
vis[row, col] = true;
Console.Write(grid[row, col] + " ");
for(int i = 0; i < 4; i++)
{
int adjx = row + dRow[i];
int adjy = col + dCol[i];
st.Push(new Tuple<int, int>(adjx, adjy));
}
}
}
static void Main()
{
int[,] grid = { { -1, 2, 3 },
{ 0, 9, 8 },
{ 1, 0, 1 } };
bool[,] vis = new bool[ROW, COL];
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
vis[i, j] = false;
}
}
DFS(0, 0, grid, vis);
}
}