精准核酸检测

 为了达到新冠疫情精准防控的需要,为了避免全员核酸检测带来的浪费,需要精准圈定可能被感染的人群。现在根据传染病流调以及大数据分析,得到了每个人之间在时间、空间上是否存在轨迹的交叉。
 现在给定一组确诊人员编号 (X1, X2., X3, ..., n),在所有人当中,找出哪些人需要进行核酸检测,输出需要进行核酸检测的人数。(注意:确诊病例自身不需要再做核酸检测)
 需要进行核酸检测的人,是病毒传播链条上的所有人员,即有可能通过确诊病例所能传播到的所有人。
 例如:A是确诊病例,A和B有接触、B和C有接触、C和D有接触、D和E有接触,那么B\C\D\E都是需要进行核酸检测的人。
 输入描述:
  第一行为总人数N
  第二行为确诊病例人员编号(确诊病例人员数量<N),用逗号分割
  第三行开始,为一个N*N的矩阵,表示每个人员之间是否有接触,0表示没有接触,1表示有接触。
 输出描述:需要做核酸检测的人数

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int total = sc.nextInt();
        sc.nextLine();
        String[] temp = sc.nextLine().split("[,]");
        List<Integer> unNormal = new ArrayList<>();
        //接收不正常的列表
        for(int i = 0;i < temp.length;i++){
            unNormal.add(Integer.parseInt(temp[i]));
        }
        //校验
        boolean[] check = new boolean[total];
        //接收编号
        int[][] people = new int[total][total];
        for(int i = 0;i < total;i++){
            String[] tempS = sc.nextLine().split("[,]");
            for(int j = 0;j < total;j++){
                people[i][j] = Integer.parseInt(tempS[j]);
            }
        }
        
        //记录结果,包括了已经有问题的
        int res = 0;
        int unSize = unNormal.size();
        for(int i = 0;i < unSize;i++){
            int m = unNormal.get(i);
            res += dfs(people,check,m);
        }
        System.out.print(res - unSize);
    }
    
    public static int dfs(int[][] people,boolean[] check,int i){
        if(check[i]){
            return 0;
        }
        check[i] = true;
        int res = 1;
        for(int j = 0;j < people.length;j++){
            if(people[i][j] == 1){
                res += dfs(people,check,j);
            }
        }
        return res;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容