按照http://blog.csdn.net/thestoryofsnow/article/details/40942009的顺序刷,果然轻松愉快。这道题就是DFS,同时记录结果避免重复。做完后看了看解法,大致如此,大同小异,表示对poj上0ms过的很惊奇,不知如何做到的。
1088 | Accepted | 336K | 63MS | C++ | 1683B |
/* ID: thestor1 LANG: C++ TASK: poj1088 */ #include <iostream> #include <fstream> #include <cmath> #include <cstdio> #include <cstring> #include <climits> #include <cassert> #include <string> #include <vector> #include <list> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> #include <cassert> using namespace std; int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; void DFS(int r, int c, const int R, const int C, std::vector<std::vector<int> > &heights, std::vector<std::vector<int> > &seqs) { seqs[r][c] = 1; int nr, nc; for (int d = 0; d < 4; ++d) { nr = r + dx[d], nc = c + dy[d]; if (0 <= nr && nr < R && 0 <= nc && nc < C && heights[r][c] > heights[nr][nc]) { if (seqs[nr][nc] < 0) { DFS(nr, nc, R, C, heights, seqs); } if (seqs[nr][nc] + 1 > seqs[r][c]) { seqs[r][c] = seqs[nr][nc] + 1; } } } } int main() { int R, C; cin >> R >> C; std::vector<std::vector<int> > heights(R, std::vector<int>(C, 0)); std::vector<std::vector<int> > seqs(R, std::vector<int>(C, -1)); for (int r = 0; r < R; ++r) { for (int c = 0; c < C; ++c) { cin >> heights[r][c]; } } // cout << "heights:" << endl; // for (int r = 0; r < R; ++r) // { // for (int c = 0; c < C; ++c) // { // cout << heights[r][c]; // } // cout << endl; // } int maxseq = 1; for (int r = 0; r < R; ++r) { for (int c = 0; c < C; ++c) { if (seqs[r][c] < 0) { DFS(r, c, R, C, heights, seqs); } if (seqs[r][c] > maxseq) { maxseq = seqs[r][c]; } } } cout << maxseq << endl; return 0; }