Submission #3996066


Source Code Expand

Copy
H, W = gets.split.map(&:to_i)
S = H.times.map{gets.chomp}

visited = Array.new(H){Array.new(W)}
ans = 0
H.times do |i|
    W.times do |j|
        next if visited[i][j]
        visited[i][j] = true
        num = [0, 0]
        que = [[i, j]]
        while que.size > 0
            x, y = que.shift
            num[S[x][y] == '#' ? 1 : 0] += 1

            [[-1, 0], [1, 0], [0, -1], [0, 1]].each do |a, b|
                x2 = x+a
                y2 = y+b
                if x2.between?(0, H-1) && y2.between?(0, W-1) && !visited[x2][y2] && S[x][y] != S[x2][y2]
                    que.push([x2, y2])
                    visited[x2][y2] = true
                end
            end
        end
        ans += num[0] * num[1]
    end
end
puts ans

Submission Info

Submission Time
Task C - Alternating Path
User betrue12
Language Ruby (2.3.3)
Score 300
Code Size 769 Byte
Status
Exec Time 557 ms
Memory 3708 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample-01.txt, sample-02.txt, sample-03.txt
All 300 / 300 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01.txt 227 ms 2428 KB
02.txt 557 ms 3708 KB
03.txt 31 ms 1916 KB
04.txt 483 ms 3452 KB
05.txt 492 ms 3708 KB
06.txt 508 ms 3708 KB
07.txt 448 ms 3452 KB
08.txt 467 ms 3452 KB
09.txt 540 ms 3708 KB
10.txt 7 ms 1788 KB
11.txt 9 ms 1788 KB
12.txt 10 ms 1788 KB
sample-01.txt 7 ms 1788 KB
sample-02.txt 7 ms 1788 KB
sample-03.txt 7 ms 1788 KB