def remove_island(i, j, map) map[i][j] = '0' [[0, 1], [1, 0], [0, -1], [-1, 0]].each do |x, y| next if !(0..11).include?(i + x) || !(0..11).include?(j + y) if map[i + x][j + y]=='1' remove_island(i + x, j + y, map) end end end
loop do map = 12.times.map{ gets.chomp } count = 0 12.times do |i| 12.times do |j| if map[i][j] == '1' count += 1 remove_island(i, j, map) end end end puts count break if gets.nil? end
def remove_island(x, y, map): map[x][y] = 0 move = [[1, 0], [0, 1], [-1, 0], [0, -1]] for i, j in move: if 0 <= 0 x + i <="11" and j map[x i][y j]="=" '1':< span> map = remove_island(x + i, y + j, map) return map[:]
while True: map = get_map() if len(map) != 12: break
count = 0 for x in range(12): for y in range(12): if map[x][y] == '1': count += 1 map = remove_island(x, y, map) print count =>
🍣 c++コード
#include using namespace std;
int m[12][12];
bool valid(int y, int x) { if(0 <= y && < 12) {< span> if(0 <= x && < 12) {< span> if(m[y][x] == 1) return true; } } return false; }
void remove_island(int y, int x) { int nx, ny; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1};
m[y][x] = 0; for(int i = 0; i < 4; i++){ ny = y + dy[i]; nx = x + dx[i]; if(valid(ny, nx)) remove_island(ny, nx); } }
int main() { char c[12]; int ans; while(1){ for(int i = 0; i < 12; i++) { if(!(cin >> c)) return 0; for(int j = 0; j < 12; j++) { m[i][j] = (int)(c[j] - '0'); } }
ans = 0; for(int i = 0; i < 12; i++) { for(int j = 0; j < 12; j++) { if(valid(i, j)) { remove_island(i, j); ans++; } } } cout << ans << endl; } return 0; } =>=>