QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#446387 | #4788. Gravity | C1942huangjiaxu | ML | 28ms | 207956kb | C++14 | 1.5kb | 2024-06-17 09:30:10 | 2024-06-17 09:30:11 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2005,M=N*N;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1},dw[N][N],dwp[N][N];
int n,m,id[N][N],ci,d[M];
bool vis[M];
vector<pair<int,int> >e[M];
char s[N][N],t[N][N];
vector<int>q[M];
void dfs(int x,int y){
id[x][y]=ci;
for(int i=0;i<4;++i){
int X=x+dx[i],Y=y+dy[i];
if(X<1||X>n||Y<1||Y>m||s[X][Y]=='.')continue;
if(!id[X][Y])dfs(X,Y);
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)scanf("%s",s[i]+1);
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(s[i][j]=='#'&&!id[i][j])++ci,dfs(i,j);
for(int i=1;i<=m;++i)dwp[n][i]=n+1;
for(int i=n-1;i;--i){
for(int j=1;j<=m;++j){
if(s[i+1][j]=='#')dw[i][j]=id[i+1][j],dwp[i][j]=i+1;
else dw[i][j]=dw[i+1][j],dwp[i][j]=dwp[i+1][j];
}
}
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)
if(id[i][j]&&dw[i][j]!=id[i][j])e[dw[i][j]].emplace_back(id[i][j],dwp[i][j]-i-1);
memset(d,0x3f,sizeof(d));
d[0]=0,q[0].emplace_back(0);
for(int i=0;i<n;++i){
while(!q[i].empty()){
int u=q[i].back();
q[i].pop_back();
if(vis[u])continue;
vis[u]=true;
for(auto [v,w]:e[u])if(d[v]>d[u]+w){
d[v]=d[u]+w;
q[d[v]].emplace_back(v);
}
e[u].clear(),e[u].shrink_to_fit();
}
q[i].shrink_to_fit();
}
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)t[i][j]='.';
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(id[i][j])t[i+d[id[i][j]]][j]='#';
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j)putchar(t[i][j]);
putchar(10);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 28ms
memory: 207956kb
input:
10 10 .......... ..######.. ..#....#.. ..#.#..#.. ..#..#.#.. ..#....#.. ..######.. .......... ..#....#.. .......#..
output:
.......... .......... ..######.. ..#....#.. ..#....#.. ..#....#.. ..#.##.#.. ..######.. .......#.. ..#....#..
result:
ok 10 lines
Test #2:
score: -100
Memory Limit Exceeded
input:
1583 1799 #..###..##..#.####.##.##.###..#.....##..#.#.#.#......#.....##..#.##...#.#....#..##.###...#...##.###.#.....#.##.###...#..##.#...###..#.###.#...###..#.......#...#....#.#..#...##........#.#..#..##.....###...#..#.####..####...#..##......#........#..#.##.##..#..#....##.##.##..#..##.....###....#...
output:
...............................................................................................................................................................................................................................................................................................................