[Topcoder] SRM211

來源:互聯網
上載者:User

 DivI lev2

 

定義grid,0為空白,1代表有障礙或已考慮

從每個格式開始flood fill一遍,記錄flood fill的格子數量

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.