POJ 1970 The Game

 

 

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6886   Accepted: 1763

 

Description

 

A game of Renju is played on a 19*19 board by two players. One player uses black stones and the other uses white stones. The game begins in an empty board and two players alternate in placing black stones and white stones. Black always goes first. There are 19 horizontal lines and 19 vertical lines in the board and the stones are placed on the intersections of the lines. 

Horizontal lines are marked 1, 2, ..., 19 from up to down and vertical lines are marked 1, 2, ..., 19 from left to right. 

The objective of this game is to put five stones of the same color consecutively along a horizontal, vertical, or diagonal line. So, black wins in the above figure. But, a player does not win the game if more than five stones of the same color were put consecutively. 

Given a configuration of the game, write a program to determine whether white has won or black has won or nobody has won yet. There will be no input data where the black and the white both win at the same time. Also there will be no input data where the white or the black wins in more than one place. 

 

Input

 

The first line of the input contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. Each test case consists of 19 lines, each having 19 numbers. A black stone is denoted by 1, a white stone is denoted by 2, and 0 denotes no stone.

 

Output

 

There should be one or two line(s) per test case. In the first line of the test case output, you should print 1 if black wins, 2 if white wins, and 0 if nobody wins yet. If black or white won, print in the second line the horizontal line number and the vertical line number of the left-most stone among the five consecutive stones. (Select the upper-most stone if the five consecutive stones are located vertically.)

 

Sample Input

 

1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 0 0 2 2 2 1 0 0 0 0 0 0 0 0 0 0
0 0 1 2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 2 2 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

 

Sample Output

 

1
3 2

 

Source

 

 

思路:搜索。

#include<iostream>  
using namespace std;  
const int dir_x[]={1,0,1,-1};  
const int dir_y[]={0,1,1,1};  
void search(int x,int y);  
int judge,goal_x,goal_y,board[21][21];  
void search(int x,int y){  
    for(int i=0;i<4;i++){  
        int tmpx=x,tmpy=y,counter=0;  
        int tx=tmpx-dir_x[i],ty=tmpy-dir_y[i];  
        if(tx>=0&&tx<19&&ty>=0&&ty<19&&board[tx][ty]==board[x][y])      continue;  
        while(tmpx>=0&&tmpx<19&&tmpy>=0&&tmpy<19&&board[tmpx][tmpy]==board[x][y]){  
            counter++;  
            tmpx+=dir_x[i];  
            tmpy+=dir_y[i];  
        }  
        if(counter==5){  
            judge=board[x][y];  
            goal_x=x+1; goal_y=y+1;  
            return;  
        }  
    }  
    return;  
}  
int main(){  
    int t;cin>>t;  
    while(t--){  
        for(int i=0;i<19;i++)  
             for(int j=0;j<19;j++)  
                 cin>>board[i][j];  
        judge=0;  
        for(int i=0;i<19;i++){  
            if(judge!=0)    break;  
            for(int j=0;j<19;j++){  
                if(judge!=0)    break;  
                if(board[i][j]==0)    continue;             
                search(i,j);  
            }  
        }  
        cout<<judge<<endl;  
        if(judge==1)cout<<goal_x<<" "<<goal_y<<endl;  
        else if(judge==2)    cout<<goal_x<<" "<<goal_y<<endl;  
    }
}

 

posted @ 2018-04-29 09:09  一蓑烟雨任生平  阅读(338)  评论(0编辑  收藏  举报