QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#218945#2924. Lone Rookucup-team1004WA 5ms24372kbC++143.6kb2023-10-18 21:01:452023-10-18 21:01:45

Judging History

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

  • [2023-10-18 21:01:45]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:24372kb
  • [2023-10-18 21:01:45]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
	if(x.empty())return out<<"[]";
	out<<'['<<x[0];
	for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
	return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
	cerr<<x<<' ',debug(y...);
}
const int N=755,V=N*N;
int n,m,sx,sy,tx,ty;
char a[N][N];
struct Map{
	set<int>h[N],l[N];
	void insert(int x,int y){
		h[x].insert(y),l[y].insert(x);
	}
	void erase(int x,int y){
		h[x].erase(y),l[y].erase(x);
	}
	pair<int,int> getH(int x,int y){
		int pre=0,nex=m+1;
		auto it=h[x].lower_bound(y);
		if(it!=h[x].begin())pre=*prev(it);
		it=h[x].upper_bound(y);
		if(it!=h[x].end())nex=*it;
		return {pre,nex};
	}
	pair<int,int> getL(int x,int y){
		int pre=0,nex=n+1;
		auto it=l[y].lower_bound(x);
		if(it!=l[y].begin())pre=*prev(it);
		it=l[y].upper_bound(x);
		if(it!=l[y].end())nex=*it;
		return {pre,nex};
	}
}ok,ban;
const int dx[8]={2,2,1,1,-2,-2,-1,-1};
const int dy[8]={1,-1,2,-2,1,-1,2,-2};
int k,cnt[N][N],id[N][N],fa[V];
int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
vector<pair<int,int> >p[V];
bool merge(int x,int y){
	x=find(x),y=find(y);
	if(x==y)return 0;
	if(p[x].size()>p[y].size())p[x].swap(p[y]);
	for(auto pt:p[x])p[y].push_back(pt);
	return p[x].clear(),fa[x]=y,1;
}
bool valid(int x,int y){
	return 1<=x&&x<=n&&1<=y&&y<=m;
}
void reach(int x,int y){
	// debug("reach",x,y,k);
	int lx,rx,ly,ry,Lx,Rx,Ly,Ry;
	tie(ly,ry)=ok.getH(x,y),tie(lx,rx)=ok.getL(x,y);
	tie(Ly,Ry)=ban.getH(x,y),tie(Lx,Rx)=ban.getL(x,y);
	// debug("reach",x,y,ly,Ly);
	if(valid(x,ly)&&ly>Ly)merge(id[x][ly],id[x][y]);
	if(valid(x,ry)&&ry<Ry)merge(id[x][ry],id[x][y]);
	if(valid(lx,y)&&lx>Lx)merge(id[lx][y],id[x][y]);
	if(valid(rx,y)&&rx<Rx)merge(id[rx][y],id[x][y]);
	ok.insert(x,y);
}
queue<pair<int,int> >q;
void reduce(){
	for(auto pt:p[find(id[sx][sy])]){
		ban.erase(pt.first,pt.second);
		q.push(pt);
	}
	p[find(id[sx][sy])].clear();
}
void topo(){
	for(int i,j;!q.empty();){
		tie(i,j)=q.front(),q.pop();
		for(int k=0;k<8;k++){
			int x=i+dx[k],y=j+dy[k];
			if(!valid(x,y))continue;
			if(!--cnt[x][y]){
				if(a[x][y]=='K'){
					p[find(id[x][y])].push_back({x,y});
				}
				reach(x,y);
				reduce();
			}
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%s",a[i]+1);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			id[i][j]=++k,fa[k]=k;
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(a[i][j]=='R')sx=i,sy=j,a[i][j]='.';
			else if(a[i][j]=='T')tx=i,ty=j,a[i][j]='.';
			else if(a[i][j]=='K'){
				ban.insert(i,j);
				for(int k=0;k<8;k++){
					int x=i+dx[k],y=j+dy[k];
					if(valid(x,y))cnt[x][y]++;
				}
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(!cnt[i][j]){
				if(a[i][j]=='K'){
					p[find(id[i][j])].push_back({i,j});
				}
				reach(i,j);
				reduce();
				if(find(1)==find(3)){
					// debug(i,j);
					for(int i=1;i<=n;i++){
						vector<int>col;
						for(int j=1;j<=m;j++){
							col.push_back(find(id[i][j]));
						}
						// debug("col",i,col);
					}
				}
			}
		}
	}
	topo();
	// for(int i=1;i<=n;i++){
	// 	vector<int>col;
	// 	for(int j=1;j<=m;j++){
	// 		col.push_back(find(id[i][j]));
	// 	}
	// 	debug("col",i,col);
	// }
	puts(find(id[sx][sy])==find(id[tx][ty])?"yes":"no");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 22484kb

input:

2 2
KR
TK

output:

yes

result:

ok single line: 'yes'

Test #2:

score: 0
Accepted
time: 4ms
memory: 23116kb

input:

2 3
R.K
KKT

output:

yes

result:

ok single line: 'yes'

Test #3:

score: 0
Accepted
time: 2ms
memory: 24372kb

input:

5 3
KKT
.K.
K..
...
KKR

output:

yes

result:

ok single line: 'yes'

Test #4:

score: 0
Accepted
time: 4ms
memory: 24272kb

input:

2 4
R.KK
KK.T

output:

no

result:

ok single line: 'no'

Test #5:

score: 0
Accepted
time: 0ms
memory: 24192kb

input:

2 5
RKKK.
...KT

output:

no

result:

ok single line: 'no'

Test #6:

score: 0
Accepted
time: 0ms
memory: 21328kb

input:

5 6
.....T
..K...
..KK..
...K..
R.....

output:

no

result:

ok single line: 'no'

Test #7:

score: 0
Accepted
time: 0ms
memory: 21888kb

input:

3 4
...K
T.KR
..K.

output:

no

result:

ok single line: 'no'

Test #8:

score: 0
Accepted
time: 0ms
memory: 24224kb

input:

6 3
.R.
...
...
KKK
.K.
.T.

output:

yes

result:

ok single line: 'yes'

Test #9:

score: 0
Accepted
time: 5ms
memory: 23728kb

input:

6 6
..K..T
..K...
......
..KK.K
...K..
R.....

output:

no

result:

ok single line: 'no'

Test #10:

score: 0
Accepted
time: 4ms
memory: 23220kb

input:

6 6
..K...
..KTK.
......
..KK..
...K..
R.....

output:

yes

result:

ok single line: 'yes'

Test #11:

score: -100
Wrong Answer
time: 0ms
memory: 22908kb

input:

14 6
T.K..K
......
K....K
KKK.KK
KKK.KK
......
......
..K.KK
.K...K
......
..KK.K
...K..
......
R.K.K.

output:

no

result:

wrong answer 1st lines differ - expected: 'yes', found: 'no'