1164 -- The Castle (递归)The CastleTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4361 Accepted: 2460Description
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. InputYour 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.OutputYour program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).Sample Input4711 6 11 6 3 10 67 9 6 13 5 15 51 10 12 7 13 7 513 11 10 8 10 12 13Sample Output59SourceIOI 1994
///
解题:
先构图,后求值
#include <iostream> using namespace std; int N,M; int dir[][2]={{0,-1},{-1,0},{0,1},{1,0}}; int sdir[][2]={{0,-2},{-2,0},{0,2},{2,0}}; int RealMap[110][110]; int SourMap[51][51]; bool Mark[110][110]; void AnalyseMap() //构图 { int i,j; for(i=0;i<N;i++) // 2*(i+1),2*(j+1) { for(j=0;j<M;j++) { int index=0; int p=2*(i+1); int q=2*(j+1); RealMap[p][q]=8; while(index<4) { if(SourMap[i][j]&1) { } else RealMap[p+dir[index][0]][q+dir[index][1]]=1; SourMap[i][j]>>=1; index++; } } } } int Result(int i,int j) //求result { int sum=0; if(Mark[i][j]) return 0; Mark[i][j]=true; if(RealMap[i][j]==8) sum+=1; for(int k=0;k<4;k++) { if(RealMap[i+dir[k][0]][j+dir[k][1]]==1) { RealMap[i+dir[k][0]][j+dir[k][1]]=0; sum+=Result(i+sdir[k][0],j+sdir[k][1]); } } return sum; } int main() { scanf("%d%d",&N,&M); memset(RealMap,0,sizeof(RealMap)); memset(Mark,false,sizeof(Mark)); int i,j; for(i=0;i<N;i++) for(j=0;j<M;j++) scanf("%d",&SourMap[i][j]); int t=3; AnalyseMap(); int ans=0; int room=0; /* 输出测试 for(i=0;i<N*2+2;i++) { for(j=0;j<M*2+2;j++) cout<<RealMap[i][j]; cout<<endl; }*/ for(i=0;i<N;i++) for(j=0;j<M;j++) { int s,d; s=2*(i+1); d=2*(j+1); if(Mark[s][d]) continue; int temp=Result(s,d); if(temp!=0) room++; if(temp>ans) ans=temp; } printf("%d/n%d/n",room,ans); }