可以组成网络的服务器

可以组成网络的服务器 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
在一个机房中,服务器的位置标识在 n*m 的整数矩阵网格中,1 表示单元格上有服务器,0 表示没有。如果两台服务器位于同一行或者同一列中紧邻的位置,则认为它们之间可以组成一个局域网。
请你统计机房中最大的局域网包含的服务器个数。

import sys

def change(res,x,y,n,m):
    for i in range(n):
        for j in range(m):
            if res[i][j] == y:
                res[i][j] = x

if __name__ == '__main__':
    arr = []
    n,m = input().split()
    n = int(n)
    m = int(m)
    for i in range(n):
        arr.append(sys.stdin.readline())
    mat = []
    result = []
    for i in range(n):
        mat.append(arr[i].split())
        result.append([0 for j in range(m)])
    index = 1
    for i in range(n):
        for j in range(m):
            if mat[i][j] != "0":
                if i > 0 and mat[i][j] != "0" and mat[i - 1][j] != "0":
                    result[i][j] = result[i - 1][j]
                    continue
                if j > 0 and mat[i][j] != "0" and mat[i][j - 1] != "0":
                    result[i][j] = result[i][j - 1]
                    continue
                if not result[i][j]:
                    result[i][j] = index
                    index += 1
                 
    while True:
        cnt = 0
        for i in range(n):
            for j in range(m):
                if i > 0 and result[i][j] and result[i - 1][j] and result[i][j] != result[i - 1][j]:
                    change(result,result[i][j],result[i - 1][j],n,m)
                    cnt += 1
                if j > 0 and result[i][j] and result[i][j - 1] and result[i][j] != result[i][j - 1]:
                    change(result,result[i][j],result[i][j - 1],n,m)
                    cnt += 1
        if cnt == 0:
            break
    calc = [0 for i in range(index)]
    for i in range(n):
        for j in range(m):
            if result[i][j]:
                calc[result[i][j]] += 1
    print(max(calc))
def get_num(node, network):
    x, y = node
    if node in network:
        network.remove(node)
        group[-1].append(node)
        for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
            new_x, new_y = x + dx, y + dy
            if (new_x, new_y) in network:
                get_num((new_x, new_y), network)


while True:
    try:
        n, m = list(map(int, input().split()))
        server = [list(map(int, input().split())) for _ in range(n)]
        network = list()
        for i, x in enumerate(server):
            network.extend([(i, j) for j, y in enumerate(x) if y == 1])
        group = []
        for node in network:
            group.append([])
            get_num(node, network)
        print(max([len(i) for i in group]))
    except:
        break
//190fen

import java.util.Scanner;


public class Main {

    static int[][] tmp;
    static int t = 0 , n , m;

    public static void process(int a, int b){
        if (a < 0 || a >= n || b < 0 || b >= m || tmp[a][b] != 1){
            return;
        }
        tmp[a][b] = 0;
        t++;
        process(a - 1, b);
        process(a + 1, b);
        process(a , b + 1);
        process(a , b -1);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        tmp = new int[n][m];
        int res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                tmp[i][j] = sc.nextInt();
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                process(i,j);
                res = Math.max(res,t);
                t = 0;
            }
        }
        System.out.println(res);
    }
}
//manfen



全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务