poj 1111

3 篇文章 0 订阅
/*************************************************************************
     File Name: 1111forbfs.cpp
     Author: yubo
     Mail: yuzibode@126.com 
     Created Time: 2014年06月15日 星期日 18时56分21秒
     学习重点:
	 别人都是通过这道题破的处,而我,唉,还是那么纯洁,看来自己写的代码真的有必要重新审视一番,否则死都不知怎么死的!
	 这道题就是
 ************************************************************************/

#include<cstring>
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
int dir[8][2]={
	0,-1,-1,0,1,0,0,1,-1,-1,1,-1,-1,1,1,1,
};
int row,col,x,y;
typedef struct {
	int x2,y2;
}node;
int vis[25][25];
char map[25][25];
int total;
int judge(int i,int j)
{
	if(i>=1&&i<=row&&j>=1&&j<=col)
		return 1;
	else
		return 0;
}
void bfs(int x,int y)
{
	queue<node> s;
	node t1,t2,t3;
	vis[x][y]=1;
	t1.x2=x;
	t1.y2=y;
	s.push(t1);
	while(!s.empty()){
		t2=s.front();
		s.pop();
	//	printf("%d %d \n",t2.x2,t2.y2);
		for(int i=0;i<4;i++)
		{
			 t3.x2=t2.x2+dir[i][0];
			 t3.y2=t2.y2+dir[i][1];
			if(judge(t3.x2,t3.y2))//如果在所示的图内
			{
				if(map[t3.x2][t3.y2]=='.')
					total++;
				else
					if(map[t3.x2][t3.y2]=='X'&&!vis[t3.x2][t3.y2])
					{
						s.push(t3);
						vis[t3.x2][t3.y2]=1;
					}

			}
			else//???
				total++;
		}
		for(int i=4;i<8;i++)
		{
			 t3.x2=t2.x2+dir[i][0];
			 t3.y2=t2.y2+dir[i][1];
			if(judge(t3.x2,t3.y2))
			{
				if(map[t3.x2][t3.y2]=='X'&&!vis[t3.x2][t3.y2])
				{
					s.push(t3);
					vis[t3.x2][t3.y2]=1;
				}
			}


		}
	}
}
int main()
{
	freopen("in.txt","r",stdin);
	while(scanf("%d%d%d%d",&row,&col,&x,&y)){
		if(row==0||col==0||x==0||y==0)
			break;
		total=0;
		memset(vis,0,sizeof(vis));
		memset(map,'.',sizeof(map));
		for(int i=1;i<=row;i++)
			for(int j=1;j<=col;j++)
				cin>>map[i][j];
         bfs(x,y);
		 printf("%d\n",total);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值