链接:https://codeforces.com/contest/1105/problem/D
题意:
给n*m的地图,最多9个人,同时有每个人的扩张次数(我开始以为是直线扩张最大长度。。实际是能连续扩张次数。)
地图上有‘#’,‘.',和数字,数字对应每个人的据点,
从1-n轮流扩张。
地图被扩张完后,输入每个人的据点数目。
思路:
赛后写的题。还一堆bug,
用队列和一个数组,记录每个人能扩张的点和下一次能扩张的个数。
然后就是一堆循环套着。每次入队更新下一次的扩张个数,同时用flag记录有几个人还可以扩张。
不能扩张就减一。
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
struct Node
{
int _x;
int _y;
Node(int x,int y):_x(x),_y(y){}
};
int Next[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int next_time[10];
int Map[MAXN][MAXN];
int number[10];
int speed[10];
int vis[10];
queue<Node> player[10];
int main()
{
int n, m, p;
char c;
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= p; i++)
{
scanf("%d", &speed[i]);
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> c;
if (c == '.')
Map[i][j] = 0;
else if (c == '#')
Map[i][j] = -1;
else
{
Map[i][j] = c - '0';
player[c - '0'].push(Node(i, j));
number[c - '0']++;
next_time[c - '0']++;
}
}
}
int flag = p;
while (flag > 0)
{
for (int i = 1; i <= p; i++)
{
if (player[i].empty())
continue;
for (int times = 1; times <= speed[i]; times++)
{
int ti = next_time[i];
next_time[i] = 0;
for (int z = 1; z <= ti; z++)
{
//cout << 2 << endl;
int x = player[i].front()._x;
int y = player[i].front()._y;
player[i].pop();
for (int j = 0; j < 4; j++)
{
int tx = x + Next[j][0];
int ty = y + Next[j][1];
if (tx < 1 || tx > n || ty < 1 || ty > m)
continue;
if (Map[tx][ty] != 0 || Map[tx][ty] == '#')
continue;
Map[tx][ty] = i;
number[i]++;
player[i].push(Node(tx, ty));
next_time[i]++;
}
}
if (player[i].empty()&&vis[i] == 0)
{
flag--;
vis[i] = 1;
break;
}
/*
for (int v = 1;v<=n;v++)
{
for (int c = 1;c<=m;c++)
cout << Map[v][c] << ' ';
cout << endl;
}
*/
}
//cout << 3 << endl;
//cout << player[i].size() << endl;
}
if (flag <= 0)
break;
//cout << 4 << endl;
}
for (int i = 1; i <= p; i++)
printf("%d ", number[i]);
printf("\n");
return 0;
}