QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#277749 | #4376. Dragon slayer | chunzhifeng | TL | 0ms | 0kb | C++14 | 1.8kb | 2023-12-06 22:18:07 | 2023-12-06 22:18:09 |
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define endl "\n"
#define PII pair<int,int>
const ll INF=0x3f3f3f3f;//3f3f3f3f;
const int mod=1e9+7;
const int N=2e6+5;
vector<PII>g[400];
int d[400];
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
void solve(){
int n,m,k; cin>>n>>m>>k;
int tot=n*m-1;
for(int i=0;i<=tot;i++) g[i].clear();
int sx,sy,ex,ey; cin>>sx>>sy>>ex>>ey;
sx=sx*m+sy; ex=ex*m+ey;
for(int i=0;i<k;i++){
int x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2;
if(x1==x2){
if(x1==0||x1==n) continue;
int l=min(y1,y2),r=max(y1,y2);
for(int j=l;j<r;j++){
int t1=(x1-1)*m+j,t2=x1*m+j;
if(t1>=0&&t1<=tot&&t2>=0&&t2<=tot){
g[t1].push_back({t2,i});
g[t2].push_back({t1,i});
//cout<<t1<<' '<<t2<<endl;
}
}
}else{
if(y1==0||y1==m) continue;
int l=min(x1,x2),r=max(x1,x2);
for(int j=l;j<r;j++){
int t1=j*m+y1-1,t2=j*m+y1;
if(t1>=0&&t1<=tot&&t2>=0&&t2<=tot){
g[t1].push_back({t2,i});
g[t2].push_back({t1,i});
//cout<<t1<<' '<<t2<<endl;
}
}
}
}
auto get=[&](int x){
int ans=0;
while(x) x-=x&-x,ans++;
return ans;
};
int ans=INF;
for(int s=0;s<1<<k;s++){
memset(d,0x3f,sizeof d);
deque<int>Q;
Q.push_front(sx);
d[sx]=0;
while(Q.size()){
auto u=Q.front(); Q.pop_front();
int x=u/m,y=u%m;
map<int,int>mp;
for(auto &[v,id]:g[u]){
if(s>>id&1) continue;
mp[v]=1;
}
for(int i=0;i<4;i++){
int tx=x+dx[i],ty=y+dy[i];
int v=tx*m+ty,flag=mp[v];
if(tx>=0&&tx<n&&ty>=0&&ty<m&&d[v]==INF&&!flag){
d[v]=d[u]+flag;
Q.push_front(v);
}
}
} if(d[ex]<INF) ans=min(ans,get(s));
} cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int T=1; cin>>T;
while(T--) solve();
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
10 4 4 4 0 2 3 3 0 2 4 2 1 3 1 0 2 4 2 1 3 1 3 4 3 2 2 0 0 2 1 0 1 3 1 1 0 1 2 3 2 2 0 0 2 1 2 1 2 2 1 0 1 1 15 15 15 3 12 4 1 8 0 8 15 1 11 15 11 1 1 1 15 3 1 3 15 0 10 14 10 14 1 14 14 8 1 8 15 1 5 14 5 0 15 14 15 0 4 14 4 0 2 15 2 11 0 11 15 4 1 4 15 1 11 15 11 1 12 14 12 15 15 15 8 5 14 0 0 12 1...