(POJ1970)DFS-The Game

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

题目分析:

为什么要用DFS?

这道题的题目大意是这样的,判断是否五个子同颜色且相连,这五个子可以是水平或竖直或对角线相连,将相连的子的颜色和左上方的坐标输出(五子棋的胜负规则,判哪五个子相连)。五子相连,DFS判连通块。

怎么用DFS求解?

从当前棋子处向八个方向DFS(其实是四个,因为是从左上角开始遍历图的,当需要从某点处进行搜索时,其左上角的已经搜过了,所以只用向下,右,右上,右下搜,简化了搜索过程),搜索过程中,如果当前出界,或当前棋子颜色发生改变,就返回,将搜索过程中记录下的同颜色的棋子个数和5比较,若相等,输出棋子颜色和五个棋子中左上角的棋子坐标结束;否则继续遍历搜索。如果没有就输出0。

#include<iostream>
using namespace std;
struct Point {
	int x;
	int y;
};
int mapp[20][20];
Point dir[4] = { {-1,1}, {1,0}, {1,1}, {0,1}};
int visit[20][20][4];
void DFS(int r,int c,int p,int &cnt,int type) {		//当棋子是某一type时,沿着p方向进行搜索
	if (r < 1 || c < 1 || r>19 || c>19||mapp[r][c]!=type)return;
	cnt++;
	visit[r][c][p] = 1;
	DFS(r+dir[p].x, c+dir[p].y, p, cnt,type);
}
int main() {
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		memset(visit, 0, sizeof(visit));
		//数据输入
		for (int j = 1; j <= 19; j++)
		{
			for (int k = 1; k <= 19; k++) {
				cin >> mapp[j][k];
			}
		}
		int flag = 0;		//标记是否找到
		for (int k = 1; k <= 19; k++) {
			for (int j = 1; j <= 19; j++) {
				if (mapp[j][k] != 0) {
					for (int p = 0; p < 4; p++) {
						if (!visit[j][k][p]) {		//棋子沿当前p方向没有搜过
							int sum = 0;
							DFS(j, k, p, sum, mapp[j][k]);
							if (sum == 5) {
								cout << mapp[j][k] << endl << j << " " << k << endl;
								flag = 1;
								break;
							}
						}
					}
				}
				if (flag)break;
			}
			if (flag)break;
		}
		if(flag==0)
		cout << 0 << endl;
	}
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值