#include <iostream> #include <vector> using std::cout; using std::cin; using std::endl; using std::vector; size_t MinInOneLine(const vector<vector<int> > & vec); size_t MinInOneDirection(vector<vector<int> > & vec); size_t MinDrawLine(const size_t N); int main() { size_t S = 0; size_t N = 0; cin >> S; for (size_t index = 0; index < S; ++index) { cin >> N; cout << MinDrawLine(N) << endl; } return 0; } size_t MinDrawLine(const size_t N) { if (N < 2) return N; int x0 = 0; int y0 = 0; int x1 = 0; int y1 = 0; vector<vector<int> > vecType1; // x vector<vector<int> > vecType2; // y vector<vector<int> > vecType3; // 45 vector<vector<int> > vecType4; // -45 for (size_t index = 0; index < N; ++index) { cin >> x0 >> y0 >> x1 >> y1; vector<int> vecTemp; vecTemp.push_back(x0); vecTemp.push_back(y0); vecTemp.push_back(x1); vecTemp.push_back(y1); // divide into four type if (y0 == y1) vecType1.push_back(vecTemp); // x else if (x0 == x1) // y { vecTemp[0] = vecTemp[1]; vecTemp[2] = vecTemp[3]; vecType2.push_back(vecTemp); } else if ((y1 - y0) / (x1 - x0) > 0) vecType3.push_back(vecTemp); // 45 else vecType4.push_back(vecTemp); // -45 } return MinInOneDirection(vecType1) + MinInOneDirection(vecType2) + MinInOneDirection(vecType3) + MinInOneDirection(vecType4); } size_t MinInOneDirection(vector<vector<int> > & vec) { if (vec.size() < 2) return vec.size(); size_t nRet = 0; bool * pbFlag = new bool[vec.size()]; for (int i = 0; i < vec.size(); ++i) pbFlag[i] = false; // divide into different lines for (int i = 0; i < vec.size(); ++i) { if (pbFlag[i] == true) continue; vector<vector<int> > vecTemp; vecTemp.push_back(vec[i]); for (int j = i + 1; j < vec.size(); ++j) { bool i = pbFlag[j]; if (pbFlag[j] == false && vec[j][0] != vec[i][0] && (vec[j][1] - vec[i][1]) / (vec[j][0] - vec[i][0]) == (vec[i][3] - vec[i][1]) / (vec[i][2] - vec[i][0])) { vecTemp.push_back(vec[j]); pbFlag[j] = true; } } nRet += MinInOneLine(vecTemp); } delete[] pbFlag; return nRet; } size_t MinInOneLine(const vector<vector<int> > & vec) { if (vec.size() == 0) return 0; if (vec.size() == 1) return 1; size_t nRet = 1; // make a hash table of [nMin, nMax] int nMin = 0x7fffffff; int nMax = 0x80000000; for (int index = 0; index < vec.size(); ++index) { nMin = vec[index][0] < nMin ? vec[index][0] : nMin; nMin = vec[index][2] < nMin ? vec[index][2] : nMin; nMax = vec[index][0] > nMax ? vec[index][0] : nMax; nMax = vec[index][2] > nMax ? vec[index][2] : nMax; } bool * pbHash = new bool[nMax - nMin + 1]; for (int index = 0; index <= nMax - nMin; ++index) pbHash[index] = false; // map each drawline into x zone int nBegin = 0; int nEnd = 0; for (int index = 0; index < vec.size(); ++index) { nBegin = vec[index][0] < vec[index][2] ? vec[index][0] : vec[index][2]; nEnd = vec[index][0] == nBegin ? vec[index][2] : vec[index][0]; for (int j = nBegin; j <= nEnd; ++j) pbHash[j - nMin] = true; } // count min drawline in one line bool flag = false; for (int index = nMin; index <= nMax; ++index) { if (flag == true) { if (pbHash[index - nMin] == true) { nRet++; flag = false; } continue; } if (flag == false && pbHash[index - nMin] == false) flag == true; } delete[] pbHash; return nRet; }
时间: 2024-10-13 11:52:24