Last active
August 29, 2015 14:22
-
-
Save draqoon/39a9be7e7b27b30647c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//島探し (paizaランク S 相当) | |
//https://paiza.jp/learning/search-island | |
using System; | |
using System.Collections.Generic; | |
public class Program { | |
public static void Main() { | |
var s = Console.ReadLine().Split( ' ' ); | |
var mx = int.Parse( s[0] ); //列数 | |
var my = int.Parse( s[1] ); //行数 | |
var map = new bool[mx, my]; | |
for( var y = 0; y < my; y++ ) { | |
var line = Console.ReadLine(); | |
if( string.IsNullOrWhiteSpace( line ) ) | |
break; | |
s = line.Split( ' ' ); | |
for( var x = 0; x < mx && x <= s.Length; x++ ) { | |
map[x, y] = "1".Equals( s[x] ); | |
} | |
} | |
var islands = 0; | |
for( var y = 0; y < my; y++ ) { | |
for( var x = 0; x < mx; x++ ) { | |
if( map[x, y] ) { | |
islands++; | |
checkRounds( map, x, y ); | |
} | |
} | |
} | |
Console.WriteLine( islands ); | |
} | |
private struct Point { | |
public Point( int x, int y ) { | |
this.X = x; | |
this.Y = y; | |
} | |
public readonly int X; | |
public readonly int Y; | |
} | |
private static void checkRounds( bool[,] map, int x, int y ) { | |
var queue = new Queue<Point>(); | |
var mx = map.GetLength( 0 ); | |
var my = map.GetLength( 1 ); | |
queue.Enqueue( new Point( x, y ) ); | |
map[x, y] = false; | |
while( 0 < queue.Count ) { | |
var p = queue.Dequeue(); | |
//left | |
if( 0 < p.X && map[p.X - 1, p.Y] ) { | |
queue.Enqueue( new Point( p.X - 1, p.Y ) ); | |
map[p.X - 1, p.Y] = false; | |
} | |
//right | |
if( p.X < mx - 1 && map[p.X + 1, p.Y] ) { | |
queue.Enqueue( new Point( p.X + 1, p.Y ) ); | |
map[p.X + 1, p.Y] = false; | |
} | |
//up | |
if( 0 < p.Y && map[p.X, p.Y - 1] ) { | |
queue.Enqueue( new Point( p.X, p.Y - 1 ) ); | |
map[p.X, p.Y - 1] = false; | |
} | |
//down | |
if( p.Y < my - 1 && map[p.X, p.Y + 1] ) { | |
queue.Enqueue( new Point( p.X, p.Y + 1 ) ); | |
map[p.X, p.Y + 1] = false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment