QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#446382#4788. GravityC1942huangjiaxuML 0ms0kbC++141.5kb2024-06-17 09:23:142024-06-17 09:23:15

Judging History

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

  • [2024-06-17 09:23:15]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-06-17 09:23:14]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=4e6+5;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1},dw[N],dwp[N];
int n,m,id[N],ci,d[N];
bool vis[N];
vector<pair<int,int> >e[N];
char *s[N],t[N];
vector<pair<int,int> >nd[N];
vector<int>q[N];
#define h(x,y) ((x)*m-m+(y))
void dfs(int x,int y){
	id[h(x,y)]=ci;
	nd[ci].emplace_back(x,y);
	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[h(X,Y)])dfs(X,Y);
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i){
		s[i]=new char[m+3];
		scanf("%s",s[i]+1);
	}
	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(s[i][j]=='#'&&!id[h(i,j)])++ci,dfs(i,j);
	for(int i=1;i<=m;++i)dwp[h(n,i)]=n+1;
	for(int i=n-1;i;--i){
		for(int j=1;j<=m;++j){
			int x=h(i,j);
			if(s[i+1][j]=='#')dw[x]=id[h(i+1,j)],dwp[x]=i+1;
			else dw[x]=dw[h(i+1,j)],dwp[x]=dwp[h(i+1,j)];
		}
	}
	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){
		int x=h(i,j);
		if(id[x]&&dw[x]!=id[x])e[dw[x]].emplace_back(id[x],dwp[x]-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);
		}
	}
	for(int i=1;i<=n*m;++i)t[i]='.';
	for(int i=1;i<=ci;++i)for(auto [x,y]:nd[i])t[h(x+d[i],y)]='#';
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j)putchar(t[h(i,j)]);
		putchar(10);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Memory Limit Exceeded

input:

10 10
..........
..######..
..#....#..
..#.#..#..
..#..#.#..
..#....#..
..######..
..........
..#....#..
.......#..


output:

..........
..........
..######..
..#....#..
..#....#..
..#....#..
..#.##.#..
..######..
.......#..
..#....#..

result: