RED AND BLACK DFS和BFS

RED AND BLACK

1.题目:
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

2.思考:
红与黑 咋一看想起了司汤达······想要看书了。)
在这里插入图片描述
题目简而言之就是,地上铺满了黑的和红的瓷砖,我们只能走黑的瓷砖(红色果然是危险的颜色),求我们能够在所给范围内走多少个黑瓷砖。

dfs:
从起点出发,开始搜索,碰到边界、红砖、已走过的砖就撤退返回,可以走就记录数目加1,不断递归直到能走的都走完。这个题目在dfs中没有固定明确的终止条件,走不了了就终止返回了。(emmmm也没有很多知识点)

bfs:
dfs是一条路走到黑,那么bfs就是多路齐发,根据层数,逐层多条路一起走,能走的就砖块加1,不能走也相应的返回。因为是通过队列实现的,所以要注意放入队列元素和及时删除已经搜索过得元素。

**注意:**有时候会忘记标记起点,还有忘记总砖数加上起点,多组数据忘记把总砖数重置,标记重置。

在这里插入图片描述

3.代码实现
dfs:

#include<bits/stdc++.h>
using namespace std;

const int MAXN=25;
char g[MAXN][MAXN];//存图 
bool vis[MAXN][MAXN];//标记是否走过 
int sum=1;//总数
int xd[4]={-1,1,0,0};//四个方向的相应坐标变化 
int yd[4]={0,0,-1,1}; 
int W,H;//图的宽和长 
void dfs(int,int);

int main(){
	while(scanf("%d%d",&W,&H)&&W&&H){//输入图的大小
		int x,y;
		for(int i=0;i<H;i++){//读取图
			cin>>g[i];
		}
		for(int j=0;j<W;j++){//找到起点的坐标
			for(int i=0;i<H;i++){
				if(g[i][j]=='@'){
					x=i;
					y=j;
					break;
				}
			}
		}
		sum=1;
		memset(vis,0,sizeof vis);
		vis[x][y] = 1;//记得把起点也标记已走喔,要不然结果会多一
		dfs(x,y);//开始搜索
		cout<<sum<<endl;//输出结果
	}
	return 0;
} 

void dfs(int x,int y){
	int xx,yy;
	for(int i=0;i<4;i++){//改变坐标
		xx=x+xd[i];
		yy=y+yd[i];
		if(xx<0||xx>=H||yy<0||yy>=W)//判断是否能走
		continue;
		if(g[xx][yy]=='#')
		continue;
		if(vis[xx][yy]==true)
		continue;
		
		vis[xx][yy]=true;//标记已走
		sum++;//砖块加1
		dfs(xx,yy);//又开始搜索下一个
	}
}

bfs:

#include<bits/stdc++.h>
using namespace std;

const int MAXN=25;
char g[MAXN][MAXN];//存图 
bool vis[MAXN][MAXN];//标记是否走过 
int sum=1;//总数
int xd[4]={-1,1,0,0};//改变 
int yd[4]={0,0,-1,1}; 
int W,H;//宽和长
int x,y; //起点坐标
void bfs();
struct node{//储存节点坐标
	int x1,y1;
};

int main(){
	while(scanf("%d%d",&W,&H)&&W&&H){//输入图的大小
		for(int i=0;i<H;i++){//读取图
			cin>>g[i];
		}
		for(int j=0;j<W;j++){//找到起点坐标
			for(int i=0;i<H;i++){
				if(g[i][j]=='@'){
					x=i;
					y=j;
					break;
				}
			}
		}
		sum=1;//起点的第一块砖
		memset(vis,0,sizeof vis);//初始化标记
		vis[x][y] = 1;//标记起点
		bfs();//搜索
		cout<<sum<<endl;
	}
	return 0;
} 

void bfs(){
	queue<node>q;//储存坐标 
	q.push({x,y});//从起点开始搜索
	while(q.size()){//终止条件为没有层数可搜素,即已经搜索到最后一层
	node t=q.front();//获得查找节点
	q.pop();//删除该节点	
	int xx,yy;
	for(int i=0;i<4;i++){//坐标变化
		xx=t.x1+xd[i];
		yy=t.y1+yd[i]; 
		if(xx<0||xx>=H||yy<0||yy>=W)//判断是否能走
		continue;
		if(g[xx][yy]=='#')
		continue;
		if(vis[xx][yy]==true)
		continue;
		
		vis[xx][yy]=true;//能走,标记
		sum++;//砖数加1
		q.push({xx,yy});//放入该节点,进行下一次查找
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值