CF1105D Kilani and the Game
Posted wangyiming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于CF1105D Kilani and the Game的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 1105D(Kilani and the Game,双队列bfs)
Codeforces 1105D(Kilani and the Game,双队列bfs)
Codeforces 1105D Kilani and the GameBFS
Kilani and the Game-吉拉尼的游戏 CodeForce#1105d 模拟 搜索