The Castle
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6907 | Accepted: 3877 |
Description
1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#---#####---# 2 # # | # # # # # #---#####---#####---#####---# 3 # | | # # # # # #---#########---#####---#---# 4 # # | | | | # # ############################# (Figure 1) # = Wall | = No wall - = No wall
Figure 1 shows the map of a castle.Write a program that calculates
1. how many rooms the castle has
2. how big the largest room is
The castle is divided into m * n (m<=50, n<=50) square modules. Each such module can have between zero and four walls.
Input
Your program is to read from standard input. The first line contains the number of modules in the north-south direction and the number of modules in the east-west direction. In the following lines each module is described by a number (0 <= p <= 15). This number is the sum of: 1 (= wall to the west), 2 (= wall to the north), 4 (= wall to the east), 8 (= wall to the south). Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1. The castle always has at least two rooms.
Output
Your program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).
Sample Input
4 7 11 6 11 6 3 10 6 7 9 6 13 5 15 5 1 10 12 7 13 7 5 13 11 10 8 10 12 13
Sample Output
5 9
Source
这类题目做过好几遍了
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson num<<1,le,mid
#define rson num<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk make_pair
#define _f first
#define _s second
using namespace std;
//const int INF= ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
//const int maxn= ;
//const int maxm= ;
int m,n;
int dir[4][2]={ {0,-1},{-1,0},{0,+1},{+1,0} };
int num[53*53];
int pre[53*53];
int getid(int r,int c)
{
return (r-1)*n+c;
}
void init()
{
for(int i=1;i<=m*n;i++)
{
pre[i]=i;
}
}
int find(int x)
{
return x==pre[x]?x:pre[x]=find(pre[x]);
}
void merge(int s,int t)
{
int x=find(s);
int y=find(t);
if(x==y) return;
pre[x]=y;
}
void cope(int x,int r,int c)
{
for(int i=0;i<2;i++)
{
if( (1<<i)&x ) continue;
int id=getid(r+dir[i][0],c+dir[i][1]);
int x=getid(r,c);
merge(id,x);
}
}
void cal()
{
memset(num,0,sizeof num);
for(int i=1;i<=m*n;i++)
{
int root=find(i);
num[root]++;
}
int cnt=0,maxi=0;
for(int i=1;i<=n*m;i++)
{
if(!num[i]) continue;
cnt++;
maxi=max(maxi,num[i]);
}
printf("%d\n%d\n",cnt,maxi);
}
int main()
{
scanf("%d%d",&m,&n);
init();
int x;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&x);
cope(x,i,j);
}
}
cal();
return 0;
}