黑红砖块,POJ-1979,BFS

题目的

".'' - 黑色瓷砖
'#' - 红色瓷砖
'@' - 黑色瓷砖上的男人(在数据集中只显示一次)

 

这间长方形客房铺有方形瓷砖。每个瓷砖都是红色或黑色。一个男人站在黑色的瓷砖上。从瓷砖中,他可以移动到四个相邻瓷砖中的一个。但是他不能在红色瓷砖上移动,他只能在黑色瓷砖上移动。

编写一个程序,通过重复上述动作来计算他可以达到的黑色瓷砖的数量。

就是计算他能 到达“.”的数量,包括最开始他站的点;

#include <iostream>
#include<stdio.h>
#include<string.h> 
#include<queue>
using namespace std;
int m,n;
int sx,sy;

char map[25][25];
int vis[25][25];
int to[4][2]={
  
  {1,0},{-1,0},{0,1},{0,-1}};
struct node{
	int x,y;
}now,next;
void bfs(int xx,int yy){
	queue<node> q;
	int ans=0;
	now.x=xx;
	now.y=yy;
	q.push(now);
	while(!q.empty()){
		now=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			next.x=now.x+to[i][0];
			next.y=now.y+to[i][1];
			if(next.x>=1&&next.y>=1&&next.x<=m&&next.y<=n&&!vis[next.x][next.y]&&map[next.x][next.y]=='.')
			{
				vis[next.x][next.y]=1;
				ans++;
				q.push(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值