您的位置:首页 > 其它

POJ 1979 解题报告

2014-12-03 10:35 411 查看
这道题是简单的DFS。感觉c++输入比较头疼。

1979Accepted728K0MSG++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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: