Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace ConsoleApplication9
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             StreamReader read = new StreamReader("input.txt");
  15.             StreamWriter write = new StreamWriter("write.txt");
  16.             Console.SetIn(read);
  17.             Console.SetOut(write);
  18.  
  19.             List<string> temp = new List<string>(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
  20.             int n = Convert.ToInt32(temp[0]);
  21.             int m = Convert.ToInt32(temp[1]);
  22.             char[,] map = new char[n, m];
  23.  
  24.             for (int i = 0; i < n; i++)
  25.             {
  26.  
  27.                 string temp1 = Console.ReadLine();
  28.                 for (int j = 0; j < m; j++)
  29.                 {
  30.                     map[i, j] = Convert.ToChar(temp1[j]);
  31.                 }
  32.             }
  33.  
  34.             Graph myGraph = new Graph(map, n, m);
  35.             Console.WriteLine(myGraph.findAll());
  36.  
  37.  
  38.             read.Close();
  39.             write.Close();
  40.  
  41.         }
  42.     }
  43.  
  44.     class Graph
  45.     {
  46.         char[,] matrix;
  47.         int n, m;
  48.         bool[,] used;
  49.  
  50.         static int[] moveX = { 0, 0, -1, 1 };
  51.         static int[] moveY = { -1, 1, 0, 0 };
  52.  
  53.  
  54.         public Graph(char[,] matrix, int n, int m)
  55.         {
  56.             this.matrix = matrix;
  57.             this.m = m;
  58.             this.n = n;
  59.             this.used = new bool[n, m];
  60.         }
  61.  
  62.         public int findAll()
  63.         {
  64.             int bases = 0;
  65.             for (int i = 0; i < n; i++)
  66.                 for (int j = 0; j < m; j++)
  67.                     if (matrix[i, j] == '*' && !used[i, j])
  68.                     {
  69.                         bases++;
  70.                         DFS(i, j);
  71.                     }
  72.  
  73.  
  74.             return bases;
  75.         }
  76.  
  77.  
  78.         void DFS(int x, int y)
  79.         {
  80.             used[x, y] = true;
  81.             for (int i = 0; i < 4; i++)
  82.             {
  83.                 int nx = x + moveX[i];
  84.                 int ny = y + moveY[i];
  85.                 if (isGood(nx, ny))
  86.                     if (matrix[nx, ny] == '*' && !used[nx, ny])
  87.                         DFS(nx, ny);
  88.             }
  89.  
  90.         }
  91.  
  92.         bool isGood(int x, int y)
  93.         {
  94.             return 0 <= x && 0 <= y && x < n && y < m;
  95.         }
  96.  
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement