namespace myjinxin
{publicclassKata{publicintPolygonPerimeter(bool[][] matrix){int numSides =0;for(int row =0; row < matrix.Length; row++){for(int col =0; col < matrix[row].Length; col++){if(matrix[row][col])// if the square is true, check if matrix edge, or neighbor is false{
numSides +=( col ==0||!matrix[row][col-1])?1:0;// left
numSides +=( col == matrix[row].Length-1||!matrix[row][col+1])?1:0;// right
numSides +=( row ==0||!matrix[row-1][col])?1:0;// top
numSides +=( row == matrix.Length-1||!matrix[row+1][col])?1:0;// bottom}}}return numSides;}}}
答案2:
namespace myjinxin
{using System;publicclassKata{publicboolIsEdge(bool[][] array,int y,int x){try{return(array[y][x]==false)?true:false;//if it is false, it is an edge. Otherwise, it is "true" and not an edge.}catch(IndexOutOfRangeException){returntrue;//if it is out of range, it is an edge}}publicintPolygonPerimeter(bool[][] matrix){var edges =0;for(var y =0; y < matrix.Length; y++)//iterate through each array in array{for(var x =0; x < matrix[y].Length; x++)///iterate through elements in each array{if(matrix[y][x]){if(IsEdge(matrix, y, x +1)) edges++;//checks to see if neighboring positions are edges relative to matrix[y][x]if(IsEdge(matrix, y, x -1)) edges++;if(IsEdge(matrix, y +1, x)) edges++;if(IsEdge(matrix, y -1, x)) edges++;}}}return edges;}}}
答案3:
namespace myjinxin
{using System;using System.Linq;publicclassKata{publicintPolygonPerimeter(bool[][] matrix){int per =0;for(int i =0; i < matrix.Count(); i++){for(int j =0; j < matrix[i].Count(); j++){if(matrix[i][j]==true){
per+=(i >0)?(matrix[i -1][j]?0:2):2;
per+=(j >0)?(matrix[i][j -1]?0:2):2;}}}return per;}}}
namespace myjinxin
{using System;publicclassKata{publicintPolygonPerimeter(bool[][] matrix){int perimeter =0;var rowCount = matrix.Length;var colCount = matrix[0].Length;for(int i =0; i < rowCount; i++){for(int j =0; j < colCount; j++){// -- Black cell --if(matrix[i][j]){// ----- Check LEFT -----if(j ==0){
perimeter++;}// -> Cell to the LEFT is white <-elseif(!matrix[i][j -1]){
perimeter++;}// ----- Check RIGHT -----if(j == colCount -1){
perimeter++;}// -> Cell to the RIGHT is white <-elseif(!matrix[i][j +1]){
perimeter++;}// ----- Check UP -----if(i ==0){
perimeter++;}// -> Cell ABOVE is white <-elseif(!matrix[i -1][j]){
perimeter++;}// ----- Check DOWN -----if(i == rowCount -1){
perimeter++;}// -> Cell BELOW is white <-elseif(!matrix[i +1][j]){
perimeter++;}}}}return perimeter;}}}
答案10:
namespace myjinxin
{using System;publicclassKata{publicintPolygonPerimeter(bool[][] matrix){int polygons =0;for(int x =0; x < matrix.Length; x++){for(int y =0; y < matrix[0].Length; y++){
Console.WriteLine("h");if(matrix[x][y]==true){
Console.WriteLine("l");for(int i =-1; i <=1; i++){for(int n =-1; n <=1; n++){if((i ==0&& n ==0)||(i ==-1&& n ==-1)||(i ==-1&& n ==1)||(i ==1&& n ==-1)||(i ==1&& n ==1))continue;if(x+i <0|| x+i > matrix.Length-1)
polygons++;elseif(y+n <0|| y+n > matrix[0].Length-1)
polygons++;elseif(matrix[x+i][y+n]==false)
polygons++;}}}}}
Console.WriteLine("-----------------");return polygons;}}}