QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#711886 | #1943. Borders | harlem | WA | 17ms | 10820kb | C++14 | 2.0kb | 2024-11-05 13:52:12 | 2024-11-05 13:52:12 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=505;
int n,m,num,ans;
char g[N][N];
int uid[N][N];
bool nbord[N*N];
int bordum;
vector<int> lt,rt;
vector<int> edge[N*N];
bool vis[N][N];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void dfs(int x,int y,int id){
uid[x][y]=id;
vis[x][y]=true;
if(x==1||x==n||y==1||y==m){
if(!nbord[id])bordum++;
nbord[id]=true;
}
for(int k=0;k<4;k++){
int nx=x+dx[k],ny=y+dy[k];
if(nx<1||nx>n||ny<1||ny>m)continue;
if(!vis[nx][ny]&&g[nx][ny]==g[x][y])dfs(nx,ny,id);
}
}
bool st[N*N];
int mg[N*N];
bool hungry(int now){
for(auto nxt:edge[now]){
if(vis[nxt])continue;
st[nxt]=true;
if(!mg[nxt]||hungry(mg[nxt])){
mg[nxt]=now;
return true;
}
}
return false;
}
int main(){
// freopen("unity.in","r",stdin);
// freopen("unity.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>g[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!vis[i][j]){
dfs(i,j,++num);
if(nbord[num])continue;
if(g[i][j]=='0')lt.push_back(num);
else rt.push_back(num);
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(nbord[uid[i][j]]||g[i][j]=='1')continue;
for(int k=0;k<4;k++){
int nx=i+dx[k],ny=j+dy[k];
if(uid[i][j]!=uid[nx][ny]&&!nbord[uid[nx][ny]]){
edge[uid[i][j]].push_back(uid[nx][ny]);
}
}
}
}
for(int i=1;i<=num;i++){
sort(edge[i].begin(),edge[i].end());
auto lst=unique(edge[i].begin(),edge[i].end());
edge[i].erase(lst,edge[i].end());
}
ans=bordum;
for(auto i:lt){
memset(st,0,sizeof(st));
if(hungry(i))ans++;
}
cout<<ans;
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 17ms
memory: 10820kb
input:
100 100 0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101 1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010 010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101...
output:
396
result:
wrong answer 1st lines differ - expected: '5198', found: '396'