C C++最新ZOJ 2849 Attack of Panda Virus(BFS+优先队列),2024C C++高级进阶学习资料

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

1 0 1 0
1 1 1 2
0 1 2 2

In day 3:

1 1 1 1
1 1 1 2
1 1 2 2

So at last, all the computers in the networks were infected by virus.

Your task is to calculate after all the computers are infected, how many computers are infected with some specific virus variations.

Input

The input contains multiple test cases!

On the first line of each test case are two integers M and N (1 <= MN <= 500), followed by a M * N matrix. A positive integer T in the matrix indicates that the corresponding computer had already been infected by the virus variations of type T at the beginning while a negative integer -L indicates that the computer has a defense level L. Then there is an integer Q indicating the number of queries. Each of the following Q lines has an integer which is the virus variation type we care.

Output

For each query of the input, output an integer in a single line which indicates the number of computers attacked by this type of virus variation.

Sample Input

3 4
1 -3 -2 -3
-2 -1 -2 2
-3 -2 -1 -1
2
1
2

Sample Output

9
3


Author: XUE, Zaiyue

题目大意:熊猫烧香病毒入侵电脑,病毒入侵的能力每天增长一,每天都可以入侵不大于自己入侵能力,且与感染病毒电脑相连的电脑,没种电脑只能被感染一次

t次询问,问感染某种病毒的电脑数量是多少。

思路:

BFS 优先队列搜索,先取入侵能力小的代表入侵的时间是靠前的,如果入侵能力相同,取病毒类型数小的先入侵

每次入侵时,如果病毒的入侵能力大于当前电脑的防御能力,则对其进行感染,如果不够入侵,则先取该病毒所在位置周边电脑防御能力最小的入队列,循环操作,直至队列为空

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int x,y;
int c[][2]= {{0,1},{0,-1},{1,0},{-1,0}};
int maps[520][520];
int v[520][520];
int cou[250000];
int n,m;
struct Node
{
    int x,y;
    int type;
    int level;
    friend bool operator < (Node a,Node b)
    {
        if(a.level==b.level)
            return a.type>b.type;
        return a.level>b.level;
    }
} node;
priority_queue<Node> q;
void bfs()
{

    while(!q.empty())
    {
        int flag=0;
        Node tp=q.top(),temp;
        q.pop();
        for(int i=0; i<4; i++)
        {
            temp.x=tp.x+c[i][0];
            temp.y=tp.y+c[i][1];
            if(temp.x>=n||temp.y>=m||temp.x<0||temp.y<0||maps[temp.x][temp.y]>0) continue;
            if(tp.level>=maps[temp.x][temp.y]*(-1))
            {
                temp.type=tp.type;
                temp.level=tp.level;
                q.push(temp);
                maps[temp.x][temp.y]=temp.type;
                cou[temp.type]++;
            }
            else
            {
               if(!flag) flag=maps[temp.x][temp.y];
               else
                flag=max(flag,maps[temp.x][temp.y]);//因为输入的防御能力是负数,所以我们取得是最大值(eg,-2>-3,我们取-2能力最弱)
            }
        }
        if(flag){//如果还有,没有入侵的电脑,将其入队列;
            tp.level=-flag;
            q.push(tp);
        }
    }
}
int main()
{

    while(~scanf("%d%d",&n,&m))
    {
        while(!q.empty()) q.pop();


![img](https://img-blog.csdnimg.cn/img_convert/142d8613071768fea09312c24b919e03.png)
![img](https://img-blog.csdnimg.cn/img_convert/6fe93c26e15254981017ab2dab89f3d9.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**


**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 11
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值