NowCoder最近爱上了五子棋,现在给你一个棋局,请你帮忙判断其中有没有五子连珠(超过五颗也算)。 输入描述: 输入有多组数据,每组数据为一张20x20的棋盘。 其中黑子用“*”表示,白子用“+”表示,空白位置用“.”表示。 输出描述: 如果棋盘上存在五子连珠(无论哪种颜色的棋子),输入“Yes”,否则输出“No”。
- // write your code here
- import java.util.*;
- public class Main{
- final static int N=20;
- public static void main(String[] args){
- Scanner sc =new Scanner(System.in);
- while(sc.hasNext()){
-
- char[][] map = new char[N][N];
- for(int i = 0; i < N; ++i){
- map[i] = sc.next().toCharArray();
- }
- System.out.println(solve(map)? "Yes" : "No");
- }
- }
-
-
- // 遍历map的每一个位置
- public static boolean solve(char[][] map){
- for(int i = 0; i < N; ++i){
- for(int j = 0; j < N; ++j){
- // 如果该位置有符号,则进行搜索
- if('+' == map[i][j] || '*' == map[i][j]){
- if(isWin(map, map[i][j], i, j) >= 5)
- return true;
- }
- }
- }
- return false;
- }
- public static int isWin(char[][] map,char ch,int x,int y){
- int[][][] pos={
- {{-1, 0}, {1, 0,}},
- {{0, -1,}, {0, 1}},
- {{-1, -1}, {1, 1}}
- , {{-1, 1}, {1, -1}}};
- int maxcount=0;
- //八个方向搜索
- for(int i=0;i<4;i++){
- int count=0;
- for(int j=0;j<2;j++){
- int nx = x;
- int ny = y;
-
- while(nx >= 0 && nx < N && ny >= 0 &&
- ny< N && map[nx][ny] ==ch){
- count++;
- nx+=pos[i][j][0];
- ny+=pos[i][j][1];
- }
- }
- maxcount=Math.max(maxcount,count);
- }
- return maxcount-1;
- }
- }
【解题思路】
结构设计:dir代表当前位置的8个方向,其中上下、左右、左上右下、右上左下为必须放在一起
检测。
获取一个棋盘,按照行列检测棋盘中的每个位置,当拿到一个位置后,按照以下步骤进行操作:
1. 以该位置为中心,依次检测该位置的上下、左右、左上右下、右上左下,比如左上
2. 从该位置开始向上检测,找出连在一起的同种棋子个数,再向下检测同种棋子的个数并累
计,注意在检测时,中心位置统计了两次,上下统计完时,需要给结果减去1
3. 按照2统计完上下、左右、左上右下、右上左下各个方向,找出最大的同种棋子个数
4. 检测3中统计出的最大同种棋子个数,如果大于等于5,输出YSE,否则取下一个位置继续1
5. 如果所有的位置检测完,没有超过5个的相同棋子,则输出NO