hiho 1613 墨水滴 [Offer收割]编程练习赛32 Problem C 优先队列+BFS

这个题挺简单的,也挺有意思,我第一次做这种优先队列结合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
//
//
//


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值