QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#230465#6739. TeleportwxqwqWA 30ms153064kbC++141.3kb2023-10-28 18:58:182023-10-28 18:58:19

Judging History

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

  • [2023-10-28 18:58:19]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:153064kb
  • [2023-10-28 18:58:18]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second

typedef pair<int,int> PII;
const int N=5010;

int n,k;
PII fa[N][N];
PII q[N*N];
int dist[N][N];
char str[N][N];

int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};

PII find(int x,int y)
{
	if(fa[x][y]!=(PII){x,y}) fa[x][y]=find(fa[x][y].x,fa[x][y].y);
	return fa[x][y];
}

void merge(PII a,PII b)
{
	PII x=find(a.x,a.y),y=find(b.x,b.y);
	if(x!=y) fa[x.x][x.y]=y;
}

void bfs()
{
	int tt=-1,hh=0;
	memset(dist,-1,sizeof(dist));
	dist[1][1]=0;
	q[++tt]={1,1};
	while(hh<=tt){
		int x=q[hh].x,y=q[hh++].y;
		if(x==n && y==n){
			cout<<dist[x][y]<<endl;
			return ;
		}
		
		for(int i=0;i<4;i++){
			int a=x+dx[i],b=y+dy[i];
			if(a<1 || a>n || b<1 || b>n || dist[a][b]!=-1 || str[a][b]=='*') continue;
			dist[a][b]=dist[x][y]+1;
			// cout<<x<<' '<<y<<' '<<a<<' '<<b<<endl;
			q[++tt]={a,b};
		}
		
		PII t=find(x,y);
		for(int a=t.x,b=t.y,i=t.x+t.y-x-y+1;i<=k;i++){
			swap(a,b),++a;
			if(a>n || b>n) break;
			merge(t,{a,b});
			if(str[a][b]=='*') continue;
			if(dist[a][b]!=-1) break;
			dist[a][b]=dist[x][y]+1;
			q[++tt]={a,b};
		}
	}
}

int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%s",str[i]+1);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			fa[i][j]={i,j};
	
	bfs();
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 3ms
memory: 106240kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 30ms
memory: 153064kb

input:

961 4
...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....

output:

545

result:

wrong answer 1st numbers differ - expected: '540', found: '545'