您的位置:首页 > 其它

zoj 1047

2012-05-02 22:06 267 查看
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;

char map[25][25];
bool vis[25][25];
int n, m, a, b;
int len;
int dir[4][2] = {-1,0, 0,1, 1,0, 0,-1};
int dir_x[4][2] = {-1,-1, -1,1, 1,1, 1,-1};
void init(int n, int m)
{
memset(vis, false,sizeof(vis));
memset(map,'.',sizeof(map));
len = 0;
char str[25];
for(int i=1; i<=n; i++)
{
scanf("%s",str);
for(int j=1; j<=m; j++)
map[i][j] = str[j-1];
}
}
void dfs(int x, int y)
{
vis[x][y] = true;
for(int i=0; i<4; i++)
{
int x0 = x+dir[i][0];
int y0 = y+dir[i][1];
if(!vis[x0][y0]&&map[x0][y0]=='X')
dfs(x0,y0);
else if(!vis[x0][y0]&&map[x0][y0]=='.')
len++; //dfs(x,y,len+1);
}
for(int i=0; i<4; i++)
{
int x0 = x + dir_x[i][0];
int y0 = y + dir_x[i][1];
if(!vis[x0][y0]&&map[x0][y0]=='X')
dfs(x0,y0);
}
}

int main()
{

while(scanf("%d",&n)&&n)
{
scanf("%d%d%d",&m, &a, &b);
init(n,m);
dfs(a,b);
printf("%d\n",len);
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: