From 3cfeb9a032380e54cecb6102132f581f99f623c2 Mon Sep 17 00:00:00 2001 From: arthur Date: Sat, 2 May 2026 12:34:17 -0300 Subject: [PATCH] feat: new solution code. --- ra-preguicosa/src/ac.cpp | 122 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 1 deletion(-) diff --git a/ra-preguicosa/src/ac.cpp b/ra-preguicosa/src/ac.cpp index 0b2cb0b..6260937 100644 --- a/ra-preguicosa/src/ac.cpp +++ b/ra-preguicosa/src/ac.cpp @@ -1,8 +1,128 @@ #include +#define endl '\n' +#define INF 0x3f + using namespace std; +typedef pair pii; -int main(){ +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}, +}; +int vis[MAXN][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)); +} + +bool reachable() +{ + queue q; + memset(vis, 0, sizeof(vis)); + + 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; +} + +void solve() +{ + if (!reachable()) + { + cout << "impossible" << endl; + return; + } + + memset(vis, 0, sizeof(vis)); + memset(g, INF, sizeof(g)); + priority_queue> pq; + g[Cr][Lr] = 0; + 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 (c == Cs && l == Ls){ + cout << g[c][l] << endl; + return; + } + + 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)) continue; + + 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}}); + } + } + } + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + while (cin >> C >> L, C && L){ + cin >> Cr >> Lr; + cin >> Cs >> Ls; + 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; + for (int k = C1; k <= C2; k++) + for (int l = L1; l <= L2; l++) + alagado[k][l] = 1; + } + solve(); + } return 0; } \ No newline at end of file