-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path541.go
60 lines (54 loc) · 1.16 KB
/
541.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// UVa 541 - Error Correction
package main
import (
"fmt"
"os"
)
func cell(matrix [][]byte, i, j int, forRow bool) byte {
if forRow {
return matrix[i][j]
}
return matrix[j][i]
}
func invalid(matrix [][]byte, forRow bool) []int {
var invalids []int
for i := range matrix {
isValid := true
for j := range matrix[i] {
if cell(matrix, i, j, forRow) == 1 {
isValid = !isValid
}
}
if !isValid {
invalids = append(invalids, i)
}
}
return invalids
}
func main() {
in, _ := os.Open("541.in")
defer in.Close()
out, _ := os.Create("541.out")
defer out.Close()
var n int
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
matrix := make([][]byte, n)
for i := range matrix {
matrix[i] = make([]byte, n)
for j := range matrix[i] {
fmt.Fscanf(in, "%d", &matrix[i][j])
}
}
switch invalidRow, invalidColumn := invalid(matrix, true), invalid(matrix, false); {
case len(invalidRow) == 0 && len(invalidColumn) == 0:
fmt.Fprintln(out, "OK")
case len(invalidRow) == 1 && len(invalidColumn) == 1:
fmt.Fprintf(out, "Change bit (%d,%d)\n", invalidRow[0]+1, invalidColumn[0]+1)
default:
fmt.Fprintln(out, "Corrupt")
}
}
}