Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created April 17, 2016 01:02
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/d6e2994723452aef951a860e8de2def8 to your computer and use it in GitHub Desktop.
Save jianminchen/d6e2994723452aef951a860e8de2def8 to your computer and use it in GitHub Desktop.
design the function to return the value - it is also a good idea.
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
int rows = Convert.ToInt32(Console.ReadLine());
int columns = Convert.ToInt32(Console.ReadLine());
string[][] matrix = new string[rows][];
for(int i=0;i<rows;i++)
matrix[i] = Console.ReadLine().Split(' ');
int maxScore = 0;
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
int score = findArea(matrix, i, j, rows, columns);
if(score>maxScore)
maxScore = score;
}
}
Console.WriteLine(maxScore);
}
static int findArea(string[][] matrix, int x, int y, int rows, int columns){
int result = 0;
if(x>=0 && x<rows && y>=0 && y<columns && matrix[x][y]=="1"){
result++;
matrix[x][y]="X";
result += findArea(matrix, x + 1, y, rows, columns);
result += findArea(matrix, x + 1, y-1, rows, columns);
result += findArea(matrix, x + 1, y+1, rows, columns);
result += findArea(matrix, x - 1, y, rows, columns);
result += findArea(matrix, x - 1, y-1, rows, columns);
result += findArea(matrix, x - 1, y+1, rows, columns);
result += findArea(matrix, x, y-1, rows, columns);
result += findArea(matrix, x, y+1, rows, columns);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment