QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#276712#6739. Teleportucup-team958#TL 8ms101740kbC++141.1kb2023-12-06 09:57:302023-12-06 09:57:31

Judging History

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

  • [2023-12-06 09:57:31]
  • 评测
  • 测评结果:TL
  • 用时:8ms
  • 内存:101740kb
  • [2023-12-06 09:57:30]
  • 提交

answer

#include<bits/stdc++.h>
#define il inline
using namespace std;
const int maxn=5010;
struct pos{
	int x,y;
	pos(int a=0,int b=0){x=a,y=b;}
};
int p[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
queue<pos>q;
char a[maxn][maxn];
int f[maxn][maxn];
int n,k;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%s",a[i]+1);
	memset(f,-1,sizeof(f));
	q.push(pos(1,1)),f[1][1]=0;
	while(!q.empty()){
		pos t=q.front(); q.pop();
		for(int i=1;i*2<=k;i++){
			pos M(t.x+i,t.y+i);
			if(M.x>n||M.y>n||(~f[M.x][M.y]&&f[M.x][M.y]<=f[t.x][t.y])) break;
			if(a[M.x][M.y]!='*')
				f[M.x][M.y]=f[t.x][t.y]+1,q.push(M);
		}
		for(int i=0;i*2+1<=k;i++){
			pos M(t.y+1+i,t.x+i);
			if(M.x>n||M.y>n||(~f[M.x][M.y]&&f[M.x][M.y]<=f[t.x][t.y])) break;
			if(a[M.x][M.y]!='*')
				f[M.x][M.y]=f[t.x][t.y]+1,q.push(M);
		}
		for(int i=0;i<4;i++){
			pos M(t.x+p[i][0],t.y+p[i][1]);
			if(M.x<1||M.y<1||M.x>n||M.y>n) continue;
			if(a[M.x][M.y]=='*') continue;
			if(~f[M.x][M.y]) continue;
			f[M.x][M.y]=f[t.x][t.y]+1;
			q.push(M);
		}
	}
	printf("%d\n",f[n][n]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 101676kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 4ms
memory: 101740kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Time Limit Exceeded

input:

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

output:


result: