|
| 1 | +import Foundation |
| 2 | + |
| 3 | +let input = readLine()!.split(separator: " ").map { Int($0)! } |
| 4 | +let R = input[0], C = input[1] |
| 5 | + |
| 6 | +var board = Array(repeating: Array(repeating: Character("."), count: C), count: R) |
| 7 | +var notConnected = Array( |
| 8 | + repeating: Array(repeating: [Bool](repeating: false, count: 4), count: C), |
| 9 | + count: R |
| 10 | +) |
| 11 | + |
| 12 | +let blocks: [Character] = ["|", "-", "+", "1", "2", "3", "4"] |
| 13 | + |
| 14 | +let dr = [-1, 0, 1, 0] |
| 15 | +let dc = [0, -1, 0, 1] |
| 16 | + |
| 17 | +let adjs: [[Bool]] = [ |
| 18 | + [true, false, true, false], // | |
| 19 | + [false, true, false, true], // - |
| 20 | + [true, true, true, true], // + |
| 21 | + [false, false, true, true], // 1 |
| 22 | + [true, false, false, true], // 2 |
| 23 | + [true, true, false, false], // 3 |
| 24 | + [false, true, true, false] // 4 |
| 25 | +] |
| 26 | + |
| 27 | +func getAdjacent(_ r: Int, _ c: Int) -> [Bool] { |
| 28 | + switch board[r][c] { |
| 29 | + case "|": return adjs[0] |
| 30 | + case "-": return adjs[1] |
| 31 | + case "+": return adjs[2] |
| 32 | + case "1": return adjs[3] |
| 33 | + case "2": return adjs[4] |
| 34 | + case "3": return adjs[5] |
| 35 | + case "4": return adjs[6] |
| 36 | + default: return [] |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func checkConnected(_ r: Int, _ c: Int) { |
| 41 | + let adj = getAdjacent(r, c) |
| 42 | + |
| 43 | + for i in 0..<4 { |
| 44 | + if !adj[i] { continue } |
| 45 | + |
| 46 | + let nr = r + dr[i] |
| 47 | + let nc = c + dc[i] |
| 48 | + |
| 49 | + if nr < 0 || nr >= R || nc < 0 || nc >= C { continue } |
| 50 | + |
| 51 | + if board[nr][nc] == "." { |
| 52 | + notConnected[nr][nc][(i + 2) % 4] = true |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func calcBlock(_ r: Int, _ c: Int) -> Character { |
| 58 | + for i in 0..<7 { |
| 59 | + if notConnected[r][c] == adjs[i] { |
| 60 | + return blocks[i] |
| 61 | + } |
| 62 | + } |
| 63 | + return "?" |
| 64 | +} |
| 65 | + |
| 66 | +for i in 0..<R { |
| 67 | + let line = Array(readLine()!) |
| 68 | + board[i] = line |
| 69 | +} |
| 70 | + |
| 71 | +for i in 0..<R { |
| 72 | + for j in 0..<C { |
| 73 | + let ch = board[i][j] |
| 74 | + if ch == "." || ch == "M" || ch == "Z" { continue } |
| 75 | + checkConnected(i, j) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +for i in 0..<R { |
| 80 | + for j in 0..<C { |
| 81 | + for k in 0..<4 { |
| 82 | + if notConnected[i][j][k] { |
| 83 | + let block = calcBlock(i, j) |
| 84 | + print(i + 1, j + 1, block) |
| 85 | + exit(0) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments