The Game

题目名称:The Game

题目链接: The Game

描述

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.

输入

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.

输出

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.)

样例输入

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

样例输出

1
3 2

题目大意

根据五子棋的规则判断黑棋和白旗谁胜

解题思路

遍历棋盘的每个方格,分别判断横,竖,斜八个方向是否存在连续五个均为统一色的棋子,在这里判断右方,下方,右上方和右下方即可,相同方向上存在连续5个颜色的棋子(不能超过5个)即为胜出,不存在就输出0,

代码如下

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 25
int table[25][25];     //将边界之外的部分设置为0,便于忽略边界处情况
int t;
int dx[4]={1,0,1,-1},dy[4]={1,1,0,1};

void solve()
{
	for(int row=1;row<=19;row++){    
		for(int col=1;col<=19;col++){   // 遍历每个格子
			if(table[row][col]!=0){
				for(int i=0;i<4;i++){     //对每个方向上的情况进行判断
					for(int j=1;j<5;j++){
					//	cout<<row+dx[i]*j<<' '<<dx[i]*j<<' ';
						if(table[row+dx[i]*j][col+dy[i]*j]!=table[row][col]) break;
						if(j==4){
							if(table[row-dx[i]][col-dy[i]]!=table[row][col]&&table[row+dx[i]*5][col+dy[i]*5]!=table[row][col]){
								printf("%d\n%d %d\n",table[row][col],row,col);
								return ;
							}
						} 
					}
				//	cout<<endl;
				}
			}
		}
	}
	cout<<"0"<<endl;
}

int main()
{
//	freopen("in.txt", "r", stdin);
	memset(table,0,sizeof(table));
	int t;
	cin>>t;
	while(t--){
		for(int i=1;i<=19;i++){
			for(int j=1;j<=19;j++){
				cin>>table[i][j];
			}
		}
		solve();
	}
	return 0;
}

写在最后

最近在学习 spark和机器学习,把python语法和c++串了,一个小bug没找出来,改了一下午,心塞

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值