poj 1979 DFS

类似poj 2383的八连通,然而这只是四个方向移动,还是很简单的.....不过这题还让我WA一次,基本功不扎实,尽然行号和列号混淆了。大哭


/*
 * =====================================================================================
 *
 *       Filename:  poj_1979.cpp
 *
 *    Description:  poj 1979 red and black
 *
 *        Version:  1.0
 *        Created:  03/29/2015 01:39:09 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *		   Author:  Kart, 786900722@qq.com
 *           Blog:  http://blog.youkuaiyun.com/linux_kernel_fan/
 *
 * =====================================================================================
 */

#include<iostream>
#include<cstdio>
#include<cstring>
#include<utility>
#define MAX 20

using namespace std;
int W, H;
char mat[MAX+1][MAX+1];
int sx, sy;
pair<int, int> dir[4] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int count;
bool in_range(int x, int y)
{
	if(x >= 0 && x < H && y >= 0 && y < W)
		return true;
	else
		return false;
}

void solve(int x, int y)
{
	mat[x][y] = '#';
	for(int i = 0; i < 4; i++)
	{
		int tx = dir[i].first + x;
		int ty = dir[i].second + y;
		if(mat[tx][ty] == '.' && in_range(tx, ty))
		{
			count ++;
			solve(tx, ty);
		}
	}
	return ;
}

int main()
{
	scanf("%d%d", &W, &H);
	while(W && H)
	{
		count = 0;
		for(int i =0; i < H; i++)
		{
			scanf("%s", mat[i]);
			for(int j = 0; j < W; j++)
			{
				if(mat[i][j] == '@')
				{
					sx = i;
					sy = j;
				}
			}
		}
		solve(sx, sy);
		printf("%d\n", count + 1);
		scanf("%d%d", &W, &H);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值