TLE on #8
查看原帖
TLE on #8
657210
dp_hater楼主2023/10/11 18:01
https://www.luogu.com.cn/record/128759258
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef unsigned long long sln;
int mx[10]={1,2,2,1,-1,-2,-2,-1},my[10]={2,1,-1,-2,-2,-1,1,2};
struct point{
	int x,y;
	int step;
};
bool vis[405][405];
int st[405][405];
int sx,sy,ex,ey,tx,ty;
int n,m;
int bfs(){
	if(sx==ex&&sy==ey) return 0;
	memset(vis,false,sizeof(vis));
	queue<point>q;
	point start,p;
	start.x=sx,start.y=sy;
	start.step=0;
	q.push(start);
	int step,x,y;
	while(!q.empty()){
		start=q.front(),q.pop();
		x=start.x;
		y=start.y;
		step=start.step;
		for(int i=0;i<8;i++){
			tx=x+mx[i],ty=y+my[i];
			if(tx==ex&&ty==ey) return step+1;
			if(tx>=0&&tx<n&&ty>=0&&ty<m&&!vis[tx][ty]){
				p.x=tx;
				p.y=ty;
				p.step=step+1;
				q.push(p);
				vis[tx][ty]=1;
				st[x][y]=min((signed)max((signed)st[x][y],-1),(signed)step);
			}
		}
	}
	return -1;
}
signed main(){
	cin>>n>>m;
	cin>>sx>>sy;
	sx--;
	sy--;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			st[i][j]=-1;
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			ex=i,ey=j;
			st[i][j]=bfs();
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			printf("%-5d",st[i][j]);
		}
		cout<<endl;
	}
	cout<<endl;
	return 0;
}

备注:用的是 cpp98 否则 #4 也会 TLE

2023/10/11 18:01
加载中...