HDU 3683 Gomoku 模拟 胜局判断

Gomoku

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1413    Accepted Submission(s): 362


Problem Description
You are probably not familiar with the title, “Gomoku”, but you must have played it a lot. Gomoku is an abstract strategy board game and is also called Five in a Row, or GoBang. It is traditionally played with go pieces (black and white stones) on a go board (19x19 intersections). Nowadays, standard chessboard of Gomoku has 15x15 intersections. Black plays first, and players alternate in placing a stone of their color on an empty intersection. The winner is the first player to get an unbroken row of five or more stones horizontally, vertically, or diagonally. 
HDU 3683 Gomoku 模拟 胜局判断_第1张图片
For convenience, we coordinate the chessboard as illustrated above. The left-bottom intersection is (0,0). And the bottom horizontal edge is x-axis, while the left vertical line is y-axis. 

I am a fan of this game, actually. However, I have to admit that I don’t have a sharp mind. So I need a computer program to help me. What I want is quite simple. Given a chess layout, I want to know whether someone can win within 3 moves, assuming both players are clever enough. Take the picture above for example. There are 31 stones on it already, 16 black ones and 15 white ones. Then we know it is white turn. The white player must place a white stone at (5,8). Otherwise, the black player will win next turn. After that, however, the white player also gets a perfect situation that no matter how his opponent moves, he will win at the 3rd move. 

So I want a program to do similar things for me. Given the number of stones and positions of them, the program should tell me whose turn it is, and what will happen within 3 moves.
 

Input
The input contains no more than 20 cases.
Each case contains n+1 lines which are formatted as follows.
n
x1 y1 c1
x2 y2 c2
......
xn yn cn
The first integer n indicates the number of all stones. n<=222 which means players have enough space to place stones. Then n lines follow. Each line contains three integers: xi and yi and ci. xi and yi are coordinates of the stone, and ci means the color of the stone. If ci=0 the stone is white. If ci=1 the stone is black. It is guaranteed that 0<=xi,yi<=14, and ci=0 or 1. No two stones are placed at the same position. It is also guaranteed that there is no five in a row already, in the given cases.
The input is ended by n=0.
 

Output
For each test case:

First of all, the program should check whose turn next. Let’s call the player who will move next “Mr. Lucky”. Obviously, if the number of the black stone equals to the number of white, Mr. Lucky is the black player. If the number of the black stone equals to one plus the numbers of white, Mr. Lucky is the white player. If it is not the first situation or the second, print “Invalid.” 

A valid chess layout leads to four situations below:

1)Mr. Lucky wins at the 1st move. In this situation, print :

Place TURN at (x,y) to win in 1 move.

“TURN” must be replaced by “black” or “white” according to the situation and (x,y) is the position of the move. If there are different moves to win, choose the one where x is the smallest. If there are still different moves, choose the one where y is the smallest.

2)Mr. Lucky’s opponent wins at the 2nd move. In this situation, print:

Lose in 2 moves.

3)Mr. Lucky wins at the 3rd move. If so, print:

Place TURN at (x,y) to win in 3 moves.

“TURN” should replaced by “black” or “white”, (x,y) is the position where the Mr. Lucky should place a stone at the 1st move. After he place a stone at (x,y), no matter what his opponent does, Mr. Lucky will win at the 3[sup]rd[sup] step. If there are multiple choices, do the same thing as described in situation 1. 

4)Nobody wins within 3 moves. If so, print:

Cannot win in 3 moves.
 

Sample Input
   
   
   
   
31 3 3 1 3 4 0 3 5 0 3 6 0 4 4 1 4 5 1 4 7 0 5 3 0 5 4 0 5 5 1 5 6 1 5 7 1 5 9 1 6 4 1 6 5 1 6 6 0 6 7 1 6 8 0 6 9 0 7 5 1 7 6 0 7 7 1 7 8 1 7 9 0 8 5 0 8 6 1 8 7 0 8 8 1 8 9 0 9 7 1 10 8 0 1 7 7 1 1 7 7 0 0
 

Sample Output
   
   
   
   
Place white at (5,8) to win in 3 moves. Cannot win in 3 moves. Invalid.
 

Source
2010 Asia Hangzhou Regional Contest

题目大意:给出一个棋局,判断三步之内可能发生的局面。

分析:按照题目给出的,黑棋先走,只可能出现五种局面:

      1.不合法的局面(根据黑白棋的数量判断)

      2.第一步赢(走一步就能凑够五个字)

      3.第二步输(对手至少有两个位置,走这些位置可以凑足五子)

      4.第三步赢(走一步后,出现至少两个位置可以凑足五子,且对手没有这种位置)

      5.没赢也没输(其它情况)

其中,第四种局面容易出错,因为经过上面三步,只去除了开始局面中对手没有一个以上可以凑足五子的位置这种情况,因为双方都足够聪明,所以必须同时保证在走完第一步之后对手没有可以凑足五子的位置。

#include <iostream>
#include <memory.h>
using namespace std;
int board[20][20];
int white_num,black_num;
int n;
int x,y;

int input()
{
    memset(board,-1,sizeof(board));
    white_num=black_num=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>x>>y;
        cin>>board[14-y][x];
        if(board[14-y][x]==0)
            white_num++;
        else
            black_num++;
    }
    return n;
}

int judge_valid()
{
    if(white_num==black_num)
        return 1;
    else if((white_num+1)==black_num)
        return 0;
    else
        return -1;
}

int check_five(int p_i,int p_j,int color)
{
    int i,j,num;
    //主对角
    num=1;
    for(i=p_i-1,j=p_j-1;i>=0&&j>=0&&board[i][j]==color;i--,j--)
    {
        num++;
    }
    for(i=p_i+1,j=p_j+1;i<15&&j<15&&board[i][j]==color;i++,j++)
    {
        num++;
    }
    if(num>=5)
        return 1;

    //副对角线                                    
    num=1;
    for(i=p_i+1,j=p_j-1;i<15&&j>=0&&board[i][j]==color;i++,j--)
    {
        num++;
    }
    for(i=p_i-1,j=p_j+1;i>=0&&j<15&&board[i][j]==color;i--,j++)
    {
        num++;
    }
    if(num>=5)
        return 1;
    //横向
    num=1;
    for(i=p_i,j=p_j-1;j>=0&&board[i][j]==color;j--)
    {
        num++;
    }
    for(i=p_i,j=p_j+1;j<15&&board[i][j]==color;j++)
    {
        num++;
    }
    if(num>=5)
        return 1;
    //纵向
    num=1;
    for(i=p_i-1,j=p_j;i>=0&&board[i][j]==color;i--)
    {
        num++;
    }
    for(i=p_i+1,j=p_j;i<15&&board[i][j]==color;i++)
    {
        num++;
    }
    if(num>=5)
        return 1;
    return 0;


}


int one_step_to_win(int color)
{
	int flag=0;
    int i,j;
    int num=0;//走一步就能赢的位置的数量 
    x=y=-1;
    for(j=0;j<15;j++)
    {
        for(i=14;i>=0;i--)
        {
        	if(board[i][j]!=-1)
        	continue;
            if(check_five(i,j,color))
            {
            	//cout<<i<<' '<<j<<endl; 
                num++;
            }
            if(!flag&&num==1)
            {
            	flag=1;
                x=j;
                y=14-i;
               // cout<<x<<' '<<y<<endl;
            }
            if(num==2)
			{
				j=15;
                i=-1;
			} 

        }
    }
    return num;
}
void show();
int two_step_to_win(int color)
{
	int i,j;
	for(j=0;j<15;j++)
	{
		for(i=14;i>=0;i--)
		{
			if(board[i][j]==-1)
			board[i][j]=color;
			else
			continue;
			if((one_step_to_win(color)==2)&&(one_step_to_win(1-color)==0))
			{
				x=j;
				y=14-i;
				return 1;
			}
			
			board[i][j]=-1;
		}
	}
	return 0;
} 


void show()
{
    for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
        {
            if(board[i][j]!=-1)
                cout<<" ";
            cout<<board[i][j];

        }
        cout<<endl;
    }
}
int main()
{
    int flag;
    int first;
    while(input()>0)
    {
        //show();
        flag=judge_valid();
        if(flag==-1)
        {
            cout<<"Invalid."<<endl;
            continue;
        }
        first=flag;
        //一步赢
        if(one_step_to_win(first)>0)
        {
            cout<<"Place "<<((first==0)?"white":"black")<<" at"<<" ("<<x<<","<<y<<") "<<"to win in 1 move."<<endl;
        }
        //两步输
        else if(one_step_to_win(1-first)==2)
		{
				cout<<"Lose in 2 moves."<<endl;
		} 
		//三步赢
		else if(two_step_to_win(first)) 
		{
			cout<<"Place "<<((first==0)?"white":"black")<<" at"<<" ("<<x<<","<<y<<") "<<"to win in 3 moves."<<endl;
		}
		else
		{
			cout<<"Cannot win in 3 moves."<<endl;
		}
		
		
    }
    return 0;
}

/*
8
1 0 0
2 0 0
3 0 0
4 0 0
1 1 1
2 1 1
3 1 1
4 1 1

*/






你可能感兴趣的:(模拟,HDU,3683,Gomoku,胜局判断)