Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 11487 – Gathering Food
Source : toph.co
Category : Graph Theory
Algorithm : Dijkstra Algorithm, Dynamic Programing, Path Counting, 2D
Verdict : Accepted
#include "bits/stdc++.h"
using namespace std;
#define FI freopen("in.txt", "r", stdin)
#define FO freopen("out.txt", "w", stdout)
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FOR(i, n) for (int i = 1; i <= n; i++)
#define For(i, n) for (int i = 0; i < n; i++)
#define ROF(i, n) for (int i = n; i >= 1; i--)
#define Rof(i, n) for (int i = n-1; i >= 0; i--)
#define FORI(i, n) for (auto i : n)
#define ll long long
#define ull unsigned long long
#define vi vector <int>
#define vl vector <ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define mk make_pair
#define ff first
#define ss second
#define eb emplace_back
#define em emplace
#define pb push_back
#define ppb pop_back
#define All(a) a.begin(), a.end()
#define memo(a, b) memset(a, b, sizeof a)
#define Sort(a) sort(All(a))
#define ED(a) Sort(a), a.erase(unique(All(a)), a.end())
#define rev(a) reverse(All(a))
#define sz(a) (int)a.size()
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define maxAll(a) *max_element(All(a))
#define minAll(a) *min_element(All(a))
#define allUpper(a) transform(All(a), a.begin(), :: toupper)
#define allLower(a) transform(All(a), a.begin(), :: tolower)
#define endl '\n'
#define nl puts("")
#define ub upper_bound
#define lb lower_bound
#define Exp exp(1.0)
#define PIE 2*acos(0.0)
#define Sin(a) sin(((a)*PIE)/180.0)
#define EPS 1e-9
int dr[] = {1, -1, 0, 0}; // 4 Direction
int dc[] = {0, 0, 1, -1};
// int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction
// int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};
// int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves
// int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};
inline int setbit(int mask, int pos) { return mask |= (1 << pos); }
inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); }
inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); }
inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); }
#define popcount(mask) __builtin_popcount(mask) // count set bit
#define popcountLL(mask) __builtin_popcountll(mask) // for long long
inline int read() { int a; scanf("%d", &a); return a; }
inline ll readLL() { ll a; scanf("%lld", &a); return a; }
inline double readDD() { double a; scanf("%lf", &a); return a; }
template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; }
ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; }
#define inf 12345
#define mod 20437
static const int maxn = 10 + 5;
static const int logn = 18;
struct node
{
int x, y, d;
node(int x = 0, int y = 0, int d = 0) : x(x), y(y), d(d) {}
inline bool operator < (const node &p) const
{
return d > p.d;
}
};
int row, column;
char grid[maxn][maxn];
int dist[maxn][maxn];
inline bool inside(int r, int c)
{
return r >= 1 && r <= row && c >= 1 && c <= column;
}
inline int dijkstra(int sx, int sy, int ex, int ey)
{
FOR(i, row) FOR(j, column) dist[i][j] = inf;
char src = grid[sx][sy];
char des = grid[ex][ey];
priority_queue <node> PQ;
PQ.emplace(sx, sy, 0);
dist[sx][sy] = 0;
while (!PQ.empty())
{
int x = PQ.top().x;
int y = PQ.top().y; PQ.pop();
for (int i = 0; i < 4; i++)
{
int xx = x + dr[i];
int yy = y + dc[i];
if (!inside(xx, yy) || grid[xx][yy] == '#') continue;
if (grid[xx][yy] == '.' || (grid[xx][yy] == src || grid[xx][yy] <= des))
{
if (dist[xx][yy] > dist[x][y] + 1)
{
dist[xx][yy] = dist[x][y] + 1;
PQ.emplace(xx, yy, dist[xx][yy]);
}
}
}
}
return dist[ex][ey];
}
int dp[maxn][maxn];
bool memoize[maxn][maxn];
inline int countPath(int sx, int sy, int ex, int ey)
{
if (sx == ex && sy == ey) return 1;
int &ret = dp[sx][sy];
bool &mem = memoize[sx][sy];
if (mem) return ret;
mem = 1;
int sum = 0;
for (int i = 0; i < 4; i++)
{
int x = sx + dr[i];
int y = sy + dc[i];
if (inside(x, y) && grid[x][y] != '#' && dist[x][y] == dist[sx][sy] + 1)
sum += countPath(x, y, ex, ey);
if (sum > mod) sum -= mod;
}
return ret = sum;
}
vector <node> food;
inline pii getAns()
{
int minMove = 0;
int totalPath = 1;
int len = sz(food);
for (int i = 0; i < len-1; i++)
{
int mini = dijkstra(food[i].x, food[i].y, food[i+1].x, food[i+1].y);
if (mini == inf)
{
return pii(-1, -1);
}
minMove += mini;
memo(memoize, 0);
int path = countPath(food[i].x, food[i].y, food[i+1].x, food[i+1].y);
path %= mod;
totalPath = (totalPath * path) % mod;
}
return pii(minMove, totalPath);
}
int main()
{
//FI;
FAST;
int N, tcase = 1;
while (cin >> N)
{
if (N == 0) break;
row = column = N;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= column; j++)
{
cin >> grid[i][j];
if (grid[i][j] != '.' && grid[i][j] != '#')
{
food.eb(i, j, grid[i][j] - 'A' + 1);
}
}
}
Sort(food);
rev(food);
pii ans = getAns();
if (ans.ff == -1) cout << "Case " << tcase++ << ": Impossible\n";
else cout << "Case " << tcase++ << ": " << ans.ff << " " << ans.ss << endl;
food.clear();
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.