using System;
using System.Collections.Generic;
class pair {
public int first;
public int second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
class GFG {
static int INF = 99999999;
static int MAXR = 12;
static int MAXC = 12;
static int MAXMASK = 2048;
static int MAXHOUSE = 12;
static int[][][] dist = new int[MAXR][][];
static int[, ] dp = new int[MAXHOUSE, MAXMASK];
static List<pair> dirty = new List<pair>();
static int[] X = { -1, 0, 0, 1 };
static int[] Y = { 0, 1, -1, 0 };
static char[, ] arr = new char[21, 21];
static int len, limit, r, c;
public static bool safe(int x, int y)
{
if (x >= r || y >= c || x < 0 || y < 0)
return false;
if (arr[x, y] == '#')
return false;
return true;
}
public static void getDist(int idx)
{
bool[, ] vis = new bool[21, 21];
int cx = dirty[idx].first;
int cy = dirty[idx].second;
Queue<pair> pq = new Queue<pair>();
pq.Enqueue(new pair(cx, cy));
for (int i = 0; i <= r; i++)
for (int j = 0; j <= c; j++)
dist[i][j][idx] = INF;
vis[cx, cy] = true;
dist[cx][cy][idx] = 0;
while (pq.Count > 0) {
pair x = pq.Peek();
pq.Dequeue();
for (int i = 0; i < 4; i++) {
cx = x.first + X[i];
cy = x.second + Y[i];
if (safe(cx, cy)) {
if (vis[cx, cy])
continue;
vis[cx, cy] = true;
dist[cx][cy][idx]
= dist[x.first][x.second][idx] + 1;
pq.Enqueue(new pair(cx, cy));
}
}
}
}
public static int solve(int idx, int mask)
{
if (mask == limit)
return dist[0][0][idx];
if (dp[idx, mask] != -1)
return dp[idx, mask];
int ret = INF;
for (int i = 0; i < len; i++) {
if ((mask & (1 << i)) == 0) {
int newMask = mask | (1 << i);
ret = Math.Min(
ret, solve(i, newMask)
+ dist[dirty[i].first]
[dirty[i].second][idx]);
}
}
return dp[idx, mask] = ret;
}
public static void init()
{
for (int i = 0; i < MAXR; i++) {
dist[i] = new int[MAXC][];
for (int j = 0; j < MAXC; j++) {
dist[i][j] = new int[MAXHOUSE];
}
}
for (int i = 0; i < MAXHOUSE; i++) {
for (int j = 0; j < MAXMASK; j++) {
dp[i, j] = -1;
}
}
dirty.Clear();
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (arr[i, j] == '*')
dirty.Add(new pair(i, j));
}
}
dirty.Insert(0, new pair(0, 0));
len = dirty.Count;
limit = (1 << len) - 1;
for (int i = 0; i < len; i++)
getDist(i);
}
public static void Main(string[] args)
{
char[][] A = new char[4][] {
new char[] { '.', '.', '.', '.', '.', '*',
'.' },
new char[] { '.', '.', '.', '#', '.', '.',
'.' },
new char[] { '.', '*', '.', '#', '.', '*',
'.' },
new char[] { '.', '.', '.', '.', '.', '.', '.' }
};
r = 4;
c = 7;
Console.WriteLine("The given grid : ");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
Console.Write(A[i][j] + " ");
arr[i, j] = A[i][j];
}
Console.WriteLine();
}
init();
int ans = solve(0, 1);
Console.Write(
"Minimum distance for the given grid : ");
Console.WriteLine(ans);
char[][] Arr = new char[5][] {
new char[] { '.', '.', '.', '#', '.', '.',
'.' },
new char[] { '.', '.', '.', '#', '.', '*',
'.' },
new char[] { '.', '.', '.', '#', '.', '.',
'.' },
new char[] { '.', '*', '.', '#', '.', '*',
'.' },
new char[] { '.', '.', '.', '#', '.', '.', '.' }
};
r = 5;
c = 7;
Console.WriteLine("The given grid: ");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
Console.Write(Arr[i][j] + " ");
arr[i, j] = Arr[i][j];
}
Console.WriteLine();
}
init();
ans = solve(0, 1);
Console.Write(
"Minimum distance for the given grid: ");
if (ans >= INF)
Console.WriteLine("not possible");
else
Console.WriteLine(ans);
}
}