HDU 1198 Farm Irrigation (query set optimization, diagram)
This is similar to the HDU smooth project, except that the smooth project provides the connection relationship between numbers,
This question requires you to determine the connectivity, that is, whether the two water pipes can be connected together is also the difficulty of this question.
Record the status and continuously combine (). Note that you only need to judge the left side and the top side. This will not repeat the judgment, and it will certainly be able to traverse all the states.
# Include
# Include
# Include
// Record the shape of the pipe. Each pipe is represented by a 4-character string consisting of '0' and '1'. // It indicates whether there is an interface between the upper, lower, and left sides, respectively, '0' none, '1' has char a [11] [5] = {"1010", "1001", "0110", "0101", "1100 ", "0011", "1011", "1110", "0111", "1101", "1111"}; int father [51] [51]; char map [51] [51]; int n, m; using namespace std; int find (int x) // find the parent node, and the compression path {if (father [x/n] [x % n]! = X) father [x/n] [x % n] = find (father [x/n] [x % n]); return father [x/n] [x % n];} void Union (int x, int y) // merges the set of x and y {x = find (x ); y = find (y); if (x! = Y) father [y/n] [y % n] = x;} void judge (int I, int j) // determine whether map [I] [j] is connected to the left and top sides of map, for example, if (j> 0 & a [map [I] [j]-'a'] [2] = '1' & A [map [I] [J-1]-'a'] [3] = '1 ') // determine the Union above (I * n + j, I * n + J-1 ); if (I> 0 & a [map [I] [j]-'a'] [0] = '1' & A [map [I-1] [j] -'a'] [1] = '1 ') // determine the left side of Union (I * n + j, (I-1) * n + j);} int main () {int I, j, count; while (scanf ("% d", & m, & n )! =-1 & (n! =-1 | m! =-1) {for (I = 0; I