From 6acc6e738d1a4efa219bc735b5d84d143e265c66 Mon Sep 17 00:00:00 2001 From: arthur Date: Mon, 4 May 2026 15:56:45 -0300 Subject: [PATCH] fix: new faster heuristic used in solution --- ra-preguicosa/src/ac.cpp | 92 ++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 55 deletions(-) diff --git a/ra-preguicosa/src/ac.cpp b/ra-preguicosa/src/ac.cpp index 6260937..8b008ae 100644 --- a/ra-preguicosa/src/ac.cpp +++ b/ra-preguicosa/src/ac.cpp @@ -1,7 +1,7 @@ #include #define endl '\n' -#define INF 0x3f +#define INF 0x3f3f3f3f using namespace std; @@ -11,75 +11,54 @@ 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}, - {5, 2, 0, 2, 5}, - {6, 3, 2, 3, 6}, - {7, 6, 5, 6, 7}, + {7, 6, 5, 6, 7}, + {6, 3, 2, 3, 6}, + {5, 2, 0, 2, 5}, + {6, 3, 2, 3, 6}, + {7, 6, 5, 6, 7}, }; -int vis[MAXN][MAXN]; +bitset 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 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> pq; + priority_queue< + pair, + vector>, + 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;