Advertisement
Guest User

c# islands

a guest
Oct 19th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. int sizex, sizey;
  2. int [,]map;
  3. int [,]check;
  4. Dictionary<int,island_info> islands;
  5.  
  6. class island_info
  7. {
  8. public int x; public  int y; public int area;
  9. }
  10.  
  11. int area(int x, int y, int island, int island_id)
  12. {
  13.    //проверка выхода за границы
  14.    if(x < 0 || y < 0 || x >= sizex || y>=sizey)
  15.        return 0;
  16.    //проверка принадлежности нужному острову и не проверяли ли раньше
  17.    if(map[x,y] != island || check[x,y] != 0)
  18.        return 0;
  19.    //отмечаем принадлежность острову
  20.    check[x,y] = island_id;
  21.    /считаем площадь с соседями
  22.    return 1 + area(x-1,y, island, island_id) + area(x+1,y, island, island_id) +area(x,y+1, island, island_id) +area(x,y-1, island, island_id);
  23. }
  24.  
  25. public void Area()
  26. {
  27.    check = new int[sizex,sizey];
  28.    islands = new Dictionary<int, island_info>();
  29.    int n = 1;
  30.    for(int i =0;i<sizex;i++)
  31.       for(int j=0;j<sizey;j++)
  32.          if(map[i,j]!=0 && check[i,j]==0)
  33.          {
  34.              var info = new island_info();
  35.              info.x = i;
  36.          info.y = j;
  37.              info.area = area(i,j,map[i,j], n);
  38.              n++;
  39.              islands.add(n, info); 
  40.          }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement