70分求调
查看原帖
70分求调
985457
caokang楼主2025/7/26 20:09

评测记录

#include<bits/stdc++.h>
using namespace std;
long long n, m, x, y;
long long vis[410][410];
long long isok = 0;
long long board[410][410] = {-1};
struct Pos {
    long long x,y, cost;
    Pos(int ax = 0,int ay = 0, int acost = 0) {
        x = ax,y=ay, cost = acost;
    }
};
void qingkong(){
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			vis[i][j] = 0;
		}
	}
	isok = 0;
}
void bfs(){
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
				queue<Pos> q;
				q.push(Pos(x, y, 0));
				int flag = 0;
				while(!q.empty()){
					Pos now = q.front();
					q.pop();
					long long x2 = now.x;
					long long y2 = now.y;
					int cost = now.cost;
					if(x2 < 1 || x2 > n) continue;
					if(y2 < 1 || y2 > m) continue;
					if(vis[x2][y2] != 0) continue;
					vis[x2][y2] = 1;
					if(x2 == i && y2 == j){
						isok = 1;
						board[i][j] = cost;
						flag = 1;
						break;
					} 
					q.push(Pos(x2-1,y2+2,cost+1));
					q.push(Pos(x2+1,y2+2,cost+1));
					q.push(Pos(x2-1,y2-2,cost+1));
					q.push(Pos(x2+1,y2-2,cost+1));
					q.push(Pos(x2-2,y2+1,cost+1));
					q.push(Pos(x2+2,y2+1,cost+1));
					q.push(Pos(x2-2,y2-1,cost+1));
					q.push(Pos(x2+2,y2-1,cost+1));
				}
				if(flag = 1){
					qingkong();
					flag = 0;
				}
		}
	}

}

int main(){
	cin >> n >> m >> x >> y;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			board[i][j] = -1;
		}
	}
	bfs(); 
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			printf("%d ", board[i][j]);
		}
		printf("\n");
	}
	return 0;
}
2025/7/26 20:09
加载中...