• [CODEVS1537] 血色先锋队


    题目描述 Description

    巫妖王的天灾军团终于卷土重来,血色十字军组织了一支先锋军前往诺森德大陆对抗天灾军团,以及一切沾有亡灵气息的生物。孤立于联盟和部落的血色先锋军很快就遭到了天灾军团的重重包围,现在他们将主力只好聚集了起来,以抵抗天灾军团的围剿。可怕的是,他们之中有人感染上了亡灵瘟疫,如果不设法阻止瘟疫的扩散,很快就会遭到灭顶之灾。大领主阿比迪斯已经开始调查瘟疫的源头。原来是血色先锋军的内部出现了叛徒,这个叛徒已经投靠了天灾军团,想要将整个血色先锋军全部转化为天灾军团!无需惊讶,你就是那个叛徒。在你的行踪败露之前,要尽快完成巫妖王交给你的任务。

    军团是一个N行M列的矩阵,每个单元是一个血色先锋军的成员。感染瘟疫的人,每过一个小时,就会向四周扩散瘟疫,直到所有人全部感染上瘟疫。你已经掌握了感染源的位置,任务是算出血色先锋军的领主们感染瘟疫的时间,并且将它报告给巫妖王,以便对血色先锋军进行一轮有针对性的围剿。

    输入描述 Input Description

    第1行:四个整数N,M,A,B,表示军团矩阵有N行M列。有A个感染源,B为血色敢死队中领主的数量。

    接下来A行:每行有两个整数x,y,表示感染源在第x行第y列。

    接下来B行:每行有两个整数x,y,表示领主的位置在第x行第y列。

    输出描述 Output Description

    第1至B行:每行一个整数,表示这个领主感染瘟疫的时间,输出顺序与输入顺序一致。如果某个人的位置在感染源,那么他感染瘟疫的时间为0。

    样例输入 Sample Input

    5 4 2 3

    1 1

    5 4

    3 3

    5 3

    2 4

    样例输出 Sample Output

    3

    1

    3

    数据范围及提示 Data Size & Hint

    【数据规模】

    1<=M,N<=500

    1<=A,B<=M*N


    提交地址CODEVS


    题解:

    直接bfs;

    记得数组开够;


    Code:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 using namespace std;
     6 
     7 int n, m, A, B;
     8 int dx[]={0, 1, -1, 0, 0}, dy[]={0, 0, 0, -1, 1};
     9 
    10 bool a[505][505];
    11 int bbx[250001], bby[250001];
    12 int when[505][505];
    13 
    14 struct bfs
    15 {
    16     int x, y, stp;
    17 };
    18 
    19 queue <bfs> q;
    20 
    21 
    22 int main()
    23 {
    24     cin >> n >> m >> A >> B;
    25     for (register int i = 1 ; i <= A ; i ++) 
    26         {int x, y;scanf("%d %d", &x, &y);a[x][y]=1;q.push((bfs){x, y, 0});}
    27     for (register int i = 1 ; i <= B ; i ++)
    28         {int x, y;scanf("%d %d", &x, &y);bbx[i]=x,bby[i]=y;}
    29     memset(when, -1, sizeof when);
    30     while (!q.empty())
    31     {
    32         int x = q.front().x, y = q.front().y, stp = q.front().stp;
    33         q.pop();
    34         if (when[x][y] == -1) when[x][y] = stp;
    35         
    36         for (register int i = 1 ; i <= 4 ; i ++)
    37         {
    38             int tx = x + dx[i], ty = y + dy[i];
    39             if (a[tx][ty] or tx <= 0 or ty <= 0 or tx > n or ty > m) continue;
    40             a[tx][ty] = 1;
    41             q.push((bfs){tx, ty, stp + 1});
    42         }
    43     }
    44     
    45     for (register int i = 1 ; i <= B ; i ++) printf("%d
    ", when[bbx[i]][bby[i]]);
    46     
    47     return 0;
    48 }
  • 相关阅读:
    Eclipse创建Python工程
    MySQL python安装
    pip安装-python2.7.15
    接口测试-HTTP重点知识及 测试工具
    接口测试-基础
    关闭win10的自动更新功能
    高版本MySQL(5.7,5.8)的JDBC连接新问题
    错误:java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
    Idea SpringBoot工程提示 "Error running 'xxxx'": Command line is too long... 问题解决
    错误:23:36:21.161 [main] ERROR org.springframework.boot.SpringApplication
  • 原文地址:https://www.cnblogs.com/BriMon/p/9160425.html
Copyright © 2020-2023  润新知