洛谷P1506 拯救oibh总部【DFS】

题目链接:P1506 拯救oibh总部

程序说明:

深度优先搜索,和P1162填涂颜色差不多(没有区别)。

参考题解:P1506 拯救oibh总部 题解

代码如下:

#include <iostream>
#define MAX 520 
using namespace std;
int a[MAX][MAX];
int n, m, cnt = 0;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
void dfs(int x, int y) {
	if(x < 0 || y < 0 || x > n + 1 || y > m + 1 || a[x][y] == 1)
		return;
	a[x][y] = 1;
	for(int i = 0; i < 4; i++) {
		dfs(x + dx[i], y + dy[i]);
	}		
}
int main() {
	cin>>n>>m;
	char ch;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++) {
			cin>>ch;
			if(ch == '*')
				a[i][j] = 1;
			else
				a[i][j] = 0; 
		}	
	dfs(0, 0);
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++) {
			if(a[i][j] == 0)
				cnt++;
		}
	cout<<cnt;
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值