当前位置: 代码迷 >> 综合 >> 【openjudge】The Castle
  详细解决方案

【openjudge】The Castle

热度:83   发布时间:2023-11-27 23:07:40.0

166:The Castle

Total time limit: 1000ms
Memory limit: 65536kB
P.S.如果有需要中文版的同学轻点中文版,需要源代码的同学轻点源代码。

describe

      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:
IOI 1994

【Code】

状态: Accepted

#include<iostream> 
#include<cstdio> 
#include<cstring> 
using namespace std;  
int m,n,ans,cnt,bigest; 
bool flag[100][100],q[100][100][4];  
int map[105][105];  
int wayr[4]={
   0,1,0,-1},wayc[4]={
   1,0,-1,0}; 
bool check(int a,int b,int x) 
{  if(a<m&&b<n&&a>=0&&b>=0&&q[a][b][x]&&!flag[a][b]) return 1;  return 0;  
}  
void dfs(int y,int c) 
{  for(int i=0;i<4;i++)if(check(y+wayr[i],c+wayc[i],i))  {  flag[y+wayr[i]][c+wayc[i]]=1;  cnt++; dfs(y+wayr[i],c+wayc[i]);  } 
}  
int main()  
{  scanf("%d%d",&m,&n);  for(int l=0;l<m;l++) for(int j=0;j<n;j++)  {  scanf("%d",&map[l][j]);  if(!(map[l][j]&1)) q[l][j][0]=1; if(!(map[l][j]&2)) q[l][j][1]=1;  if(!(map[l][j]&4)) q[l][j][2]=1;  if(!(map[l][j]&8)) q[l][j][3]=1;  }  for(int l=0;l<m;l++) for(int j=0;j<n;j++)  if(!flag[l][j]) {  flag[l][j]=1; cnt=1;dfs(l,j);  if(cnt>bigest) bigest=cnt; ans++; }  printf("%d\n%d\n",ans,bigest);
}