类似poj 2383的八连通,然而这只是四个方向移动,还是很简单的.....不过这题还让我WA一次,基本功不扎实,尽然行号和列号混淆了。
/*
* =====================================================================================
*
* Filename: poj_1979.cpp
*
* Description: poj 1979 red and black
*
* Version: 1.0
* Created: 03/29/2015 01:39:09 AM
* Revision: none
* Compiler: gcc
*
* Author: Kart, 786900722@qq.com
* Blog: http://blog.youkuaiyun.com/linux_kernel_fan/
*
* =====================================================================================
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<utility>
#define MAX 20
using namespace std;
int W, H;
char mat[MAX+1][MAX+1];
int sx, sy;
pair<int, int> dir[4] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int count;
bool in_range(int x, int y)
{
if(x >= 0 && x < H && y >= 0 && y < W)
return true;
else
return false;
}
void solve(int x, int y)
{
mat[x][y] = '#';
for(int i = 0; i < 4; i++)
{
int tx = dir[i].first + x;
int ty = dir[i].second + y;
if(mat[tx][ty] == '.' && in_range(tx, ty))
{
count ++;
solve(tx, ty);
}
}
return ;
}
int main()
{
scanf("%d%d", &W, &H);
while(W && H)
{
count = 0;
for(int i =0; i < H; i++)
{
scanf("%s", mat[i]);
for(int j = 0; j < W; j++)
{
if(mat[i][j] == '@')
{
sx = i;
sy = j;
}
}
}
solve(sx, sy);
printf("%d\n", count + 1);
scanf("%d%d", &W, &H);
}
return 0;
}