Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created April 17, 2016 01:17
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/bc2c399407a30181ca68292459a3c791 to your computer and use it in GitHub Desktop.
Save jianminchen/bc2c399407a30181ca68292459a3c791 to your computer and use it in GitHub Desktop.
study the code
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 row = Convert.ToInt32(Console.ReadLine());
int col = Convert.ToInt32(Console.ReadLine());
int[,] arr = new int[row, col];
bool[,] marked = new bool[row, col];
for (int i = 0, j = 0; i < row; i++)
{
j = 0;
foreach (string str in Console.ReadLine().Split(' '))
{
arr[i, j] = Convert.ToInt32(str);
j++;
}
}
int count;
int result=0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (marked[i, j] == false && arr[i, j] == 1)
{
count = Calculate(arr, marked, i, j);
if (count > result)
{
result = count;
}
}
else
{
marked[i, j] = true;
}
}
}
Console.WriteLine(result);
Console.ReadLine();
}
static int Calculate(int[,] arr, bool[,] marked, int row, int col)
{
if (row < 0 || col < 0 || row >= arr.GetLength(0) || col >= arr.GetLength(1))
{
return 0;
}
if (marked[row, col] == false && arr[row, col] == 1)
{
marked[row, col] = true;
return 1
+ Calculate(arr, marked, row - 1, col)
+ Calculate(arr, marked, row - 1, col - 1)
+ Calculate(arr, marked, row - 1, col + 1)
+ Calculate(arr, marked, row, col - 1)
+ Calculate(arr, marked, row, col + 1)
+ Calculate(arr, marked, row + 1, col - 1)
+ Calculate(arr, marked, row + 1, col)
+ Calculate(arr, marked, row + 1, col + 1);
}
else
{
marked[row, col] = true;
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment