pku 1164 -- The Castle (递归)
2011-03-02 17:15
381 查看
1164 -- The Castle (递归)
The CastleTime Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4361 Accepted: 2460
Description
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
解题:
先构图,后求值
The CastleTime Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4361 Accepted: 2460
Description
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
解题:
先构图,后求值
#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); }
相关文章推荐
- POJ 1164 The Castle
- The Castle POJ - 1164
- poj 1164 The Castle dp区域计数水题
- poj 1164:The Castle
- BZOJ 2258 pku2758 Checking the Text 文本校对 Splay+Hash
- 城堡问题(The Castle)
- PKU/POJ 3275 Ranking the Cows
- Pku acm 2388 Who's in the Middle 排序算法解题报告(二)----计数排序
- PKU2387-Til the Cows Come Home(SPFA+邻接表)
- Pku acm 1274 The Perfect Stall 数据结构题目解题报告(十三)---- 匈牙利算法求二分图的最大匹配
- pku 3587 The Biggest Cake
- 【树状数组】PKU-2057-The Lost House
- 基于深度优先搜索的回溯算法(递归剪枝及奇偶性剪枝好题!):HDOJ 1010 - Tempter of the Bone
- pku 1743 Musical Theme 最长重复不重叠子串 后缀数组
- PKU_ACM_3650_The Seven Percent Solution
- pku2886 Who Gets the Most Candies?
- pku2761 Feed the dogs
- UVa 699 The Falling Leaves(递归建树)
- pku 1634 Who's the boss? stl排序+二分查找
- BZOJ1954: Pku3764 The xor-longest Path