Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created April 17, 2016 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/737138aeb946961aded556181c06502e to your computer and use it in GitHub Desktop.
Save jianminchen/737138aeb946961aded556181c06502e to your computer and use it in GitHub Desktop.
declare offset array for x and y, Julia likes the idea.
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int m = Convert.ToInt32(Console.ReadLine()); // row
int n = Convert.ToInt32(Console.ReadLine()); // col
string[,] grid = new string[m,n];
for (int i = 0; i < m; i++) {
string row = Console.ReadLine();
string[] r = row.Split(' ');
for (int j = 0; j < n; j++) {
grid[i,j] = r[j];
//Console.Write(grid[i,j] + " ");
}
//Console.WriteLine();
}
int max = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int res = CountRegion(i, j, grid);
max = Math.Max(res,max);
}
}
Console.WriteLine(max);
}
public static int CountRegion(int row, int col, string[,] grid) {
int count = 0;
int[] rowOffset = new int[] { -1, -1, -1, 0, 1, 1, 1, 0 };
int[] colOffset = new int[] { -1, 0, 1, 1, 1, 0, -1, -1 };
if (row >= 0 && col >= 0 && row < grid.GetLength(0) && col < grid.GetLength(1)) {
if (grid[row,col] == "1") {
count++;
grid[row,col] = "X";
for (int i = 0; i < rowOffset.Length; i++) {
count += CountRegion(row + rowOffset[i], col + colOffset[i], grid);
}
}
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment