DFS遍历模板 --- 图上方格四周处理(三消算法等等)

#include<bits/stdc++.h>
//挑战程序设计竞赛   
using namespace std;


int N , M;
vector<vector<char> > grid;
vector<vector<bool> > can_goal;    
struct direct{
	int x, y;
}dire[4] ={{-1,0},{0,1},{0,-1},{1,0}};


void dfs(int x, int y) 
{
        //访问该点
        can_goal[x][y] = true;

	//对该点进行处理 
	
	
	int nx = 0 , ny = 0; 
	//遍历当前点的四周四个点 ,不包括斜对线上的四个点,如果具体逻辑需要遍历斜对线上的四个点,则改dire即可 
	for(int i = 0 ; i < 4; ++i)
	{
		 nx = x + dire[i].x , ny = y + dire[i].y;
                //具体判断条件,前五个必须有,其他的可根据实际情况再添加
		if(0 <= nx && nx < N && 0 <= ny && ny < M && !can_goal[nx][ny])
		{
			dfs(nx,ny);
		}
		else
		{
			continue;
		}
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值