Triangle War
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2685   Accepted: 1061

Description

Triangle War is a two-player game played on the following triangular grid: 


Two players, A and B, take turns filling in any dotted line connecting two dots, with A starting first. Once a line is filled, it cannot be filled again. If the line filled by a player completes one or more triangles, she owns the completed triangles and she
is awarded another turn (i.e. the opponent skips a turn). The game ends after all dotted lines are filled in, and the player with the most triangles wins the game. The difference in the number of triangles owned by the two players is not important. 

For example, if A fills in the line between 2 and 5 in the partial game on the left below: 


Then, she owns the triangle labelled A and takes another turn to fill in the line between 3 and 5. B can now own 3 triangles (if he wishes) by filling in the line between 2 and 3, then the one between 5 and 6, and finally the one between 6 and 9. B would then
make one more move before it is A's turn again. 
In this problem, you are given a number of moves that have already been made. From the partial game, you should determine which player will win assuming that each player plays a perfect game from that point on. That is, assume that each player always chooses
the play that leads to the best possible outcome for himself/herself.

Input

You will be given a number of games in the input. The first line of input is a positive integer indicating the number of games to follow. Each game starts with an integer 6 <= m <= 18 indicating the number of moves that have been made in the game. The next
m lines indicate the moves made by the two players in order, each of the form i j (with i < j) indicating that the line between i and j is filled in that move. You may assume that all given moves are legal.

Output

For each game, print the game number and the result on one line as shown below. If A wins, print the sentence "A wins." If B wins, print "B wins."

Sample Input

  1. 4
  2. 6
  3. 2 4
  4. 4 5
  5. 5 9
  6. 3 6
  7. 2 5
  8. 3 5
  9. 7
  10. 2 4
  11. 4 5
  12. 5 9
  13. 3 6
  14. 2 5
  15. 3 5
  16. 7 8
  17. 6
  18. 1 2
  19. 2 3
  20. 1 3
  21. 2 4
  22. 2 5
  23. 4 5
  24. 10
  25. 1 2
  26. 2 5
  27. 3 6
  28. 5 8
  29. 4 7
  30. 6 10
  31. 2 4
  32. 4 5
  33. 4 8
  34. 7 8

Sample Output

  1. Game 1: B wins.
  2. Game 2: A wins.
  3. Game 3: A wins.
  4. Game 4: B wins.

Source

题意:

两个人玩游戏,依次在三角形上放边,假设能构成三角形。则奖励继续该此人放,问最后得到的三角形多。

思路:

给边编号,记忆化搜索即可。做过好多这样的题。就不多写思路了。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <map>
  8. #include <stack>
  9. #include <vector>
  10. #include <set>
  11. #include <queue>
  12. #pragma comment (linker,"/STACK:102400000,102400000")
  13. #define maxn 1005
  14. #define MAXN 50005
  15. #define mod 1000000009
  16. #define INF 0x3f3f3f3f
  17. #define pi acos(-1.0)
  18. #define eps 1e-6
  19. typedef long long ll;
  20. using namespace std;
  21. int n,m,ans,cnt,tot,flag;
  22. bool vis[10];
  23. int dp[300000],mp[15][15],sc[5];
  24. int tri[9][3]=
  25. {
  26. 0,1,2,3,4,7,2,4,5,5,6,8,9,10,15,
  27. 7,10,11,11,12,16,8,12,13,13,14,17
  28. };
  29. int cal(int s)
  30. {
  31. int i,j,t=0;
  32. for(j=0; j<9; j++)
  33. {
  34. if((s&(1<<tri[j][0]))&&(s&(1<<tri[j][1]))&&(s&(1<<tri[j][2]))) t++;
  35. }
  36. return t;
  37. }
  38. int dfs(int state,int score)
  39. {
  40. if(dp[state]!=-1) return dp[state];
  41. int i,j,t,tst,num,best=0,tmp;
  42. num=9-score;
  43. for(i=0; i<=17; i++)
  44. {
  45. if(state&(1<<i)) continue ;
  46. tst=state|(1<<i);
  47. t=cal(tst);
  48. if(t>num)
  49. {
  50. tmp=t-num+dfs(tst,score-(t-num));
  51. best=max(best,tmp);
  52. }
  53. else
  54. {
  55. tmp=score-dfs(tst,score);
  56. best=max(best,tmp);
  57. }
  58. }
  59. dp[state]=best;
  60. return best;
  61. }
  62. int main()
  63. {
  64. int i,j,t,test=0;
  65. mp[1][2]=0;mp[1][3]=1;mp[2][3]=2;mp[2][4]=3;mp[2][5]=4;mp[3][5]=5;
  66. mp[3][6]=6;mp[4][5]=7;mp[5][6]=8;mp[4][7]=9;mp[4][8]=10;mp[5][8]=11;
  67. mp[5][9]=12;mp[6][9]=13;mp[6][10]=14;mp[7][8]=15;mp[8][9]=16;mp[9][10]=17;
  68. scanf("%d",&t);
  69. while(t--)
  70. {
  71. scanf("%d",&n);
  72. tot=0;
  73. int x,y,z,turn=0,num=0;
  74. sc[0]=sc[1]=0;
  75. memset(vis,0,sizeof(vis));
  76. for(i=1; i<=n; i++)
  77. {
  78. scanf("%d%d",&x,&y);
  79. z=mp[x][y];
  80. tot|=(1<<z);
  81. flag=0;
  82. for(j=0; j<9; j++)
  83. {
  84. if(vis[j]) continue ;
  85. if((tot&(1<<tri[j][0]))&&(tot&(1<<tri[j][1]))&&(tot&(1<<tri[j][2])))
  86. {
  87. vis[j]=1;
  88. num++;
  89. flag=1;
  90. sc[turn]++;
  91. }
  92. }
  93. if(!flag) turn^=1;
  94. }
  95. memset(dp,-1,sizeof(dp));
  96. z=dfs(tot,9-num);
  97. sc[turn]+=z;
  98. sc[turn^1]+=(9-num-z);
  99. if(sc[0]>sc[1]) printf("Game %d: A wins.\n",++test);
  100. else printf("Game %d: B wins.\n",++test);
  101. }
  102. return 0;
  103. }

poj 1085 Triangle War (状压+记忆化搜索)的更多相关文章

  1. Luogu P2831 愤怒的小鸟(状压+记忆化搜索)

    P2831 愤怒的小鸟 题意 题目描述 Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于\((0,0)\)处,每次Kiana可以用它向第一象限发射 ...

  2. poj 1085 Triangle War 博弈论+记忆化搜索

    思路:总共有18条边,9个三角形. 极大极小化搜索+剪枝比较慢,所以用记忆化搜索!! 用state存放当前的加边后的状态,并判断是否构成三角形,找出最优解. 代码如下: #include<ios ...

  3. POJ 1191 棋盘分割 【DFS记忆化搜索经典】

    题目传送门:http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS   Memory Limit: 10000K Total Submission ...

  4. POJ 1579 Function Run Fun 【记忆化搜索入门】

    题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  5. (中等) POJ 1054 The Troublesome Frog,记忆化搜索。

    Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a we ...

  6. POJ 3249 Test for Job (记忆化搜索)

    Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11830   Accepted: 2814 Des ...

  7. HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索

    题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析:  枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...

  8. poj 1088 滑雪(区间dp+记忆化搜索)

    题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程 ...

  9. POJ 1088: 滑雪(经典 DP+记忆化搜索)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 74996   Accepted: 27818 Description ...

随机推荐

  1. win10系统下安装打印机驱动

    以前安装过一次打印机的驱动,当时是从网上下载的,今天按照以前的方法安装打印机驱动,发现并不能使用,而且并不知道驱动还能自动安装. 首先在系统图标下选择设置-设备和打印机-添加打印机-搜索打印机,如果没 ...

  2. g++编译多个源原文件和头文件(转载)

    (头文件名是:Sales_item.hpp 源文件名:7-31.cpp和Sales_item.cpp)(点击我下载源文件) 方法1: 我用的命令是:g++ -o 7-31 7-31.cpp Sales ...

  3. Jboss配置HTTPS

    配置jboss的HTTP请求走SSL(HTTPS协议) l         生成keystore 文件 用keytool生成server.keystore文件: 进入命令行 C:\Documents ...

  4. 为什么打不开IDEA或webStorm官方网页?

    为什么打不开IDEA或webStorm官方网页? 一.问题描述 idea和webStorm的官网:https://www.jetbrains.com/ 有时候打开idea的官网会出现无法访问的情况,页 ...

  5. Django+小程序技术打造微信小程序助手

    Django+小程序技术打造微信小程序助手   整个课程都看完了,当前这个课程的分享可以往下看,下面有某盘的链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,同时也分享下自己的总结 ...

  6. Oracle锁表查杀会话进程

    一.逐条--锁表 (1)查表名 和 sessionidselect b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_obj ...

  7. python 常见分布的产生方式

  8. vue+ElementUI项目中,input只能输入正整数的验证

    代码如下:     <el-input  v-model="famount" placeholder="请输入内容"   @keyup.native=&q ...

  9. Laravel5.5/6 报错call to undefined function openssl cipher iv length()

    在安装laravel5.5后, 访问显示报错. call to undefined function openssl cipher iv length() 经查为php7.1的OpenSSL扩展加载失 ...

  10. 关于chrome浏览器的帐号密码自动填充以及出现的黄色背景色填充问题

    不知道大家平时做项目的时候有木有关注这个问题,其实之前做项目遇到过类似的问题,但是因为是单独的chrome浏览器的填充,而且是样式问题稍微严重点,也就没在意.然而在近期的项目中有遇到了这个问题,最为一 ...