poj1085 Triangle War 极大极小

Triangle War
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2171   Accepted: 831

Description

Triangle War is a two-player game played on the following triangular grid:
poj1085 Triangle War 极大极小_第1张图片
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:
poj1085 Triangle War 极大极小_第2张图片
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.

Source

East Central North America 1999
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define M 11
int map[M][M]=
{{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0},
{0,0,0,2,3,4,0,0,0,0,0},
{0,1,2,0,0,5,6,0,0,0,0},
{0,0,3,0,0,7,0,9,10,0,0},
{0,0,4,5,7,0,8,0,11,12,0},
{0,0,0,6,0,8,0,0,0,13,14},
{0,0,0,0,9,0,0,0,15,0,0},
{0,0,0,0,10,11,0,15,0,16,0},
{0,0,0,0,0,12,13,0,16,0,17},
{0,0,0,0,0,0,14,0,0,17,0}
};
int tri[9]={7,152,52,352,34304,3200,71680,12544,155648};
/*tri[0]=1|(1<<1)|(1<<2);tri[1]=(1<<3)|(1<<4)|(1<<7);tri[2]=(1<<2)|(1<<4)|(1<<5);
tri[3]=(1<<5)|(1<<6)|(1<<8);tri[4]=(1<<9)|(1<<10)|(1<<15);tri[5]=(1<<7)|(1<<10)|(1<<11);
tri[6]=(1<<11)|(1<<12)|(1<<16);tri[7]=(1<<8)|(1<<12)|(1<<13);tri[8]=(1<<13)|(1<<14)|(1<<17);*/

const int mirror=(1<<18)-1;

int MinSearch(int ,int ,int ,int );
int MaxSearch(int ,int ,int ,int );

int Place(int s,int seg,int &of)
{
	int i;
	int r=s|seg;
	for(i=0;i<9;i++){
		if(((tri[i]&s)!=tri[i])&&((tri[i]&r)==tri[i]))
			of++;
	}
	return r;
}
int MaxSearch(int statu,int alpha,int as,int bs)
{

	int bit,seg;
	int cs,r;
	int max=-1,tmp;
	if(as>=5) return 1;
	if(bs>=5) return -1;
	if(statu==mirror)
		return as>bs?1:-1;

	bit=(~statu)&mirror;

	while(bit){
		seg=bit&(-bit);
		cs=as;
		r=Place(statu,seg,cs);
		if(cs>as)
			tmp=MaxSearch(r,alpha,cs,bs);
		else
			tmp=MinSearch(r,max,cs,bs);

		if(tmp>max)
			max=tmp;
		if(max>=alpha)///
			return max;
		bit-=seg;
	}
	return max;
}
int MinSearch(int statu,int beta,int as,int bs)
{
	int bit,seg,cs,r;
	int min=1,tmp;
	if(as>=5) return 1;
	if(bs>=5) return -1;
	if(statu==mirror)
		return as>bs?1:-1;
	bit=(~statu)&mirror;
	while(bit){
		seg=bit&(-bit);//最右边的1
		cs=bs;
		r=Place(statu,seg,cs);
		if(cs>bs)
			tmp=MinSearch(r,beta,as,cs);
		else
			tmp=MaxSearch(r,min,as,cs);
		if(tmp<min) min=tmp;
		if(tmp<=beta)///
			return min;
		bit-=seg;
	}
	return min;
}

int main()
{
	int t,k=0;
	int a,b,n,i,turn;
	int res;
	int ofa,ofb;
	int ta,tb;
	int status;
	scanf("%d",&t);
	while(t--){

		scanf("%d",&n);

		status=ofa=ofb=0;

		for(turn=i=0;i<n;i++){
			scanf("%d%d",&a,&b);
			ta=ofa,tb=ofb;
			status=Place(status,1<<map[a][b],(turn&1)?ofb:ofa);
			if(ta==ofa&&tb==ofb)
				turn++;
		}
		if(turn&1)
			res=MinSearch(status,-1,ofa,ofb);
		else
			res=MaxSearch(status,1,ofa,ofb);
		printf("Game %d: %c wins.\n",++k,res==1?'A':'B');
	}
	return 0;
}


你可能感兴趣的:(c,Integer,ini,input,each)