TLE on #4 #8 求助
查看原帖
TLE on #4 #8 求助
657210
dp_hater楼主2023/7/7 08:31
#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[1005][1005];
int st[1005][1005];
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]=true;
				st[x][y]=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<n;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++){
			cout<<setw(6)<<left<<st[i][j];
		}
		cout<<endl;
	}
	cout<<endl;
	return 0;
}

2023/7/7 08:31
加载中...