首页 > 试题广场 >

五子棋

[编程题]五子棋
  • 热度指数:1920 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
NowCoder最近爱上了五子棋,现在给你一个棋局,请你帮忙判断其中有没有五子连珠(超过五颗也算)。

输入描述:
输入有多组数据,每组数据为一张20x20的棋盘。

其中黑子用“*”表示,白子用“+”表示,空白位置用“.”表示。


输出描述:
如果棋盘上存在五子连珠(无论哪种颜色的棋子),输入“Yes”,否则输出“No”。
示例1

输入

....................
....................
....................
....................
......*.............
.......*............
........*...........
....++++.*..........
..........*.........
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
.......*............
......+*+++.........
.......*............
.......*............
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

输出

Yes
No
import java.util.*;
public class Main{
    public static int[][] direc = {{0,1},{0,-1},{-1,0},{1,0},{-1,-1},{-1,1},{1,1},{1,-1}};
    public static boolean solve(char[][] 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 + direc[k][0];
                        int y = j + direc[k][1]; 
                        while(x >= 0 && x < 20 && y >= 0 && y <20 && map[i][j] == map[x][y]){
                            count++;
                             x = x + direc[k][0];
                             y = y + direc[k][1]; 
                        }
                        if(count == 5){
                            return true;
                        }
                    }
                    
                }
            }
        }
        return false;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            char[][] map = new char[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(solve(map)){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }
    }
}

.
编辑于 2022-06-05 21:29:33 回复(0)
// 还就内个暴力解法
// 遍历棋盘,判断棋子的四个方向(右、右下、下、左下)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String[] checkerboard = new String[20];
            for(int i = 0; i < 20; i++){
                checkerboard[i] = sc.next();
            }
            boolean result = false;
            for(int i = 0; i < 20; i++){
                for(int j = 0; j < 20; j++){
                    if(checkerboard[i].charAt(j) != '.'){
                        if(j < 16 && checkerboard[i].charAt(j) == checkerboard[i].charAt(j+1)
                          && checkerboard[i].charAt(j) == checkerboard[i].charAt(j+2)
                          && checkerboard[i].charAt(j) == checkerboard[i].charAt(j+3)
                          && checkerboard[i].charAt(j) == checkerboard[i].charAt(j+4)){
                            result = true;
                        }
                        if(i < 16 && j < 16 && checkerboard[i].charAt(j) == checkerboard[i+1].charAt(j+1)
                          && checkerboard[i].charAt(j) == checkerboard[i+2].charAt(j+2)
                          && checkerboard[i].charAt(j) == checkerboard[i+3].charAt(j+3)
                          && checkerboard[i].charAt(j) == checkerboard[i+4].charAt(j+4)){
                            result = true;
                        }
                        if(i < 16 && checkerboard[i].charAt(j) == checkerboard[i+1].charAt(j)
                          && checkerboard[i].charAt(j) == checkerboard[i+2].charAt(j)
                          && checkerboard[i].charAt(j) == checkerboard[i+3].charAt(j)
                          && checkerboard[i].charAt(j) == checkerboard[i+4].charAt(j)){
                            result = true;
                        }
                        if(i < 16 && j > 3 && checkerboard[i].charAt(j) == checkerboard[i+1].charAt(j-1)
                          && checkerboard[i].charAt(j) == checkerboard[i+2].charAt(j-2)
                          && checkerboard[i].charAt(j) == checkerboard[i+3].charAt(j-3)
                          && checkerboard[i].charAt(j) == checkerboard[i+4].charAt(j-4)){
                            result = true;
                        }
                    }
                }
            }
            if(result){
                System.out.println("Yes");
            }
            else{
                System.out.println("No");
            }
        }
    }
}

发表于 2021-07-30 17:53:30 回复(0)
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;
	}
}

发表于 2016-10-14 01:37:48 回复(3)