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 castles in one cell).

The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player 𝑖 can expand from a cell with his castle to the empty cell if it's possible to reach it in at most 𝑠𝑖 (where 𝑠𝑖 is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.

The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.

题目大意

模拟一个占领游戏,每个玩家每个回合可以占领领地范围内上下左右走speed步可达的地方。每个玩家轮流开始占领。输出最终每个玩家各占领了多少领地。

思路

占领的速度指的是一次占领时,能走的距离。其实是和以速度为1占领n次等价的。

最近搜索题做的比较多,就用队列来存了每次新占领的区域。下次只要用新占领的区域往下走就行了。

题目本身不难,只要注意几点就行了。

1、如果一个玩家在一次占领的时候没有可以占领的区域,这个玩家在以后的回合直接不参与。

2、如果在一个回合内所有的玩家都没有占领任何一个区域,那么游戏直接停止。输出结果。

3、开始的时候玩家拥有的城市数量不一定是一个。

题解

  1 #include <iostream>
  2 #include <cstring>
  3 #include <queue>
  4 using namespace std;
  5 
  6 char map[1005][1005];
  7 int speed[10], player[10];
  8 bool can_expand[10];
  9 int n, m, p, tot;
 10 
 11 struct point
 12 {
 13     int x, y;
 14 };
 15 
 16 queue<point> q[10];
 17 
 18 void expand(int playern)
 19 {
 20     //第n个玩家扩张
 21     bool flag = true;
 22     queue<point> q1;
 23     while(!q[playern].empty())
 24     {
 25         if(tot == 0)
 26             break;
 27         point p2;
 28         point p = q[playern].front();
 29         //
 30         if(p.x - 1 >= 0 && map[p.x - 1][p.y] == '.')
 31         {
 32             p2.x = p.x - 1;
 33             p2.y = p.y;
 34             map[p2.x][p2.y] = playern+48;
 35             tot--;
 36             q1.push(p2);
 37             player[playern]++;
 38             flag = false;
 39         }
 40         //
 41         if(p.x + 1 < n && map[p.x + 1][p.y] == '.')
 42         {
 43             p2.x = p.x + 1;
 44             p2.y = p.y;
 45             map[p2.x][p2.y] = playern+48;
 46             tot--;
 47             q1.push(p2);
 48             player[playern]++;
 49             flag = false;
 50         }
 51         //
 52         if(p.y - 1 >= 0 && map[p.x][p.y - 1] == '.')
 53         {
 54             p2.x = p.x;
 55             p2.y = p.y - 1;
 56             map[p2.x][p2.y] = playern+48;
 57             tot--;
 58             q1.push(p2);
 59             player[playern]++;
 60             flag = false;
 61         }
 62         //
 63         if(p.y + 1 < m && map[p.x][p.y + 1] == '.')
 64         {
 65             p2.x = p.x;
 66             p2.y = p.y + 1;
 67             map[p2.x][p2.y] = playern+48;
 68             tot--;
 69             q1.push(p2);
 70             player[playern]++;
 71             flag = false;
 72         }
 73         q[playern].pop();
 74     }
 75     q[playern] = q1;
 76     if(flag == true)
 77     {
 78         can_expand[playern] = false;
 79     }
 80 }
 81 
 82 int main(int argc, char const *argv[])
 83 {
 84 #ifdef debug
 85     freopen("test.txt", "r" ,stdin);
 86 #endif
 87     memset(can_expand, true, sizeof(can_expand));
 88     cin >> n >> m >> p;
 89     for(int i = 1; i <= p; i++)
 90     {
 91         cin >> speed[i];
 92     }
 93     tot = n*m - p;
 94     for (int i = 0; i < n; ++i)
 95     {
 96         for (int j = 0; j < m; ++j)
 97         {
 98             cin >> map[i][j];
 99             if(map[i][j] == '#') tot--;
100             if(map[i][j] >= '0' && map[i][j] <= '9')
101             {
102                 player[map[i][j] - 48]++;
103                 point p;
104                 p.x = i;
105                 p.y = j;
106                 q[map[i][j] - 48].push(p);
107             }
108         }
109     }
110 
111     while(tot)
112     {
113         int tmp = tot;
114         for (int i = 1; i <= p; ++i)
115         {
116             if(can_expand[i] == false) continue;
117             for (int j = 0; j < speed[i]; ++j)
118             {
119                 if(can_expand[i] == false) break;
120                 expand(i);
121                 if(tot == 0) break;
122             }
123         }
124         if(tmp == tot) break;
125     }
126     for(int i = 1; i <= p; i++)
127     {
128         cout << player[i] << " ";
129     }
130     return 0;
131 }

 

posted @ 2019-01-27 20:07  SaltyFishQF  阅读(323)  评论(0编辑  收藏  举报