POJ1970 ZOJ2495 The Game【模拟+回溯】

The Game

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6979 Accepted: 1791

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

Tehran Sharif 2004 Preliminary

 

问题链接POJ1970 ZOJ2495 The Game

问题描述:(略)

问题分析

  五子棋局面判断问题。如果一方在4个方向有连续的5个子则赢,但是超过5个子则不算赢。平局(没有赢方)则输出0。

  棋盘外加一圈,可以省去下标越界判定,可以简单很多。

  没有使用递归,只使用了回溯,程序逻辑和代码要简单许多。

程序说明:(略)

参考链接:(略)

题记:(略)

 

AC的C++语言程序如下:

/* POJ1970 ZOJ2495 The Game */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

// 方向分别是:右,下,右下, 右上
int drow[] = {0, 1, 1, -1};
int dcol[] = {1, 0, 1, 1};
const int D = sizeof(drow) / sizeof(int);

const int N = 19;
const int M = 5;
int board[N + 2][N + 2];

int win, r, c;

bool find(int row, int col)
{
    int a = board[row][col];
    for(int k = 0; k < D; k++) {
        int nextrow = row, nextcol = col, l = 0;
        while(board[nextrow][nextcol] == a) {
            if(++l == M) {
                // 连续5个子,需要看一下这5个子的后一个和前一个,如果相同则不赢
                nextrow += drow[k];
                nextcol += dcol[k];
                if(board[nextrow][nextcol] == a)
                    l++;
                if(board[row - drow[k]][col - dcol[k]] == a)
                    l++;
                if(l == M) {
                    win = a, r = row, c = col;
                    return true;
                } else
                    break;
            } else {
                nextrow += drow[k];
                nextcol += dcol[k];
            }
        }
    }
    return false;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--) {
        memset(board, 0, sizeof(board));

        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= N; j++)
                scanf("%d", &board[i][j]);

        win = 0;
        bool flag = true;
        for(int i = 1; flag && i <= N; i++)
            for(int j = 1; flag && j <= N; j++)
                if(board[i][j])
                    flag = !find(i, j);

        // 输出结果
        printf("%d\n", win);
        if(win)
            printf("%d %d\n", r, c);
    }

    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值