[Offer收割]编程练习赛12 register

Ended

Participants:1323

Verdict:Accepted
Score:100 / 100
Submitted:2017-04-02 13:56:53

Lang:Java

Edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();
        char[][] matrix = new char[n][m];
        for (int i = 0i < ni++) {
            sc.nextLine();
            char[] row = new char[m];
            for (int j = 0j < mj++) {
                row[j= sc.findInLine(".").charAt(0);
            }
            matrix[i= row;
        }
        System.out.println(solve(matrix));
    }
    private static int solve(char[][] matrix) {
        int n = matrix.lengthm = matrix[0].lengthn1 = n + 1m1 = m + 1;
        UF uf = new UF(n1 * m + n * m1);
        int base = n1 * m;
        for (int i = 0i < ni++) {
            for (int j = 0j < mj++) {
                if (matrix[i][j== '/') {
                    uf.union(encode(ijm0), encode(ijm1base));
                    uf.union(encode(i + 1jm0), encode(ij + 1m1base));
                }
                if (matrix[i][j== '\\') {
                    uf.union(encode(ijm0), encode(ij + 1m1base));
                    uf.union(encode(i + 1jm0), encode(ijm1base));
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX