QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#563572 | #9224. Express Eviction | arnold518# | WA | 1ms | 4552kb | C++17 | 4.3kb | 2024-09-14 13:55:34 | 2024-09-14 13:55:35 |
Judging History
answer
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 60;
const int INF = 1e9;
int N, M;
int A[MAXN+10][MAXN+10];
const int dy[]={0, -1, 0, 1};
const int dx[]={1, 0, -1, 0};
bool in(int y, int x) { return (0<=y && y<=N && 0<=x && x<=M); }
int f(int y, int x, int d)
{
assert(in(y, x));
if(d==0) return A[y][x+1]+A[y+1][x+1];
if(d==1) return A[y][x]+A[y][x+1];
if(d==2) return A[y][x]+A[y+1][x];
if(d==3) return A[y+1][x]+A[y+1][x+1];
}
int f2(int y, int x)
{
assert(in(y, x));
return f(y, x, 0)+f(y, x, 2);
}
struct UF
{
int par[MAXN*MAXN+10];
void init()
{
for(int i=0; i<(N+1)*(M+1); i++) par[i]=i;
}
int g(int y, int x)
{
if(!in(y, x)) return -1;
return y*(M+1)+x;
}
int Find(int x)
{
if(x==par[x]) return x;
return par[x]=Find(par[x]);
}
void Union(int x, int y)
{
x=Find(x); y=Find(y);
par[x]=y;
}
bool same(int x, int y)
{
x=Find(x); y=Find(y);
return x==y;
}
}uf;
struct Queue
{
int y, x, d, w;
bool operator < (const Queue &ot) const { return w>ot.w; }
};
vector<array<int, 4>> adj[MAXN+10][MAXN+10][4];
vector<int> V[2][2];
int dist[MAXN+10][MAXN+10][4];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
V[0][0]={0, 3};
V[0][1]={2, 3};
V[1][0]={0, 1};
V[1][1]={1, 2};
cin >> N >> M;
for(int i=1; i<=N; i++)
{
string S;
cin >> S;
for(int j=0; j<M; j++) A[i][j+1]=(S[j]=='#');
}
// for(int i=1; i<=N; i++) { for(int j=1; j<=M; j++) printf("%d ", A[i][j]); printf("\n"); }
uf.init();
for(int i=0; i<=N; i++) for(int j=0; j<=M; j++)
{
if(f2(i, j)) continue;
for(int k=0; k<4; k++)
{
int ny=i+dy[k], nx=j+dx[k];
if(!in(ny, nx)) continue;
if(f2(ny, nx)) continue;
uf.Union(uf.g(i, j), uf.g(ny, nx));
}
}
for(int i=0; i<=N; i++)
{
for(int j=0; j<=M; j++)
{
for(int d=0; d<4; d++)
{
for(int k=0; k<4; k++)
{
int ny=i+dy[k], nx=j+dx[k];
if(!in(ny, nx)) continue;
adj[i][j][d].push_back({ny, nx, k, f(ny, nx, k)});
// printf("EDGE %d %d %d => %d %d %d with weight %d\n", i, j, d, ny, nx, k, f(ny, nx, k));
}
}
}
}
for(int y=1; y<=N; y++)
{
for(int x=1; x<=M; x++)
{
if(!A[y][x]) continue;
for(int i : {0, 1})
{
for(int j : {0, 1})
{
int a=y-1+i, b=x-1+j;
int c=y-1+1-i, d=x-1+1-j;
for(int p : {0, 1})
{
int dd1=V[i][j][p], dd2=V[i][j][1-p];
int a2=a-dy[dd2]*2, b2=b-dx[dd2]*2;
int c2=c+dy[dd1]*2, d2=d+dx[dd1]*2;
if(in(a2, b2) && in(c2, d2))
{
if(uf.same(uf.g(a2, b2), uf.g(c2, d2)))
{
adj[a][b][dd1].push_back({c, d, (dd1+2)%4, f2(c, d)-1});
// printf("EDGE %d %d %d => %d %d %d with weight %d\n", a, b, dd1, c, d, (dd1+2)%4, f2(c, d)-1);
// printf("%d %d %d %d\n", a2, b2, c2, d2);
}
}
}
}
}
}
}
priority_queue<Queue> PQ;
for(int i=0; i<=N; i++) for(int j=0; j<=M; j++) for(int k=0; k<4; k++) dist[i][j][k]=INF;
PQ.push({0, 0, 2, 0}); dist[0][0][2]=0;
PQ.push({0, 0, 1, 0}); dist[0][0][1]=0;
while(!PQ.empty())
{
auto [y, x, d, w] = PQ.top(); PQ.pop();
if(dist[y][x][d]!=w) continue;
// printf("%d %d %d : %d\n", y, x, d, w);
for(auto [ny, nx, nd, nw] : adj[y][x][d])
{
if(dist[ny][nx][nd]>w+nw)
{
dist[ny][nx][nd]=w+nw;
PQ.push({ny, nx, nd, w+nw});
}
}
}
cout << min(dist[N][M][3], dist[N][M][0])+A[1][1] << "\n";
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 4056kb
input:
4 6 .##... .#.... ##.... ....#.
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 4276kb
input:
20 30 ...########################### #..########################### ...########################### ...########################### .############################# ...########################### ...########################### #..########################### ...########################### ..#...............
output:
11
result:
ok 1 number(s): "11"
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 4552kb
input:
35 35 ....###########...#########........ ##..#######################..#####. ....#######################..#####. ...#.....##################..#####. .##......##################.....##. .##..##..#############.....#....##. .....##..#############......##..##. ....#....#############..##..##..##. ####.....
output:
21
result:
wrong answer 1st numbers differ - expected: '16', found: '21'