CF1105D Kilani and the Game

思路:

模拟多源bfs。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int, int> pii;
 4 char a[1005][1005];
 5 const int dx[4] = {1, 0, -1, 0};
 6 const int dy[4] = {0, 1, 0, -1};
 7 int s[10], ans[10], l[10], d[1005][1005];
 8 inline bool check(int x, int y, int n, int m)
 9 {
10     return x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] == ‘.‘;
11 }
12 int main()
13 {
14     int n, m, p;
15     while (cin >> n >> m >> p)
16     {
17         memset(ans, 0, sizeof ans);
18         memset(d, 0, sizeof d);
19         memset(l, 0, sizeof l);
20         for (int i = 0; i < p; i++) cin >> s[i];
21         vector<queue<pii>> v(p);
22         for (int i = 1; i <= n; i++)
23         {
24             for (int j = 1; j <= m; j++)
25             {
26                 cin >> a[i][j];
27                 if (a[i][j] >= ‘1‘ && a[i][j] <= ‘9‘)
28                 {
29                     v[a[i][j] - ‘1‘].push(make_pair(i, j));
30                 }
31             }
32         }
33         while (true)
34         {
35             bool flg = true;
36             for (int i = 0; i < p; i++)
37             {
38                 while (!v[i].empty())
39                 {
40                     pii t = v[i].front();
41                     int x = t.first, y = t.second;
42                     if (d[x][y] >= l[i] + s[i]) break;
43                     v[i].pop();
44                     for (int k = 0; k < 4; k++)
45                     {
46                         int nx = x + dx[k], ny = y + dy[k];
47                         if (check(nx, ny, n, m))
48                         {
49                             v[i].push(make_pair(nx, ny));
50                             a[nx][ny] = char(‘1‘ + i);
51                             d[nx][ny] = d[x][y] + 1;
52                         }
53                     }
54                 }
55                 l[i] += s[i];
56                 if (v[i].size() > 0) flg = false;
57             }
58             if (flg) break;
59         }
60         for (int i = 1; i <= n; i++)
61         {
62             for (int j = 1; j <= m; j++)
63             {
64                 if (a[i][j] >= ‘1‘ && a[i][j] <= ‘9‘) ans[a[i][j] - ‘0‘]++;
65             }
66         }
67         for (int i = 1; i <= p; i++) cout << ans[i] << " ";
68         cout << endl;
69     }
70     return 0;
71 }

原文地址:https://www.cnblogs.com/wangyiming/p/10848604.html

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

CF1105D Kilani and the Game的相关文章

Kilani and the Game-吉拉尼的游戏 CodeForce#1105d 模拟 搜索

题目链接:Kilani and the Game 题目原文 Kilani is playing a game with his friends. This game can be represented as a grid of size ??×??, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castle

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 <

Codeforces 1105D Kilani and the Game【BFS】

<题目链接> 题目大意: 每个玩家控制一个颜色去扩张,每个颜色的扩张有自己的速度,一个颜色跑完再跑下一种颜色.在所有颜色不能在继续扩张的时候停止游戏.询问此时各种颜色的数量. 解题分析: 就是用BFS去模拟颜色的扩张,但是需要注意的是,本题需要加一些小的优化,比如,每次只用扩张上一轮BFS新更新的点就行. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N int(1e3+7) 5 #define rep(

Codeforces Round #533 (Div. 2)

A. Salem and Sticks 由于长度很小,所以直接暴力枚举最后的长度即可,取最小值即可. #include<bits/stdc++.h> #define CLR(a,b) memset(a,b,sizeof(a)); using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const int maxn=10010; int a[1100],n; int cost,ans; int main(){ ci

Codeforces Round #533 (Div. 2) Solution

A. Salem and Sticks 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 1010 5 int n, a[N]; 6 7 int work(int x) 8 { 9 int res = 0; 10 for (int i = 1; i <= n; ++i) 11 res += max(0, abs(x - a[i]) - 1); 12 return res; 13 } 14 15 int m

Codeforces Round #533 (Div. 2) ABCD 题解

题目链接 A. Salem and Sticks 分析 暴力就行,题目给的n<=1000,ai<=100,暴力枚举t,t从2枚举到98,复杂度是1e5,完全可行. 代码 1 #include <cstdio> 2 #include <cmath> 3 #include <iostream> 4 #include <cstring> 5 #include <algorithm> 6 #include <vector> 7 #