P1443,0分
  • 板块P1443 马的遍历
  • 楼主_TLE_
  • 当前回复1
  • 已保存回复1
  • 发布时间2025/1/16 20:13
  • 上次更新2025/1/16 20:28:27
查看原帖
P1443,0分
1641424
_TLE_楼主2025/1/16 20:13

题目~

#include<bits/stdc++.h>
using namespace std;
struct P{
	int x,y;
}o,b;

queue<P> q;

const int dx[8] = {1,-1,1,-1,2,-2,2,-2};
const int dy[8] = {2,2,-2,-2,1,1,-1,-1};

int ans[410][410];
int main(){
	
	int m,n,x,y;
	cin>>n>>m>>x>>y;
	
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			ans[i][j] = -1;
		}
	}
	
	ans[x][y] = 0;
	o.x = x, o.y = y;
	q.push(o);
	
	while (!q.empty()) {
		o = q.front();
		q.pop();
		
		for(int t = 0 ; t < 8; t++){
			x = o.x + dy[t];
			y = o.y + dy[t];
			if ( x < 1 || x > n || y < 1 || y > m) continue;
			if ( ans[x][y] != -1) continue;
			ans[x][y] = ans[o.x][o.y] + 1;
			b.x = x; b.y = y;
			q.push(b);
		}
	}
	
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j ++){
			cout << ans[i][j] << (" \n"[j == m]);
		}
	}

	return 0;
}

//0分

2025/1/16 20:13
加载中...