[Topcoder] SRM211

 DivI lev2

 

定义grid,0为空,1代表有障碍或已考虑

从每个格式开始flood fill一遍,记录flood fill的格子数量

#include <iostream> #include <queue> #include <vector> #include <cstring> #include <sstream> #include <algorithm> #define HEIGHT 400 #define WIDTH 600 using namespace std; typedef struct pixel { int x; int y; } pixel; const int xoffset[4] = {1, 0, -1, 0}; const int yoffset[4] = {0, 1, 0, -1}; class grafixMask { private: vector<int> holes; int grid[HEIGHT][WIDTH]; int isValid(pixel n) { if (n.x < 0 || n.x > HEIGHT - 1 || n.y < 0 || n.y > WIDTH - 1 || grid[n.x][n.y]) return 0; return 1; } public: vector<int> sortedAreas(vector<string> rectangles) { memset(grid, 0, sizeof(int) * HEIGHT * WIDTH); for (int i = 0; i < rectangles.size(); i++) { istringstream ss(rectangles[i]); int tlx, tly, brx, bry; ss >> tlx >> tly >> brx >> bry; for (int x = tlx; x <= brx; x++) for (int y = tly; y <= bry; y++) grid[x][y] = 1; } for (int x = 0; x < HEIGHT; x++) for (int y = 0; y < WIDTH; y++) { if (grid[x][y] == 1) continue; //cout << "x = " << x << " y = " << y << endl; queue<pixel> q; int cnt = 1; pixel p; p.x = x; p.y = y; grid[x][y] = 1; q.push(p); while (!q.empty()) { p = q.front(); q.pop(); pixel newp; for (int i = 0; i < 4; i++) { newp.x = p.x + xoffset[i]; newp.y = p.y + yoffset[i]; if (isValid(newp)) { cnt++; //cerr << cnt << endl; grid[newp.x][newp.y] = 1; q.push(newp); } } } holes.push_back(cnt); } sort(holes.begin(), holes.end()); return holes; } }; int main() { grafixMask gm; // example 0 cout << "example 0: " << endl; vector<string> vec; vec.push_back("0 292 399 307"); vec.push_back("0 292 399 307"); vector<int> res = gm.sortedAreas(vec); return 0; }

你可能感兴趣的:(struct,Class)