这个题挺简单的,也挺有意思,我第一次做这种优先队列结合BFS的题目,给的数据量很大,暴力肯定TLE,但是我们可以用优先队列进行剪枝,先处理颜色深度高的点,就是这样吧,代码很短。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
int N,K;
int F[1009][1009];
int d[4][2]={1,0,0,1,-1,0,0,-1};
bool cango(int x,int y){
return x>=0&&x<N&&y>=0&&y<N;
}
struct node{
int deep;
int x,y;
node(){}
node(int X,int Y,int D){
x=X;
y=Y;
deep=D;
}
friend bool operator<(const node&a,const node&b){
return a.deep<b.deep;
}
};
priority_queue<node>q;
void solve(){
memset(F,0,sizeof(F));
while(!q.empty()){
node t=q.top();
q.pop();
int x=t.x;
int y=t.y;
int de=t.deep;
if(de>F[x][y]){
F[x][y]=de;
for(int i=0;i<4;i++){
int newx=x+d[i][0];
int newy=y+d[i][1];
if(cango(newx,newy)&&de-1>F[newx][newy]){
q.push(node(newx,newy,de-1));
}
}
}
}
}
int main(){
cin>>N>>K;
for(int i=0;i<K;i++){
int x,y,d;
scanf("%d%d%d",&x,&y,&d);
q.push(node(x,y,d));
}
solve();
for(int i=0;i<N;i++){
for(int j=0;j<N-1;j++){
cout<<F[i][j]<<" ";
}cout<<F[i][N-1]<<endl;
}
return 0;
}
//
// _oo0oo_
// o8888888o
// 88" . "88
// (| -_- |)
// 0\ = /0
// ___/`---'\___
// .' \\| |// '.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ - /// | |
// | \_| ''\---/'' |_/ |
// \ .-\__ '-' ___/-. /
// ___'. .' /--.--\ `. .'___
// ."" '< `.___\_<|>_/___.' >' "".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `_. \_ __\ /__ _/ .-` / /
// =====`-.____`.___ \_____/___.-`___.-'=====
// `=---='
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// 佛祖保佑 永无BUG
//
//
//