http://poj.org/problem?id=1164
提交的时候正遇oj挂了,害我白白错了几次,却不知情。。。
#include "iostream"
#include "stdio.h"
#include "string.h"
using namespace std;
struct room
{
bool vis;
int val;
}castle[51][51];
int area;
void dfs(int x, int y)
{
if(!castle[x][y].vis)
{
area++;
castle[x][y].vis = true;
if(castle[x][y].val - 8 < 0) //若向南无墙
dfs(x + 1, y);
else
castle[x][y].val -= 8;
if(castle[x][y].val - 4 < 0) //若向东无墙
dfs(x, y + 1);
else
castle[x][y].val -= 4;
if(castle[x][y].val - 2 < 0) //若向北无墙
dfs(x - 1, y);
else
castle[x][y].val -= 2;
if(castle[x][y].val - 1 < 0) //若向西无墙
dfs(x, y - 1);
else
castle[x][y].val -= 1;
}
}
int main()
{
int R, C, i, j;
while(scanf("%d%d", &R, &C) != EOF)
{
for(i = 1; i <= R; i++)
for(j = 1; j <= C; j++)
{
scanf("%d", &castle[i][j].val);
castle[i][j].vis = false;
}
int nRooms = 0, maxArea =0;
for(i = 1; i <= R; i++)
for(j = 1; j <= C; j++)
{
area = 0;
dfs(i, j);
if(area > 0) nRooms++;
if(area > maxArea) maxArea = area;
}
cout << nRooms << endl << maxArea << endl;
}
return 0;
}