#include <climits>
#include <cmath>
#include <cstdio>
#include <memory>
using namespace std;
struct Out //to store maxvertical distance and time taken to reach goal
{
int maxup, res;
Out(int x, int y) : maxup(x), res(y) {}
Out() : maxup(0), res(0) {}
};
int n, a[20][20];
shared_ptr<Out> dp[20][20];
shared_ptr<Out> solve(int, int);
int main()
{
int t, T, time;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &T);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%d", *(a+i)+j);
dp[i][j] = nullptr;
}
shared_ptr<Out> o(solve(0, 0));
//adjustments for initial jump on array(0,0)
if ((time = a[0][0] + o->res) < T) printf("YES : %d %d\n", a[0][0] > o->maxup? a[0][0] : o->maxup , T-time);
else printf("NO\n");
}
}
shared_ptr<Out> solve(int i, int j)
{
if (dp[i][j]) return dp[i][j];
if (i == n-1 && j == n-1) return dp[i][j] = shared_ptr<Out>(new Out());
int rdiff, ddiff, maxupr = INT_MAX, maxupd = INT_MAX;
shared_ptr<Out> r, d;
if (i+1 < n) {
r = solve(i+1, j);
rdiff = abs(a[i+1][j]-a[i][j]);
maxupr = rdiff > r->maxup ? rdiff : r->maxup;
r = shared_ptr<Out>(new Out(maxupr, rdiff+r->res));
}
if (j+1 < n) {
d = solve(i, j+1);
ddiff = abs(a[i][j+1]-a[i][j]);
maxupd = ddiff > d->maxup ? ddiff : d->maxup;
d = shared_ptr<Out>(new Out(maxupd, ddiff+d->res));
}
//priority is to minimize the vertical distance covered in a single down/right first
if (maxupr < maxupd) return dp[i][j] = r;
if (maxupr > maxupd) return dp[i][j] = d;
return dp[i][j] = r->res < d->res? r : d;
}
Problem Link: http://ift.tt/29cgjFE
IDEOne: http://ift.tt/29AXAT5
Also I would like to add that the problem is not well formed and there are complains about constraints, directions to move and even wrong solutions getting accepted for the problem.
The constraint on size of the 2-D array is defined but there is no constraint on the size of the array elements. Nor there is any constraint on T the total time that Batman has.
Furthermore some test cases can only be passed by considering left directions as well on each cell. For example this one:
1
5 20
1 2 3 4 5
1000 1000 1000 1000 6
11 10 9 8 7
12 1000 1000 1000 1000
13 14 15 16 17
The answer (YES : 1 3) would require movement in the left direction as well. If we can only move down and right, the answer would be NO. Still there are people claiming to have an AC with code handling only these directions. Furthermore, one user is reporting that he got accepted with a wrong solution to one test case (for which my code prints correctly). This has led me to conclude that there is a fair chance of the test cases being wrong/weak.
However, I'm asking this question so that I must get an expert view on my code and assurances if it is correct (criticism if it is not).
Aucun commentaire:
Enregistrer un commentaire