QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#218930 | #2924. Lone Rook | ucup-team1004 | WA | 5ms | 24368kb | C++14 | 3.2kb | 2023-10-18 20:48:48 | 2023-10-18 20:48:48 |
Judging History
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(Ry,Ry)=ban.getH(x,y),tie(Lx,Rx)=ban.getL(x,y);
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);
}
}
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;
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();
}
}
}
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: 21408kb
input:
2 2 KR TK
output:
yes
result:
ok single line: 'yes'
Test #2:
score: 0
Accepted
time: 5ms
memory: 24368kb
input:
2 3 R.K KKT
output:
yes
result:
ok single line: 'yes'
Test #3:
score: 0
Accepted
time: 0ms
memory: 22736kb
input:
5 3 KKT .K. K.. ... KKR
output:
yes
result:
ok single line: 'yes'
Test #4:
score: 0
Accepted
time: 0ms
memory: 21600kb
input:
2 4 R.KK KK.T
output:
no
result:
ok single line: 'no'
Test #5:
score: -100
Wrong Answer
time: 3ms
memory: 22752kb
input:
2 5 RKKK. ...KT
output:
yes
result:
wrong answer 1st lines differ - expected: 'no', found: 'yes'