NOI 2.8 图 2405: Avoid The Lakes(深搜&广搜)

题目来源:http://noi.openjudge.cn/ch0308/2405/

2405: Avoid The Lakes

总时间限制20000ms  单个测试点时间限制1000ms  内存限制65536kB

描述

Farmer John's farm was flooded in the most recent storm, a factonly aggravated by the information that his cows are deathly afraid of water.His insurance agency will only repay him, however, an amount depending on thesize of the largest "lake" on his farm.

The farm is represented as a rectangular grid with N (1 <= N <= 100) rowsand M (1 <= M <= 100) columns. Each cell in the grid is either dry orsubmerged, and exactly K (1 <= K <= N*M) of the cells are submerged. Asone would expect, a lake has a central cell to which other cells connect bysharing a long edge (not a corner). Any cell that shares a long edge with thecentral cell or shares a long edge with any connected cell becomes a connectedcell and is part of the lake.

输入

* Line 1: Three space-separated integers: N, M, and K

* Lines 2..K+1: Line i+1 describes one submerged location with two spaceseparated integers that are its row and column: R and C

输出

* Line 1: The number of cells that the largest lake contains.

样例输入

3 4 5
3 2
2 2
3 1
2 3
1 1

样例输出

4

提示

INPUT DETAILS:

The farm is a grid with three rows and four columns; five of the cells are submerged. They are located in the positions (row 3, column 2); (row 2, column 2); (row 3, column 1); (row 2, column 3); (row 1, column 1):
              # . . .
              . # # .
              # # . .


OUTPUT DETAILS:

The largest lake consists of the input's first four cells.

来源

USACO November 2007 Bronze

-----------------------------------------------------

思路

求最大面积的“湖”的面积。深搜和广搜都能解决,且性能相差不大。

-----------------------------------------------------

代码

深搜版

// 深度优先搜索版

#include<iostream>
#include<fstream>
#include<queue>
using namespace std;

const int NMAX = 105;
int n,m,s=0;
int mat[NMAX][NMAX] = {};

struct node {
	int x,y;
};

void dfs(int x, int y)
{
	mat[x][y] = 0;
	s++;
	if (x>0 && mat[x-1][y])
		dfs(x-1, y);
	if (y>0 && mat[x][y-1])
		dfs(x, y-1);
	if (x<n-1 && mat[x+1][y])
		dfs(x+1, y);
	if (y<m-1 && mat[x][y+1])
		dfs(x, y+1);
}


int main()
{
#ifndef ONLINE_JUDGE
	ifstream fin ("0308_2405.txt");
	int k,i,j,x,y,smax;
	fin >> n >> m >> k;
	for (i=0; i<k; i++)
	{
		fin >> x >> y;
		mat[--x][--y] = 1;
	}
	fin.close();
	smax = 0;
	for (i=0; i<n; i++)
	{
		for (j=0; j<m; j++)
		{
			if (mat[i][j])
			{
				s = 0;
				dfs(i,j);
				smax = smax>s? smax:s;
			}
		}
	}
	cout << smax;
	return 0;
#endif
#ifdef ONLINE_JUDGE
	int k,i,j,x,y,smax;
	cin >> n >> m >> k;
	for (i=0; i<k; i++)
	{
		cin >> x >> y;
		mat[--x][--y] = 1;
	}
	smax = 0;
	for (i=0; i<n; i++)
	{
		for (j=0; j<m; j++)
		{
			if (mat[i][j])
			{
				s = 0;
				dfs(i,j);
				smax = smax>s? smax:s;
			}
		}
	}
	cout << smax;
#endif
}

广搜版

// 广度优先搜索版

#include<iostream>
#include<fstream>
#include<queue>
using namespace std;

const int NMAX = 105;
int n,m;
int mat[NMAX][NMAX] = {};

struct node {
	int x,y;
};

int bfs(int x, int y)
{
	int cnt = 1,xx,yy;
	mat[x][y] = 0;
	node N;
	N.x = x;
	N.y = y;
	queue<node> q;
	q.push(N);
	while (!q.empty())
	{
		N = q.front();
		q.pop();
		xx = N.x;
		yy = N.y;
		mat[xx][yy] = 0;
		if (xx>0 && mat[xx-1][yy])
		{
			mat[xx-1][yy] = 0;
			N.x = xx-1;
			N.y = yy;
			q.push(N);
			cnt++;
		}
		if (yy>0 && mat[xx][yy-1])
		{
			mat[xx][yy-1] = 0;
			N.x = xx;
			N.y = yy-1;
			q.push(N);
			cnt++;
		}
		if (xx<n-1 && mat[xx+1][yy])
		{
			mat[xx+1][yy] = 0;
			N.x = xx+1;
			N.y = yy;
			q.push(N);
			cnt++;
		}
		if (yy<m-1 && mat[xx][yy+1])
		{
			mat[xx][yy+1] = 0;
			N.x = xx;
			N.y = yy+1;
			q.push(N);
			cnt++;
		}
	}
	return cnt;
}


int main()
{
#ifndef ONLINE_JUDGE
	ifstream fin ("0308_2405.txt");
	int k,i,j,x,y,smax,s;
	fin >> n >> m >> k;
	for (i=0; i<k; i++)
	{
		fin >> x >> y;
		mat[--x][--y] = 1;
	}
	fin.close();
	smax = 0;
	for (i=0; i<n; i++)
	{
		for (j=0; j<m; j++)
		{
			if (mat[i][j])
			{
				s = bfs(i,j);
				smax = smax>s? smax:s;
			}
		}
	}
	cout << smax;
	return 0;
#endif
#ifdef ONLINE_JUDGE
	int k,i,j,x,y,smax,s;
	cin >> n >> m >> k;
	for (i=0; i<k; i++)
	{
		cin >> x >> y;
		mat[--x][--y] = 1;
	}
	smax = 0;
	for (i=0; i<n; i++)
	{
		for (j=0; j<m; j++)
		{
			if (mat[i][j])
			{
				s = bfs(i,j);
				smax = smax>s? smax:s;
			}
		}
	}
	cout << smax;
#endif
}


猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/80713676
今日推荐