Posted on 2007-05-19 22:19
ZelluX 閱讀(378)
評(píng)論(0) 編輯 收藏 所屬分類:
Algorithm
5.19
第一反應(yīng)覺得是個(gè)簡單的BFS,從最高點(diǎn)開始向四周擴(kuò)展,最后找到最長的路徑即可。
結(jié)果Wrong Answer,仔細(xì)一想,才意識(shí)到并不一定是從最高點(diǎn)開始的。考慮得太不周到了。。
5.31
原來的程序找不到了。。。重新寫了一遍,居然寫好提交就AC了,恩。這幾天進(jìn)步挺大哈
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

const int MAX_HEIGHT = 10000;
const int MAX_ROWS = 100;

const int dx[] =
{0, 0, -1, 1};

const int dy[] =
{1, -1, 0, 0};


struct Point
{
int x;
int y;
int height;
};

bool comparePoints(const Point& p1, const Point& p2);

int main()
{
//ifstream fin("pku1088.in");
int c, r;
int i, j;
cin >> r >> c;
int h[MAX_ROWS + 2][MAX_ROWS + 2];
int count = 0;
vector<Point> points;

for (i = 1; i <= r; i++)
{

for (j = 1; j <= c; j++)
{
cin >> h[i][j];
Point temp;
temp.x = i;
temp.y = j;
temp.height = h[i][j];
points.push_back(temp);
}
}

sort(points.begin(), points.end(), comparePoints);

for (i = 0; i <= c; i++)
{
h[0][i] = MAX_HEIGHT + 1;
h[r + 1][i] = MAX_HEIGHT + 1;
}

for (i = 0; i <= r; i++)
{
h[i][0] = MAX_HEIGHT + 1;
h[i][c + 1] = MAX_HEIGHT + 1;
}

int f[MAX_ROWS + 1][MAX_ROWS + 1];

for (i = 1; i <= r; i++)
{

for (int j = 1; j <= c; j++)
{
f[i][j] = 1;
}
}
vector<Point>::iterator iter;

for (iter = points.begin(); iter != points.end(); iter++)
{

for (i = 0; i < 4; i++)
{
int nx = iter->x + dx[i];
int ny = iter->y + dy[i];

if (iter->height > h[nx][ny])
{

if (f[iter->x][iter->y] + 1 > f[nx][ny])
{
f[nx][ny] = f[iter->x][iter->y] + 1;
}
}
}
}

int best = 0;

for (i = 1; i <= r; i++)
{

for (j = 1; j <= c; j++)
{

if (f[i][j] > best)
{
best = f[i][j];
}
}
}
cout << best << endl;
return 0;
}


bool comparePoints(const Point& p1, const Point& p2)
{
return (p1.height > p2.height);
}