Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace ConsoleApplication9
- {
- class Program
- {
- static void Main(string[] args)
- {
- StreamReader read = new StreamReader("input.txt");
- StreamWriter write = new StreamWriter("write.txt");
- Console.SetIn(read);
- Console.SetOut(write);
- List<string> temp = new List<string>(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
- int n = Convert.ToInt32(temp[0]);
- int m = Convert.ToInt32(temp[1]);
- char[,] map = new char[n, m];
- for (int i = 0; i < n; i++)
- {
- string temp1 = Console.ReadLine();
- for (int j = 0; j < m; j++)
- {
- map[i, j] = Convert.ToChar(temp1[j]);
- }
- }
- Graph myGraph = new Graph(map, n, m);
- Console.WriteLine(myGraph.findAll());
- read.Close();
- write.Close();
- }
- }
- class Graph
- {
- char[,] matrix;
- int n, m;
- bool[,] used;
- static int[] moveX = { 0, 0, -1, 1 };
- static int[] moveY = { -1, 1, 0, 0 };
- public Graph(char[,] matrix, int n, int m)
- {
- this.matrix = matrix;
- this.m = m;
- this.n = n;
- this.used = new bool[n, m];
- }
- public int findAll()
- {
- int bases = 0;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- if (matrix[i, j] == '*' && !used[i, j])
- {
- bases++;
- DFS(i, j);
- }
- return bases;
- }
- void DFS(int x, int y)
- {
- used[x, y] = true;
- for (int i = 0; i < 4; i++)
- {
- int nx = x + moveX[i];
- int ny = y + moveY[i];
- if (isGood(nx, ny))
- if (matrix[nx, ny] == '*' && !used[nx, ny])
- DFS(nx, ny);
- }
- }
- bool isGood(int x, int y)
- {
- return 0 <= x && 0 <= y && x < n && y < m;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement