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.
入门的DFS,预处理有个技巧,不用一个个情况列举。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
struct Node{
bool vis;
int E, W, N, S;
int num;
};
int H, W, cnt, ans = 0, numb = 0;
Node a[53][53];
void Predo()
{
for(int i = 1; i <= H; i++)
for(int j = 1; j <= W; j++) {
if(a[i][j].num >= 8) a[i][j].num -= 8, a[i][j].S ++;
if(a[i][j].num >= 4) a[i][j].num -= 4, a[i][j].E ++;
if(a[i][j].num >= 2) a[i][j].num -= 2, a[i][j].N ++;
if(a[i][j].num >= 1) a[i][j].num -= 1, a[i][j].W ++;
a[i][j].vis = false;
}
}
int DFS(int x, int y)
{
cnt ++;
a[x][y].vis = true;
if(!a[x][y].N && !a[x-1][y].vis) DFS(x-1, y);
if(!a[x][y].S && !a[x+1][y].vis) DFS(x+1, y);
if(!a[x][y].W && !a[x][y-1].vis) DFS(x, y-1);
if(!a[x][y].E && !a[x][y+1].vis) DFS(x, y+1);
return 0;
}
int main()
{
scanf("%d%d", &H, &W);
for(int i = 1; i <= H; i++)
for(int j = 1; j <= W; j++)
scanf("%d", &a[i][j].num);
Predo();
for(int i = 1; i <= H; i++)
for(int j = 1; j <= W; j++)
if(!a[i][j].vis)
{
cnt = 0;
DFS(i, j);
ans = max(ans, cnt);
numb ++;
}
printf("%d\n%d", numb, ans);
return 0;
}