USACO算法系列——Big Barn

   题目:http://www.nocow.cn/index.php/Translate:USACO/bigbrn

   计算最大的正方形块。area[i][j]:表示以i,j为右下角的正方形的宽度。则

   area[i][j] = min(area[i-1][j],area[i][j-1],area[i-1][j-1])+1.

   代码如下:

#include <iostream> #include<fstream> #define SIZE 1001 using namespace std; int map[SIZE][SIZE] = {0}; int rmap[SIZE][SIZE] = {0}; int n; int t; int result = 0; ifstream fin("bigbrn.in"); ofstream fout("bigbrn.out"); void read() { fin >> n >> t; int x, y; for (int i=0; i < t; i ++) { fin >> x >> y; map[x][y] = -1; } } int issquare(int x, int y) { if (x <= 1 || y <= 1) { return 1; } else { int small = SIZE; if (rmap[x-1][y] < small) { small = rmap[x-1][y]; } if (rmap[x][y-1] < small) { small = rmap[x][y-1]; } if (rmap[x-1][y-1] < small) { small = rmap[x-1][y-1]; } return small + 1; } } void bigbrn() { for (int i=1; i <= n; i ++) { for (int j=1; j <= n; j ++) { if (map[i][j] >= 0) { rmap[i][j] = issquare(i, j); if (result < rmap[i][j]) { result = rmap[i][j]; }//end if result }//end if map[i][j] }//end for j }//end for i } int main() { read(); bigbrn(); fout<<result << endl; }

    运行结果如下:

Executing... Test 1: TEST OK [0.000 secs, 10848 KB] Test 2: TEST OK [0.000 secs, 10848 KB] Test 3: TEST OK [0.000 secs, 10848 KB] Test 4: TEST OK [0.000 secs, 10848 KB] Test 5: TEST OK [0.000 secs, 10848 KB] Test 6: TEST OK [0.000 secs, 10848 KB] Test 7: TEST OK [0.000 secs, 10848 KB] Test 8: TEST OK [0.000 secs, 10848 KB] Test 9: TEST OK [0.000 secs, 10848 KB] Test 10: TEST OK [0.027 secs, 10848 KB] Test 11: TEST OK [0.027 secs, 10848 KB] Test 12: TEST OK [0.027 secs, 10848 KB] Test 13: TEST OK [0.027 secs, 10848 KB] Test 14: TEST OK [0.027 secs, 10848 KB] Test 15: TEST OK [0.027 secs, 10848 KB] All tests OK. YOUR PROGRAM ('bigbrn') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

    有一个多月没有写博客了,主要是由于最近开题和写自然科学基金。然后做的很多事情也不被同学们理解。一直被别人嘲笑写算法,中间有段时间去做Code jam了。Google Code jam即将开赛,我也要努力学习,争取取得一点成绩啊。

    还有我的英语实在太烂了。明天还得努力。不管怎么样,先把USACO做完吧!还差11题。

  

你可能感兴趣的:(USACO算法系列——Big Barn)