int countBattleships(vector<vector<char>>& board)
        {
            int m = board.size();
            if (m == 0)
            {
                return 0;
            }
            int n = board[0].size();
            if (n == 0)
            {
                return 0;
            }
            vector<int> disjoint_sets;
            disjoint_sets.resize(m * n);
            for (int i = 0; i < m * n; i++)
            {
                disjoint_sets[i] = -1;
            }
            for (int r = 0; r < m - 1; r++)
            {
                for (int c = 0; c < n - 1; c++)
                {
                    if (board[r][c] == 'X' && board[r + 1][c] == 'X')
                    {
                        union_sets(disjoint_sets, r * n + c, (r + 1) * n + c);
                    }
                    if (board[r][c] == 'X' && board[r][c + 1] == 'X')
                    {
                        union_sets(disjoint_sets, r * n + c, r * n + c + 1);
                    }
                }
                if (board[r][n - 1] == 'X' && board[r + 1][n - 1] == 'X')
                {
                    union_sets(disjoint_sets, r * n + (n - 1), (r + 1) * n + (n - 1));
                }
            }
            for (int c = 0; c < n - 1; c++)
            {
                if (board[m - 1][c] == 'X' && board[m - 1][c + 1] == 'X')
                {
                    union_sets(disjoint_sets, (m - 1) * n + c, (m - 1) * n + c + 1);
                }
            }

            int count = 0;
            for (int r = 0; r < m; r++)
            {
                for (int c = 0; c < n; c++)
                {
                    if (board[r][c] == 'X' && disjoint_sets[r * n + c] < 0)
                    {
                        count++;
                    }
                }
            }

            return count;
        }
Beispiel #2
0
void solve_case()
{
  int i,j,k;

  /* 1. construct edges for all pairs of stones */

  k = 0;
  for (i=0; i<n; i++)
    for (j=i+1; j<n; j++)
      {
	e[k].u = i;
	e[k].v = j;
	e[k].w = SQR(x[i]-x[j])+SQR(y[i]-y[j]);
	k++;
      }

  /* 2. sort edges by length */

  qsort(e,k,sizeof(edge),edgecmp);

  /* 3. generate a forest of one tree per stone */

  for (i=0; i<n; i++)
    make_set(i);

  /* 4. while Freddy and Fiona are not in the same tree, add an edge to the forest */

  for (i=0; !same_set(0,1); i++)
    union_sets(e[i].u,e[i].v);
  
  /* 5. output */

  printf("Scenario #%d\n",++scenario);	
  printf("Frog Distance = %.3f\n\n",sqrt((double)(e[i-1].w)));
}
Beispiel #3
0
int main(int argc, char **argv)
{
    int i1=1, i2=2, i3=3;

    forest_node_t *s1=make_set(&i1), *s2=make_set(&i2), *s3=make_set(&i3);

    assert(find_set(s1) == s1);
    union_sets(s1, s2);

    assert(find_set(s1) == find_set(s2));
    assert(find_set(s1) != find_set(s3));
    union_sets(s2, s3);

    assert(find_set(s1) == find_set(s2) &&
           find_set(s1) == find_set(s3));

    return 0;
}
Beispiel #4
0
void solve()
{
    int i, max;

    for ( i = 0; i < s.ne; i++ )
        if ( !same_component( edges[i].x, edges[i].y ) )
            union_sets( edges[i].x, edges[i].y );

    max = 1;

    for ( i = 0; i < s.nv; i++ )
        if ( s.size[i] > max )
            max = s.size[i];

    printf("%i\n", max );
}
Beispiel #5
0
/**
 * Runs Kruskal MSF algorithm
 * @param el pointer to sorted edge list 
 * @param fnode_array pointer to forest nodes array
 * @param edge_membership designates whether an edge is part of the MSF
 */ 
void kruskal(edgelist_t *el, 
             forest_node_t **fnode_array,
             unsigned int *edge_membership)
{
    unsigned int i;
    edge_t *pe;
    
    assert(fnode_array);
    assert(edge_membership);

    // For each edge, in order by non-decreasing weight...
    for ( i = 0; i < el->nedges; i++ ) { 
        pe = &(el->edge_array[i]);

        forest_node_t *set1 = find_set(fnode_array[pe->vertex1]);
        forest_node_t *set2 = find_set(fnode_array[pe->vertex2]);

        // vertices belong to different forests
        if ( set1 != set2 ) {
            union_sets(set1, set2);
            edge_membership[i] = 1;
        } 
    }
}