Avoid The Lakes
Description Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of water. His insurance agency will only repay him, however, an amount depending on the size of
the largest "lake" on his farm.
The farm is represented as a rectangular grid with
N (1 ≤ N ≤ 100) rows andM (1 ≤ M ≤ 100) columns. Each cell in the grid is either dry or submerged, and exactlyK (1 ≤
K ≤ N × M) of the cells are submerged. As one would expect, a lake has a central cell to which other cells connect by sharing a long edge (not a corner). Any cell that shares a long edge with the central cell or shares a long edge
with any connected cell becomes a connected cell and is part of the lake.
Input * Line 1: Three space-separated integers: N,
M, and K
* Lines 2..K+1: Line i+1 describes one submerged location with two space separated integers that are its row and column:R and
C
Output * Line 1: The number of cells that the largest lake contains. Sample Input 3 4 5 3 2
3 1 2 2
2 3
1 1
Sample Output 4 |
介個題用DFS就可以解決,主要是我的有瑕疵,不會AC。
代碼:
<div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;">#include <cstdio></span></div><div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;">#include <cstring></span></div>#include <cmath>
<div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;">#include <algorithm></span></div>#include <climits>
#include <queue>
<div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;">int map[M][M];</span></div>#define M 110
using namespace std;
<div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;">int ans,n,m,k;</span></div>int fx[4]={1,-1,0,0};
int fy[4]={0,0,1,-1};
<div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;"> if(map[x+fx[i]][y+fy[i]]==1&&x>=0&&x<n&&y>=0&&y<m)</span></div>void f(int x,int y)
{
for(int i=0;i<4;i++)
{
{
<div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;"> }</span></div> map[x+fx[i]][y+fy[i]]=0;
ans++;
f(x+fx[i],y+fy[i]);
}
}
int main()
{
<div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;"> memset(map,0,sizeof(map));</span></div> int x,y;
while(~scanf("%d%d%d",&n,&m,&k))
{
int maxn=0;
for(int i=0;i<k;i++)
{
<div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;"> for(int j=0;j<m;j++){</span></div> scanf("%d%d",&x,&y);
map[x][y]=1;
}
for(int i=0;i<n;i++){
ans=1;
if(map[i][j]) f(i,j);
<div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;">}</span></div> if(ans>maxn) maxn=ans;
}
}
printf("%d\n",maxn);
}
<div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;"> return 0;</span></div>