菜鸟也做有道难题①

重在参与 嘿嘿。。。

思路和我的扫雷程序一样, 找到周围的所有的萝卜总数是否在AB之间,这里我没有考虑AB的大小关系。

每块地由格子(Cell)组成,每个格子(Cell)有三个属性:地标(X,Y)和萝卜数(Count) 。

逻辑很简单,遍历地(Field)里所有的格子(List<Cell>),找到当前格子(CurrentCell)周围的萝卜数是否在AB之间。 

5

9

3

0

0

26

spend 5428 milliseconds


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string[] test0 = { "111""111""111" };
            
string[] test1 = { "111""141""111" };
            
string[] test2 = { "2309""0239""2314" };
            
string[] test3 = { "924""231""390""910""121" };
            
string[] test4 = { "5" };
            
string[] test5 = { "1234567890""1234567890""1234567890""1234567890""1234567890""1234567890""1234567890""1234567890""1234567890""1234567890""1234567890" };
            Console.WriteLine(countSpecialNumbers(test0, 
48));
            Console.WriteLine(countSpecialNumbers(test1, 
48));
            Console.WriteLine(countSpecialNumbers(test2, 
57));
            Console.WriteLine(countSpecialNumbers(test3, 
3136));
            Console.WriteLine(countSpecialNumbers(test4, 
38));
            Console.WriteLine(countSpecialNumbers(test5, 
318));

            System.Diagnostics.Stopwatch watch 
= new System.Diagnostics.Stopwatch();
            watch.Start();
            
for (int i = 0; i < 10000; i++)
            {
                countSpecialNumbers(test0, 
48);
                countSpecialNumbers(test1, 
48);
                countSpecialNumbers(test2, 
57);
                countSpecialNumbers(test3, 
3136);
                countSpecialNumbers(test4, 
38);
                countSpecialNumbers(test5, 
318);
            }
            watch.Stop();

            Console.WriteLine(
"spend {0} milliseconds", watch.ElapsedMilliseconds);
            Console.Read();
        }

        
public static int countSpecialNumbers(string[] field, int A, int B)
        {
            List
<Cell> list = GetList(field);
            
int Cellcount = 0;
            
foreach (Cell cell in list)
            {
                
int Sumcount = 0;
                
foreach (Cell currentCell in list)
                {
                    
if (Math.Abs(currentCell.X - cell.X) < 2 &&
                        Math.Abs(currentCell.Y 
- cell.Y) < 2 &&
                        
!(currentCell.X == cell.X && currentCell.Y == cell.Y))
                    {
                        Sumcount 
+= currentCell.Count;
                    }
                }
                
if (Sumcount >= A && Sumcount <= B)
                    Cellcount
++;
            }
            
return Cellcount;
        }
        
//把数组转换成List<Field>
        public static List<Cell> GetList(string[] field)
        {
            List
<Cell> list = new List<Cell>();
            
for (int y = 0; y < field.Length; y++)
            {
                Char[] row 
= field[y].ToCharArray();
                
for (int x = 0; x < row.Length; x++)
                {
                    list.Add(
new Cell() { X = x, Y = y, Count = int.Parse(row[x].ToString()) });
                }
            }
            
return list;
        }
    }


    
public class Cell//一个单元格子地
    {
        
public int X { getset; }//地的坐标x
        public int Y { getset; }//y
        public int Count { getset; }//地的胡萝卜数
    }
}

原文地址:https://www.cnblogs.com/mad/p/1495937.html