上一篇里,代码没一起发出来,因为百度居然限制字数,真垃圾。

int spreadDetect(CSqure& squre, int x, int y, CHooker pHokker)
{
       static CSqure tmp;
       static bool first = true;
       bool lock = first;
       if (first)
       {
             int i, j;
             tmp.resize(squre.size());
             for(i = 0; i < squre.size(); ++i)
             {
                   tmp[i].resize(squre[i].size());
                   for(j = 0; j < squre[i].size(); ++j)
                         tmp[i][j] = 0;
             }      
             first = false;
       }
       tmp[x][y] = 1;
       int counter = 0;
       if ((x > 0) && (tmp[x - 1][y] == 0) && (squre[x][y] == squre[x - 1][y]))
             counter = 1 + spreadDetect(squre, x - 1, y, pHokker);
       if ((y > 0) && (tmp[x][y - 1] == 0) && (squre[x][y] == squre[x][y - 1]))
             counter = 1 + spreadDetect(squre, x, y - 1, pHokker);
       if ((x < squre.size() - 1) && (tmp[x + 1][y] == 0) && (squre[x][y] == squre[x + 1][y]))
             counter = 1 + spreadDetect(squre, x + 1, y, pHokker);
       if ((y < squre[0].size() - 1) && (tmp[x][y + 1] == 0) && (squre[x][y] == squre[x][y + 1]))
             counter = 1 + spreadDetect(squre, x, y + 1, pHokker);
       if (pHokker)
       {
             int r = 0;
             r = pHokker(squre, x, y);
             if (r < 0)
                   return r; 
       }
       first = lock;
       return counter;
}
int ChangeSqure(CSqure& squre, int x, int y)
{
       if (x < 0)
             return -1;
       int r = spreadDetect(squre, x, y, changer);
       if (r < 0)
             return r;
       int i, j, k;
       for(j = 0; j < squre[0].size(); ++j)
       {
             for(i = squre.size() - 1; i >=0 ; --i)
             {
                   if (squre[i][j] == -1)
                         continue;
                   for(k = i + 1; k < squre.size(); ++k)
                   {
                         if (squre[k][j] == -1)
                         {
                               if( (k + 1) == squre.size())
                                     break;
                               else if (squre[k + 1][j] != -1)
                                     break;
                         }
                   }
                   if (k < squre.size())
                   {
                         squre[k][j] = squre[i][j];
                         squre[i][j] = -1;
                   }
             }
       }
       for(j = 0; j < squre[0].size(); ++j)
       {
             for(i = 0; i < squre.size(); ++i)
             {
                   if (squre[i][j] != -1)
                         break;
             }
             if (i == squre.size())
             {
                   int exists = 0;
                   for(i = 0; i < squre.size(); ++i)
                   {
                         for(k = j; k < squre[i].size(); ++k)
                          {
                               if (squre[i][k] != -1)
                                     ++exists;
                               if (k < squre[i].size() - 1)
                                     squre[i][k] = squre[i][k + 1];
                               else
                                     squre[i][k] = -1;
                         }
                   }
                   if (exists == 0)
                         break;
                   else
                         --j;
             }
       }
       return 0;
}
int changer(CSqure& squre, int x, int y)
{
       if (squre[x][y] >= 0)
             squre[x][y] = -1;
       else
             return -1;
       return 0;
}
int PrintSqure(const CSqure& squre, int x, int y)
{
       int i, j, left = squre.size() * squre[0].size();
       std::cout << "\n\n";
       for(i = 0; i < squre.size(); ++i)
       {
             for(j = 0; j < squre[i].size(); ++j)
             {
                   switch (squre[i][j])
                   {
                   case -1:
                         --left;
                         std::cout << ". ";
                         break;
                   case 0:
                         std::cout << "* ";
                         break;
                   case 1:
                         std::cout << "X ";
                         break;
                   case 2:
                         std::cout << "# ";
                         break;
                   case 3:
                         std::cout << "O ";
                         break;
                   }
             }
             std::cout << "\n";
       }
       return left;
}

[原文在百度空间已经关闭]