POJ 1979 解题报告

这道题是简单的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;  
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值