POJ 1164

这道题的题目叙述着实唬人,感觉太多无关信息了

不过说来惭愧,偷了个小懒去看了下别人怎么翻译的,这部分小痛苦直接跳过了

简单DFS应用

#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
using namespace std;

const int maxn= 55;

int m, n;
int cas[maxn][maxn];
bool vis[maxn][maxn];
int step[4][2]= {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};

int DFS(int x, int y)
{
	if (x< 1 || x> m || y< 1 || y>n){
		return 0;
	}
	int ret= 1;

	for (int i= 0; i< 4; ++i){
		if (cas[x][y] & (1<<i)){
			continue;
		}
		int nx= x+step[i][0], ny= y+step[i][1];
		if (!vis[nx][ny]){
			vis[nx][ny]= 1;
			ret+= DFS(x+step[i][0], y+step[i][1]);
		}
	}

	return ret;
}
int main(int argc, char const *argv[])
{
	scanf("%d %d", &m, &n);
	for (int i= 1; i<= m; ++i){
		for (int j= 1; j<= n; ++j){
			scanf("%d", cas[i]+j);
		}
	}

	int ans=0, mx= 0;
	memset(vis, 0, sizeof(vis));
	for (int i= 1; i<= m; ++i){
		for (int j= 1; j<= n; ++j){
			if (!vis[i][j]){
				++ans;
				vis[i][j]= 1;
				mx= max(mx, DFS(i, j));
			}
		}
	}

	printf("%d
%d
", ans, mx);

	return 0;
}
原文地址:https://www.cnblogs.com/Idi0t-N3/p/14698219.html