Life Line
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 855 Accepted: 623
Description

Let’s play a new board game “Life Line”.
The number of the players is greater than 1 and less than 10.
In this game, the board is a regular triangle in which many small regular triangles are arranged (See Figure 1). The edges of each small triangle are of the same length.

The size of the board is expressed by the number of vertices on the bottom edge of the outer triangle.For example, the size of the board in Figure 1 is 4.

At the beginning of the game, each player is assigned his own identification number between 1 and 9,and is given some stones on which his identification number is written.

Each player puts his stone in turn on one of the “empty” vertices. An “empty vertex” is a vertex that has no stone on it.

When one player puts his stone on one of the vertices during his turn, some stones might be removed from the board. The player gains points which is equal to the number of the removed stones of himself. The points of a player for a single turn is the points he gained minus the points he lost in that turn.

The conditions for removing stones are as follows :

1.The stones on the board are divided into groups. Each group contains a set of stones whose numbersare the same and placed adjacently. That is, if the same numbered stones are placed adjacently,they belong to the same group.

2.If none of the stones in a group is adjacent to at least one “empty” vertex, all the stones in that group are removed from the board.

Figure 2 shows an example of the groups of stones.

Suppose that the turn of the player ‘4’ comes now. If he puts his stone on the vertex shown in Figure 3a, the conditions will be satisfied to remove some groups of stones (shadowed in Figure 3b). The player gains 6 points, because the 6 stones of others are removed from the board (See Figure 3c).

As another example, suppose that the turn of the player ‘2’ comes in Figure 2. If the player puts his

stone on the vertex shown in Figure 4a, the conditions will be satisfied to remove some groups of stones (shadowed in Figure 4b). The player gains 4 points, because the 4 stones of others are removed. But, at the same time, he loses 3 points, because his 3 stones are removed. As the result, the player’s points of this turn is 4 ? 3 = 1 (See Figure 4c).

When each player puts all of his stones on the board, the game is over. The total score of a player is the summation of the points of all of his turns.

Your job is to write a program that tells you the maximum points a player can get (i.e., the points he gains - the points he loses) in his current turn.

Input

The input consists of multiple data. Each data represents the state of the board of the game still in

progress. The format of each data is as follows.

N C
S1,1
S2,1 S2,2
S3,1 S3,2 S3,3

SN,1 … SN,N

N is the size of the board (3 <= N <= 10). C is the identification number of the player whose turn comes now (1 <= C <= 9). That is, your program must calculate his points in this turn. Si,j is the state of the vertex on the board (0 <= Si,j <= 9). If the value of Si,j is positive, it means that there is the stone numbered by Si,j there. If the value of Si,j is 0, it means that the vertex is “empty”. Two zeros in a line, i.e., 0 0, represents the end of the input.
Output

For each data, the maximum points the player can get in the turn should be output, each in a separate line.
Sample Input

4 4
2
2 3
1 0 4
1 1 4 0
4 5
2
2 3
3 0 4
1 1 4 0
4 1
2
2 3
3 0 4
1 1 4 0
4 1
1
1 1
1 1 1
1 1 1 0
4 2
1
1 1
1 1 1
1 1 1 0
4 1
0
2 2
5 0 7
0 5 7 0
4 2
0
0 3
1 0 4
0 1 0 4
4 3
0
3 3
3 2 3
0 3 0 3
4 2
0
3 3
3 2 3
0 3 0 3
6 1
1
1 2
1 1 0
6 7 6 8
0 7 6 8 2
6 6 7 2 2 0
5 9
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
5 3
3
3 2
4 3 2
4 4 0 3
3 3 3 0 3
0 0
Sample Output

6
5
1
-10
8
-1
0
1
-1
5
0
5

题目的意思就是讲一个游戏的规则,太长了,看图就应该明白了。这个题目数据量小,直接暴力搜索就可以了

#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>using namespace std;
int n,c;
int a[15][15];
int vis[15][15];
int tag;
int _count;
int num[15];
int dir[6][2]={{-1,0},{-1,-1},{0,-1},{0,1},{1,0},{1,1}};
void dfs(int x,int y,int term)
{for(int i=0;i<6;i++){int xx=x+dir[i][0];int yy=y+dir[i][1];if(xx<0||yy<0||xx>n-1||yy>n-1||yy>xx)continue;if(a[xx][yy]!=term&&a[xx][yy]!=0)continue;if(a[xx][yy]==0)tag=1;if(vis[xx][yy])continue;vis[xx][yy]=1;if(a[xx][yy]==term)_count++;dfs(xx,yy,term);}
}
int sove()
{int sum=0;for(int i=0;i<n;i++){for(int j=0;j<=i;j++){if(a[i][j]!=0&&vis[i][j]==0){vis[i][j]=1;tag=0;_count=1;dfs(i,j,a[i][j]);if(tag==0&&a[i][j]!=c)sum+=_count;if(tag==0&&a[i][j]==c)sum-=_count;}}}return sum;
}int main()
{int ans;while(scanf("%d%d",&n,&c)!=EOF){if(n==0&&c==0)break;ans=-9999999;memset(vis,0,sizeof(vis));for(int i=0;i<n;i++){for(int j=0;j<=i;j++){scanf("%d",&a[i][j]);num[a[i][j]]++;}}for(int i=0;i<n;i++){for(int j=0;j<=i;j++){if(a[i][j]==0){a[i][j]=c;num[c]++;memset(vis,0,sizeof(vis));int tmp=sove();if(ans<tmp)ans=tmp;a[i][j]=0;num[c]--;}}}printf("%d\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/dacc123/p/8228841.html

POJ-1414 Life Line (暴力搜索)相关推荐

  1. POJ 1414 Life Line(搜索)

    题意: 给定一块正三角形棋盘,然后给定一些棋子和空位,棋子序号为a(1<=a<=9),group的定义是相邻序号一样的棋子. 然后到C(1<=N<=9)棋手在空位放上自己序号C ...

  2. k-Anonymity(K匿名)之暴力搜索大法

    k-Anonymity(K匿名)之暴力搜索大法 k匿名 k-Anonymity(K匿名)之暴力搜索大法 写在前面 代码 读取文件 分类 距离矩阵 正向搜索 逆向匹配 匿名化处理 写入文件 实验结果 写 ...

  3. C++ 暴力搜索String pattern search字符串模式的实现算法(附完整源码)

    C++暴力搜索String pattern search字符串模式的实现算法 C++暴力搜索String pattern search字符串模式的实现算法完整源码(定义,实现,main函数测试) C+ ...

  4. 搜索 —— 暴力搜索

    [暴力搜索] 暴力搜索,就是将所有情况都举出,并判断其是否符合题目条件.其基本方法是分析题意后,找到一个合适的维度列举每一个元素,以完成题目. 一般主流的 OJ 中,1000ms 的时间限制下可以运行 ...

  5. 2016蓝桥杯C++A组第六题 寒假作业【暴力搜索】

    原题: 现在小学的数学题目也不是那么好玩的. 看看这个寒假作业:□ + □ = □□ - □ = □□ × □ = □□ ÷ □ = □(如果显示不出来,可以参见[图1.jpg])每个方块代表1~13 ...

  6. POJ 1321-棋盘问题-简单搜索DFS

    POJ 1321-棋盘问题-简单搜索DFS Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编 ...

  7. 暴力搜索---新技能get

    最近新学习了一种新的求解的方法,就是暴力搜索,在通常做题没有很明确的思路的时候,通常都会采用的一种方式. 我们知道,一个问题的解空间通常对应的是一棵树的方式进行组织的,那么我们可以通过根据题目中的条件 ...

  8. 算法模版:暴力搜索之DFS【沈七】

    本文已收录于专栏 ⭐️ <算法通关笔记>⭐️ 算法模版:暴力搜索之DFS 前言 基本概念 算法思想 常用模板 三种枚举方式 指数型枚举 排列型枚举 组合型枚举 完结散花 题目练习 参考文章 ...

  9. 2019 蓝桥杯省赛 A 组模拟赛(一)C. 结果填空:马的管辖 (暴力搜索)

    题目: 在中国象棋中,马是走日字的.一个马的管辖范围指的是当前位置以及一步之内能走到的位置,下图的绿色旗子表示马能走到的位置. 如果一匹马的某个方向被蹩马脚,它就不能往这个方向跳了,如下图所示,海星的 ...

  10. 过河系列--暴力搜索

    人鬼过河: 三个传道士和三个食人族土著 如何全部渡船到对岸 限制: 船最多载2人 当一边土著多于传道士时 土著会将传道士吃掉 5人过桥: 5个人通过的时间分别为1.3.6.8.12, 只有一盏30秒的 ...

最新文章

  1. Linux那些事儿 之 戏说USB(15)设备
  2. 1008: [HNOI2008]越狱(计数问题)
  3. 某石油公司100-500人办公网络方案设计
  4. Spring JDBC-事务方法嵌套调用解读
  5. 取代C语言标准输入输出:cin 和 cout【C++标准输入输出】
  6. SendGrid是如何扩展它的邮件传送系统的
  7. 接口测试Fiddler实战
  8. linux gettimeofday 头文件,linux-时间编程-time、gmtime、localtime、asctime、ctime、gettimeofday、sleep、usleep...
  9. Apache Ignite(七):基于Ignite的企业级分布式并行计算
  10. 多元统计分析 (一):聚类分析
  11. C语言职工工资管理系统
  12. 入手华为云学生机体验
  13. 锁屏后重新登录程序无响应问题分析
  14. uniapp路线规划
  15. 网传学习通1.7亿密码泄露:有什么补救措施?
  16. 二维条码防伪技术原理及应用简介
  17. 纵观计算机简史ppt,纵观计算机简史初中信息技术新课本中国地图出社..ppt
  18. 在线教育项目技术笔记1
  19. 鸡啄米VS2010/MFC编程入门教程——学习1初次接触
  20. 基于RealSense的坐姿检测技术

热门文章

  1. 浅谈Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路
  2. Linux kernel 3.10内核源码分析--进程退出exit_code
  3. 使用photoview+viewpager实现图片缩放切换(类似微信朋友圈图片查看)
  4. 服务器网站数据库如何保存,服务器怎么保存数据库
  5. 浏览器控制台执行代码_JavaScript 和 浏览器那些事
  6. python四大高阶函数_详谈Python高阶函数与函数装饰器(推荐)
  7. mac 当前文件夹打开终端_Mac上的这些实用你技巧,你知道几个?
  8. 26个音序的正确写法和占格_别让瞎练毁了健康,26个健身动作正误对比图,让我们正确健身...
  9. android uri转drawable,Glide4(URL转File,URL转Drawable)
  10. 网易游戏2019暑期实习开发岗笔试题