Triangle War
Time Limit:1000MS |
|
Memory Limit:65536K |
Total Submissions:2508 |
|
Accepted:983 |
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
4
6
2 4
4 5
5 9
3 6
2 5
3 5
7
2 4
4 5
5 9
3 6
2 5
3 5
7 8
6
1 2
2 3
1 3
2 4
2 5
4 5
10
1 2
2 5
3 6
5 8
4 7
6 10
2 4
4 5
4 8
7 8
Sample Output
Game 1: B wins.
Game 2: A wins.
Game 3: A wins.
Game 4: B wins.
#include<iostream>
using namespace std;
int lines[19][2]={{0,0},
{2,3},{1,2},{1,3},
{2,4},{2,5},{4,5},
{3,5},{3,6},{5,6},
{4,7},{4,8},{7,8},
{5,8},{5,9},{8,9},
{6,9},{9,10},{6,10}
};
int linesvisit[20];
int trians[10][3]={{0,0,0},
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12},
{13,14,15},
{16,17,18},
{1,5,7},
{6,11,13},
{9,14,16}
};
int dp(){
int j;
for(j=1;j<=18;j++){
if(!linesvisit[j])
break;
}
if(j>18)
return 0;
else{
int max=0,maxIndex=j;
for(j=1;j<=18;j++){
if(linesvisit[j])
continue;
linesvisit[j]=1;
int val=0;
for(int k=1;k<=9;k++){
int count=0,flag=0;
for(int l=0;l<3;l++){
if(trians[k][l]==j)
flag=true;
if(linesvisit[trians[k][l]])
count++;
}
if(count==3&&flag)
val++;
}
if(val>max){
max=val;
maxIndex=j;
}
linesvisit[j]=0;
}
linesvisit[maxIndex]=1;
if(max>0){
return max+dp();
}else{
return max-dp();
}
}
}
int getVal(int from,int to){
int i,j,k,val=0;
for(i=1;i<=18;i++)
if(from==lines[i][0]&&to==lines[i][1])
break;
for(j=1;j<=9;j++){
int count=0,flag=0;
for(k=0;k<3;k++){
if(trians[j][k]==i){
flag=1;
}
if(linesvisit[trians[j][k]])
count++;
}
if(flag&&count==2)
val++;
}
linesvisit[i]=1;
return val;
}
int main(){
int linesnum,from,to,value=0,which=1;
cin>>linesnum;
for(int i=1;i<=18;i++)
linesvisit[i]=0;
for(int i=1;i<=linesnum;i++){
cin>>from>>to;
int v=getVal(from,to);
if(v>0){
value+=which*v;
}else{
which*=-1;
}
}
value+=which*dp();
cout<<value<<endl;
}
ps:这里有一些问题,第一组测试数据在我自己测试的时候,是与我自己程序跑的结果一致的,但是,测试数据的结果与我相反。What the fuck!!另:
这是一道很典型的dp问题,主要思想就是在dp过程中遍历所有的边,得到其中能使player得到的三角形数目最大的边,设为已访问,然后继续下一轮遍历,在dp过程中,通过加减号的变换来完成player的变换。
分享到:
相关推荐
poj1085,记忆化DP+状态压缩求解
poj上只有三道极小极大算法的习题,poj1085是其中之一,并做了相关优化,时间:63ms
poj1085,博弈论的典型最大最小搜索和剪枝
(三角形类)设计一个扩展自抽象类GeometricObject 的新的Triangle 类。绘制Triangle 类和GeometricObject 类的UML图并实现Triangle 类。 编写一个测试程序,提示用户输入三角形的三条边、一种颜色以及一个表明该...
Triangle extends GeometricObject 设计一个名为Triangle的类来继承GeometricObject类。该类包括: 三个名为side1,side2,side3的double类型数据域来表示这个三角形的三条边,它们的默认值是1.0。 一个无参构造...
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26414 Accepted: 15435 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program...
编写一个重载函数,可变输入,可变输出。计算边长分别为a、b和c的一个三角形的周长及面积。公式是p=a+b+c,s=p/2,A=sqrt(s*(s-a)*(s-b)*(s-c))
The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99. 输出 Your program is to write to standard output. The highest sum is written as ...
设计一个Triangle类,通过运算符重载来实现两个三角形的面积相加。 operator + (const Triangle& t1,const Triangle& t2); 如对你有用的话,希望你来下载啊。
triangle-响应式bootstrap模板 适配 pc mobile
sierpinski triangle MATLAB Code
triangle.html
opengl triangle loader
intput a b c three sides,so that work out them could be make a triangle or not,if could be a triangle,the little could get the basic informations about the triangle.The little process is based on MFC.
Papanicolopulos Analytical computation of moderate-degree fully-symmetric cubature rules on the triangle
ray_tracing ground_up triangle_mesh 用三角形网格(Triangle Mesh)分别细分球面和牛角面。 包含完整代码和对比测试图形。
TRIANGLE_INTEGRALS
软件测试三角形问题的经典实现 之JAVA---- triangle代码实现。
资源分类:Python库 所属语言:Python 资源全名:Flask-Triangle-joeflack4-0.5.6.zip 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
用于求三角形的面积和周长包括point点类和triangle类 判断是否是三点一线