feat: new solution code.
This commit is contained in:
@@ -1,8 +1,128 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
#define endl '\n'
|
||||||
|
#define INF 0x3f
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
typedef pair<int, int> 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<pii> 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<pair<int, pii>> 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;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user