POJ 1979 解题报告
2014-12-03 10:35
411 查看
这道题是简单的DFS。感觉c++输入比较头疼。
1979 | Accepted | 728K | 0MS | G++ | 1846B |
/* ID: thestor1 LANG: C++ TASK: poj1979 */ #include <iostream> #include <fstream> #include <cmath> #include <cstdio> #include <cstring> #include <limits> #include <string> #include <vector> #include <list> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> #include <cassert> using namespace std; const int MAXW = 20; const int MAXH = 20; int dr[4] = {-1, 0, 1, 0}, dc[4] = {0, 1, 0, -1}; void DFS(int r, int c, const std::vector<string> &data, std::vector<std::vector<bool> > &visited, int &cnt, const int H, const int W) { cnt++; visited[r][c] = true; // cout << "[debug]data[" << r << "][" << c << "]:" << data[r][c] << endl; int nr, nc; for (int d = 0; d < 4; ++d) { nr = r + dr[d], nc = c + dc[d]; if (0 <= nr && nr < H && 0 <= nc && nc < W && !visited[nr][nc] && data[nr][nc] == '.') { DFS(nr, nc, data, visited, cnt, H, W); } } } int main() { std::ios::sync_with_stdio(false); int W, H; std::vector<string> data(MAXH, string(MAXW, '.')); std::vector<std::vector<bool> > visited(MAXH, std::vector<bool>(MAXW, false)); string line; int x, y; while (cin >> W >> H && W) { // cout << "H: " << H << ", W: " << W << endl; for (int h = 0; h < H;) { getline(cin, line); if (!line.size()) { continue; } // cout << "[debug]line:[" << line << "]" << endl; for (int w = 0; w < W; ++w) { data[h][w] = line[w]; visited[h][w] = false; if (line[w] == '@') { x = h; y = w; } } ++h; } // for (int r = 0; r < H; ++r) // { // for (int c = 0; c < W; ++c) // { // cout << data[r][c]; // } // cout << endl; // } int cnt = 0; DFS(x, y, data, visited, cnt, H, W); cout << cnt << endl; } return 0; }
相关文章推荐
- POJ 1979 Red and Black 解题报告
- 解题报告_POJ1979-red and black_深搜水题
- poj解题报告——1012
- POJ_3347 Snowflake Snow Snowflakes 解题报告
- POJ 1088 解题报告
- POJ - 3070 Fibonacci 解题报告
- poj 3768 解题报告
- POJ 2479 Maximum sum 动态规划 解题报告
- poj解题报告——2209
- poj 3728 LCA+并查集 解题报告
- POJ 1004(平均数) 解题报告
- 线段树和单调队列优化DP---POJ2373解题报告
- poj 2367 Genealogical tree 拓扑排序 解题报告
- POJ 1056 解题报告 Trie 树
- poj解题报告——2960
- POJ - 3176 Cow Bowling解题报告(求三角形数最大路)
- POJ 2398 Toy Storage(解题报告)
- POJ - 1459 Power Network解题报告(网络流最大流 超级源点建图)
- 解题报告——POJ 1579
- poj 1142Smith Numbers(解题报告)