华为OD机试真题-围棋的气-2023年OD统一考试(C卷)

发布时间:2024年01月10日

题目描述:

围棋棋盘由纵横各19条线垂直相交组成,棋盘上一共19x19=361个交点,对弈双方一方执白棋,一方执黑棋,落子时只能将棋子置于交点上。

“气”是围棋中很重要的一个概念,某个棋子有几口气,是指其上下左右方向四个相邻的交叉点中,有几个交叉点没有棋子,由此可知:

1、在棋盘的边缘上的棋子最多有3口气(黑1),在棋盘角点的棋子最多有2口气(黑2),其它情况最多有4口气(白1)

2、所有同色棋子的气之和叫作该色棋子的气,需要注意的是,同色棋子重合的气点,对于该颜色棋子来说,只能计算一次气,比如下图中,黑棋一共4口气,而不是5口气,因为黑1和黑2中间红色三角标出的气是两个黑棋共有的,对于黑棋整体来说只能算一个气。

3、本题目只计算气,对于眼也按气计算,如果您不清楚“眼”的概念,可忽略,按照前面描述的规则计算即可。

现在,请根据输入的黑棋和白棋的坐标位置,计算黑棋和白起一共各有多少气?

输入描述:

输入包括两行数据,如:

0 5 8 9 9 10

5 0 9 9 9 8

1、每行数据以空格分隔,数据个数是2的整数倍,每两个数是一组,代表棋子在棋盘上的坐标;

2、坐标的原点在棋盘左上角点,第一个值是行号,范围从0到18;第二个值是列号,范围从0到18。

3、举例说明:第一行数据表示三个坐标(0,5)、(8,9)、(9,10)

4、第一行表示黑棋的坐标,第二行表示白棋的坐标。

5、题目保证输入两行数据,无空行且每行按前文要求是偶数个,每个坐标不会超出棋盘范围。

输出描述:

8 7

两个数字以空格分隔,第一个数代表黑棋的气数,第二个数代表白棋的气数。

补充说明:

?收起

示例1

输入:

0 5 8 9 9 10
5 0 9 9 9 8
输出:

8 7
说明:

如果将输入数据放到棋盘上,数数黑棋一共8口气:

数数白棋一共7口气:


解题思路:华为OD题目总是喜欢将简单的题目复杂化,就像阅读理解一样,这个题目其实很简单,考察数据和矩阵,运用循环+统计就可以快速解答。

import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
 
public class Main {
 
    static int maxSide = 18;
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] locBlacks = in.nextLine().split(" ");
        String[] locWhites = in.nextLine().split(" ");
        String[] blacks = transform(locBlacks);
        String[] whites = transform(locWhites);
        System.out.println(counting(blacks, whites) + " " + counting(whites, blacks));
    }
 
 
    static int counting(String[] alias, String[] ememy) {
        Set<String> count = new HashSet<>();
        for (String a : alias) {
            count.add(a);
            String[] loc = a.split("_");
            int x = Integer.parseInt(loc[0]);
            int y = Integer.parseInt(loc[1]);
            if (x > 0) {
                count.add(Integer.toString(x - 1) + "_" + loc[1]);
            }
            if (x < maxSide) {
                count.add(Integer.toString(x + 1) + "_" + loc[1]);
            }
            if (y > 0) {
                count.add(loc[0] + "_" + Integer.toString(y - 1));
            }
            if (y < maxSide) {
                count.add(loc[0] + "_" + Integer.toString(y + 1));
            }
        }
        int res = count.size() - alias.length;
        for (String e : ememy) {
            if (count.contains(e)) {
                res--;
            }
        }
        return res;
    }
 
    static String[] transform(String[] locs) {
        String[] chess = new String[locs.length / 2];
        for (int i = 0; i < locs.length;) {
            chess[i / 2] = locs[i] + "_" + locs[i + 1];
            i += 2;
        }
        return chess;
    }
}
while True:
    try:
        black=list(map(int,input().split()))
        white=list(map(int,input().split()))
        black1=[]
        white1=[]
        for i in range(0,len(black),2):
            co=[black[i],black[i+1]]
            black1.append(co)
        for i in range(0,len(white),2):
            co=[white[i],white[i+1]]
            white1.append(co)
        plate=[[0 for j in range(19)] for i in range(19)]
        for chess in black1:
            plate[chess[0]][chess[1]]=1
        for chess in white1:
            plate[chess[0]][chess[1]]=2
        def qi(x,plate):
            cnt=0
            for i in range(19):
                for j in range(19):
                    if plate[i][j]==0:
                        if i+1<19 and plate[i+1][j]==x:
                            cnt+=1
                        elif i-1>=0 and plate[i-1][j]==x:
                            cnt+=1
                        elif j+1<19 and plate[i][j+1]==x:
                            cnt+=1
                        elif j-1>=0 and plate[i][j-1]==x:
                            cnt+=1
                    else:
                        continue
            return cnt
        black_cnt=qi(1,plate)
        white_cnt=qi(2,plate)
        print('{} {}'.format(black_cnt,white_cnt))
    except:
        break


文章来源:https://blog.csdn.net/2301_76848549/article/details/135495859
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。