Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)

题目链接:https://codeforces.com/contest/1105/problem/D

题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 1 ~ p 的顺序,问最后每个人占领的点的数量。

题解:用一个队列维护当前起点,用另一个队列模拟当前起点走 a_i 步可以到达的全部点。(60 ~ 63行关键代码,多源bfs,不可分开跑)

    数据:

    4 3 2
    2 1
    1..
    1..
    ..2
    ...

    output:10 2

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define ull unsigned long long
 5 #define mst(a,b) memset((a),(b),sizeof(a))
 6 #define mp(a,b) make_pair(a,b)
 7 #define pi acos(-1)
 8 #define pii pair<int,int>
 9 #define pb push_back
10 #define lowbit(x) ((x)&(-x))
11 const int INF = 0x3f3f3f3f;
12 const double eps = 1e-6;
13 const int maxn = 1e3 + 10;
14 const int maxm = 1e6 + 10;
15 const ll mod =  1e9 + 7;
16
17 int n,m,p;
18 char s[maxn][maxn];
19 int a[15],vis[maxn][maxn];
20 int dx[4] = {-1,0,0,1};
21 int dy[4] = {0,-1,1,0};
22
23 struct node {
24     int x,y,d;
25 };
26
27 bool judge(int x,int y) {
28     if(x <= 0 || x > n || y <= 0 || y > m || s[x][y] == ‘#‘) return false;
29     return true;
30 }
31
32 int main() {
33 #ifdef local
34     freopen("data.txt", "r", stdin);
35 //    freopen("data.txt", "w", stdout);
36 #endif
37     scanf("%d%d%d",&n,&m,&p);
38     for(int i = 1; i <= p; i++) scanf("%d",&a[i]);
39     for(int i = 1; i <= n; i++) {
40         scanf("%s",s[i] + 1);
41         for(int j = 1; j <= m; j++) vis[i][j] = -1;
42     }
43     queue<pii>q;
44     for(int k = 1; k <= p; k++) {
45         for(int i = 1; i <= n; i++) {
46             for(int j = 1; j <= m; j++) {
47                 if(s[i][j] - ‘0‘ == k) {
48                     q.push(mp(i,j));
49                     vis[i][j] = k;
50                 }
51             }
52         }
53     }
54     while(!q.empty()) {
55         pii now = q.front();
56         q.pop();
57         int x = now.first, y = now.second;
58         queue<node>q1;
59         q1.push({x,y,a[vis[x][y]]});
60         while(!q.empty() && vis[q.front().first][q.front().second] == vis[x][y]) {
61             q1.push({q.front().first,q.front().second,a[vis[x][y]]});
62             q.pop();
63         }
64         while(!q1.empty()) {
65             node hh = q1.front();
66             q1.pop();
67             if(hh.d <= 0) continue;
68             for(int i = 0; i < 4; i++) {
69                 int nx = hh.x + dx[i], ny = hh.y + dy[i], d = hh.d;
70                 if(judge(nx,ny) && vis[nx][ny] == -1) {
71                     vis[nx][ny] = vis[x][y];
72                     q1.push({nx,ny,d - 1});
73                     q.push(mp(nx,ny));
74                 }
75             }
76         }
77     }
78     int ans[15] = {0};
79     for(int i = 1; i <= n; i++) {
80         for(int j = 1; j <= m; j++) {
81             if(vis[i][j] != -1) ans[vis[i][j]]++;
82         }
83     }
84     for(int i = 1; i <= p; i++) printf("%d ",ans[i]);
85     return 0;
86 }

原文地址:https://www.cnblogs.com/scaulok/p/10296829.html

时间: 2024-10-08 11:00:19

Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)的相关文章

Codeforces Round #262 (Div. 2) 460B. Little Dima and Equation(枚举)

题目链接:http://codeforces.com/problemset/problem/460/B B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little Dima misbehaved during a math lesson a lot and the nas

Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号个数比右括号多 2 2)在这个位置之前,所有位置的前缀左括号个数都不少于前缀右括号个数 3)在这个位置和这个位置之后,在修改后所有位置的前缀左括号个数减去前缀右括号个数大于2 (这里这么想,把左变成右,左-1,右+1) 右括号也是这样 代码: #include<bits/stdc++.h> usi

Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring" int r,c; char map[55][55]; int vis[55][55]; int mark; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; int judge(int x,int y) { if(x<0||x>=r||y<0||y>

【思维】Codeforces Round #485 (Div. 2) B. High School: Become Human(对数)

题目链接:http://codeforces.com/contest/987/problem/B 在运算的时候取对数就好了 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define ll long long 6 #define eps 1e-6 7 8 int main() 9 { 10 ll x,y; 11 double xx; 12 scanf("%lld %lld",&x,&y);

Codeforces Round #603 (Div. 2) C. Everyone is a Winner! (数学)

链接: https://codeforces.com/contest/1263/problem/C 题意: On the well-known testing system MathForces, a draw of n rating units is arranged. The rating will be distributed according to the following algorithm: if k participants take part in this event, t

Codeforces Round #410 (Div. 2)C. Mike and gcd problem(数论)

传送门 Description Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful.

Codeforces Round #342 (Div. 2) B. War of the Corporations(贪心)

传送门 Description A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000. This new devic

Codeforces Round #342 (Div. 2) A. Guest From the Past(贪心)

传送门 Description Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the details of buying this delicious drink. One day, as you probably know, he found himself in year 2084, and buying kefir there is much more complicated. Koly

Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)

题目链接 Description Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i andj(1 ≤ i, j ≤ n), such that si > wi and sj < wj. Here sign si represents the i-th digit of string s,