Created
April 17, 2016 01:17
-
-
Save jianminchen/bc2c399407a30181ca68292459a3c791 to your computer and use it in GitHub Desktop.
study the code
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
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