The 2016 ACM-ICPC Asia Beijing Regional Contest E - What a Ridiculous Election

时间:2021-10-28 10:16:27

https://vjudge.net/contest/259447#problem/E

bfs,k个限制条件以数组的额外k维呈现。

 #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e9
#define pi 3.1415926536
#define nl 2.7182818284
const ll mod=1e9+;//
const int maxn=1e5+; int qx[maxn*],qy[maxn*],num[maxn*],step[maxn*],ci[maxn],c[]={,,,,},d[];
bool vis[maxn][][]; int main()
{
int head,tail,i,j,s,x,y,ss,bu;
head=,tail=;
for (i=;i<1e5;i++)
ci[i]=inf;
qx[]=,qy[]=,num[]=,step[]=,ci[]=,vis[][][]=;
while (head<tail)
{
head++;
s=num[head];
x=qx[head];
y=qy[head];
bu=step[head]+;
ss=s;
for (i=;i<;i++)
d[i]=ss%,ss/=;
for (i=;i<;i++)
{
j=i+;
ss=s+(c[i]-c[j])*(d[j]-d[i]);
if (!vis[ss][x][y])
{
tail++;
qx[tail]=x;
qy[tail]=y;
num[tail]=ss;
step[tail]=bu;
ci[ss]=min(ci[ss],bu);
vis[ss][x][y]=;
}
}
if (x!=)
for (i=;i<;i++)
{
ss=s+c[i]*(d[i]==?-:);
if (!vis[ss][x-][y])
{
tail++;
qx[tail]=x-;
qy[tail]=y;
num[tail]=ss;
step[tail]=bu;
ci[ss]=min(ci[ss],bu);
vis[ss][x-][y]=;
}
}
if (y!=)
for (i=;i<;i++)
{
ss=s+c[i]*(d[i]*%-d[i]);
if (!vis[ss][x][y-])
{
tail++;
qx[tail]=x;
qy[tail]=y-;
num[tail]=ss;
step[tail]=bu;
ci[ss]=min(ci[ss],bu);
vis[ss][x][y-]=;
}
}
}
while (~scanf("%d",&s))
printf("%d\n",ci[s]==inf?-:ci[s]);
return ;
}