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 }

原文地址:https://www.cnblogs.com/SaltyFishQF/p/10327792.html

时间: 2024-10-30 01:32:03

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

一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)

最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim Game): 桌子上有一堆石块,你和你的朋友轮流去拿这些石块,每次只能拿1块.2块或者3块.在石块被拿光前,最后一次拿到石块的人获胜.你将首先去拿这些石块. 你和你的朋友都非常聪明,并且拥有应对该游戏的最佳策略.写一个函数来决定在给定石块数量的情况下,你是否能够获胜.比如:如果桌子上有4块石块,那么

nyoj 就拿石头(三)(尼姆游戏定理的使用)

就拿石头(三) 时间限制:1000 ms |  内存限制:1000 KB 难度:6 描写叙述 小王喜欢与同事玩一些小游戏,今天他们选择了玩取石子. 游戏规则例如以下:共同拥有N堆石子,已知每堆中石子的数量,两个人轮流取子,每次仅仅能选择N堆石子中的一堆,取一定数量的石子(最少取一个),取过子之后.还能够将该堆石子中剩下的随意多个石子中随意选取几个放到其他的随意一堆或几堆上. 等哪个人无法取子时就表示此人输掉了游戏. 注意,一堆石子没有子之后.就不能再往此处放石子了. 如果每次都是小王先取石子,而

下拉框的搜索(有图,适用下拉选择内容较多,需要搜索,自动联想)

由于下拉菜单选项较多,需要在下拉菜单中加入搜索功能,而且具有搜索词联想功能.本文采用了select2.js插件,在引入后简单配置就可以使用 view中 AppAsset::register($this); //引入select2插件实现下拉搜索功能 AppAsset::addScript($this,"js/plugins/select2-master/dist/js/select2.min.js"); AppAsset::addScript($this,"js/plugin

[LeetCode] Nim Game 尼姆游戏

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove th

拉米纸牌游戏开发之路学习篇2

在篇1中的Deck类中表示一副纸牌采用的是Card数组的方法,该种方法灵活性较小,在纸牌游戏中经常会用到一组纸牌的操作,在此有必要创建纸牌集合类Cards 1.通过继承集合类实现 首先介绍System.Collection空间中的ArryList类吧,该类继承了IList接口,可以动态添加或者移去元素,很好用.对之前的代码只需略加修改.具体修改如下: (1)处,需要修改Deck类的字段 //private Card[] _Cards=new Card[52]; private ArrayList

【POJ3480】John 博弈 Anti-SG mis&#232;re规则尼姆游戏

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42671843 其实我就是觉得原创的访问量比未授权盗版多有点不爽233... 题意:跟NIM游戏差不多,不过是谁不能操作了,谁赢. 定理: NIM游戏规则取最后一个石子输 适用范围:对于任意一个Anti-SG 游戏,当局面中所有的单一游戏 的SG值为0时,游戏结束. (1)SG==0,有某单一游戏的SG>1.(败) (2)SG!=0,有某单一游戏的SG>1.(胜) (3)SG==0,无某

[LeetCode]-292. Nim Game(Easy)(C + 尼姆游戏)

292. Nim Game My Submissions Question Editorial Solution Total Accepted: 67907 Total Submissions: 128306 Difficulty: Easy You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns

bzoj3884: 上帝与集合的正确用法 欧拉降幂公式

欧拉降幂公式:http://blog.csdn.net/acdreamers/article/details/8236942 糖教题解处:http://blog.csdn.net/skywalkert/article/details/43955611 注:知道欧拉公式是远远不够的,还要知道欧拉降幂公式,因为当指数很大的时候需要用 然后欧拉降幂公式不要求A,C互质,但是B必须大于等于C的欧拉函数 吐槽:感觉记忆化搜索影响不大啊,当然肯定是因为太水了 这样复杂度是O(T*sqrt(p)*logp)

JS实现下拉刷新和上拉加载

<!DOCTYPE html><html><head> <title></title> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui"> <style type="text/css"> #slideDown{margin-t