最近爱上了五子棋,现在给你一个棋局,请你帮忙判断其中有没有五子连珠(超过五颗也算)。
admin
2024-03-23 23:44:35

NowCoder最近爱上了五子棋,现在给你一个棋局,请你帮忙判断其中有没有五子连珠(超过五颗也算)。

解析:

import java.util.*;
public class Main {static int[][] direction = {{0, 1}, {0, - 1}, {1, 0}, { - 1, 0}, {1, 1}, {1, - 1}, { - 1, 1}, { - 1, - 1}};public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (sc.hasNext()) {Character[][] map = new Character[20][20];for (int i = 0; i < 20; i ++ ) {String s = sc.next();for (int j = 0; j < 20; j ++ ) {map[i][j] = s.charAt(j);}}if(check(map)) System.out.println("Yes");else System.out.println("No");}}public static boolean check(Character[][] map) {for (int i = 0; i < 20; i ++ ) {for (int j = 0; j < 20; j ++ ) {if(map[i][j] == '*' || map[i][j] == '+') {for (int k = 0; k < 8; k ++ ) {int count = 1;int x = i + direction[k][0];int y = j + direction[k][1];while (x >= 0 && x < 20 && y >= 0 && y < 20 && map[x][y] == map[i][j]) {count ++ ;x += direction[k][0];y += direction[k][1];}if(count == 5) return true;}}}}return false;}
}

相关内容

热门资讯

今晚必须把夜宵安排上!前几天点... 今晚必须把夜宵安排上!前几天点的那家烧烤就很不错,烤鸡翅外焦里嫩,咬一口滋滋冒油,撒上的辣椒面和孜然...
原创 一... 忙忙碌碌的三餐里,总有一道汤品简单却格外暖心,那就是家常紫菜蛋花汤。它没有繁复的食材,没有考究的工序...
产区协同·国资引领·多元融合—... 第114届全国糖酒商品交易会于2026年3月26日至28日在成都世纪城新国际会展中心和中国西部国际博...
奶茶开路!这一季春茶,在海外卖... (文/刘媛媛 编辑/周远方) 每年3月至清明前,是中国春茶集中上市的黄金期。从四川宜宾的早春茶,到...
渭南蒲城椽头蒸馍!陕西非遗面食... 在八百里秦川的腹地,渭南市蒲城县孕育出一种被誉为“面食活化石”的珍馐——蒲城椽头蒸馍。它并非寻常巷陌...