fix: new faster heuristic used in solution

This commit is contained in:
2026-05-04 15:56:45 -03:00
parent 03a409f861
commit 6acc6e738d

View File

@@ -1,7 +1,7 @@
#include <bits/stdc++.h>
#define endl '\n'
#define INF 0x3f
#define INF 0x3f3f3f3f
using namespace std;
@@ -11,7 +11,6 @@ const int MAXN = 1e3 + 10;
int C, L, W;
int Cr, Lr, Cs, Ls;
int alagado[MAXN][MAXN];
const int COSTS[5][5] = {
{7, 6, 5, 6, 7},
{6, 3, 2, 3, 6},
@@ -19,67 +18,47 @@ const int COSTS[5][5] = {
{6, 3, 2, 3, 6},
{7, 6, 5, 6, 7},
};
int vis[MAXN][MAXN];
bitset<MAXN> alagado[MAXN];
int g[MAXN][MAXN];
bool valid(int c, int l) {
return c > 0 && c <= C && l > 0 && l <= L && alagado[c][l] == 0;
}
int h(int c, int l)
{
return sqrt(abs(Cs - c)*abs(Cs - c) + abs(Ls - l)*abs(Ls - l));
}
int h(int c, int l) {
int dx = abs(Cs - c);
int dy = abs(Ls - l);
bool reachable()
{
queue<pii> q;
memset(vis, 0, sizeof(vis));
int diag = min(dx, dy);
int straight = max(dx, dy) - diag;
q.push({Cr, Lr});
vis[Cr][Lr] = 1;
while (!q.empty())
{
auto [c, l] = q.front();
q.pop();
if (c == Cs && l == Ls) return true;
for (int i = -2; i <= 2; i++){
for (int j = -2; j <= 2; j++){
int nc = c + i, nl = l + j;
if (!valid(nc, nl) || vis[nc][nl]) continue;
vis[nc][nl] = 1;
q.push({nc, nl});
}
}
}
return false;
return diag * 3 + straight * 2;
}
void solve()
{
if (!reachable())
{
cout << "impossible" << endl;
return;
for (int i = 1; i <= C; i++){
for (int j = 1; j <= L; j++){
g[i][j] = INF;
}
}
memset(vis, 0, sizeof(vis));
memset(g, INF, sizeof(g));
priority_queue<pair<int, pii>> pq;
priority_queue<
pair<int, pii>,
vector<pair<int, pii>>,
greater<>
> pq;
g[Cr][Lr] = 0;
pq.push({-h(Cr, Lr), {Cr, Lr}});
pq.push({h(Cr, Lr), {Cr, Lr}});
while (!pq.empty()){
auto [curCost, curCoords] = pq.top();
pq.pop();
auto [c, l] = curCoords;
if (vis[c][l]) continue;
vis[c][l] = 1;
if (curCost != g[c][l] + h(c,l)) continue;
if (c == Cs && l == Ls){
cout << g[c][l] << endl;
@@ -91,17 +70,18 @@ void solve()
int nc = c + i, nl = l + j;
if (!valid(nc, nl)) continue;
int cost = COSTS[i + 2][j + 2];
int cost = COSTS[i+2][j+2];
int newG = g[c][l] + cost;
if (newG < g[nc][nl]){
g[nc][nl] = newG;
int f = g[nc][nl] + h(nc, nl);
// insere custo negativo pois por padrao a priority queue é uma max heap
pq.push({-f, {nc, nl}});
pq.push({newG + h(nc, nl), {nc, nl}});
}
}
}
}
cout << "impossible" << endl;
}
int main()
@@ -110,11 +90,13 @@ int main()
cin.tie(nullptr);
while (cin >> C >> L, C && L){
cin >> Cr >> Lr;
cin >> Cs >> Ls;
cin >> Cr >> Lr >> Cs >> Ls;
for (int i = 1; i <= C; i++){
for (int j = 1; j <= L; j++){
alagado[i][j]=0;
}
}
cin >> W;
memset(alagado, 0, sizeof(alagado));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < W; i++){
int C1, C2, L1, L2;
cin >> C1 >> L1 >> C2 >> L2;