QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#277759#4376. Dragon slayerchunzhifengAC ✓263ms3704kbC++141.7kb2023-12-06 22:28:202023-12-06 22:28:20

Judging History

你现在查看的是最新测评结果

  • [2023-12-06 22:28:20]
  • 评测
  • 测评结果:AC
  • 用时:263ms
  • 内存:3704kb
  • [2023-12-06 22:28:20]
  • 提交

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;
int g[300][300];
bitset<300>st;
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++){
		for(int j=0;j<=tot;j++) g[i][j]=-1;
	}
	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][t2]=i;
					g[t2][t1]=i;
				}
			}
		}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][t2]=i;
					g[t2][t1]=i;
				}
			}
		}
	}
	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++){
		bitset<16>vis;
		for(int j=0;j<k;j++) if(s>>j&1) vis[j]=1;
		queue<int>Q; Q.push(sx);
		st.reset(); st[sx]=1;
		while(Q.size()){
			auto u=Q.front(); Q.pop();
			int x=u/m,y=u%m;
			for(int i=0;i<4;i++){
				int tx=x+dx[i],ty=y+dy[i];
				int v=tx*m+ty;
				if(tx>=0&&tx<n&&ty>=0&&ty<m&&!st[v]&&(g[u][v]==-1||vis[g[u][v]])){
					st[v]=1; Q.push(v);
				}
			}
		} if(st[ex]) 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: 100
Accepted
time: 263ms
memory: 3704kb

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...

output:

1
2
0
5
3
5
1
4
1
0

result:

ok 10 lines