Grid division-DFS

My level is limited and I am only for learning. If I find any mistakes, I would like to thank you for pointing out.
Insert picture description here
Idea: Do not search for grid points, otherwise it is very troublesome, just search for the dividing line (the line to be cut), the left and right are the same, the top and bottom are the same, so divide by 4.

#include<bits/stdc++.h>
using namespace std;
//思路:不要搜索格点,不然很麻烦,搜索分隔线即可,左右相同,上下相同,故除4
int xx[4]={
    
    -1,1,0,0},yy[4]={
    
    0,0,-1,1},vis[7][7],res;
void dfs(int x,int y)
{
    
    
	if(x<=0||x>=6||y<=0||y>=6){
    
    res++;return;}
	for(int i=0; i<4; i++)
	{
    
    
		int tx = x+xx[i];
		int ty = y+yy[i];
		if(!vis[tx][ty] && !vis[6-tx][6-ty])
		{
    
    
			vis[tx][ty]=1;
			vis[6-tx][6-ty]=1;
			dfs(tx,ty);
			vis[tx][ty]=0;//recall
			vis[6-tx][6-ty]=0;//recall
		}
	}
}

int main()
{
    
    
	vis[3][3]=1;
	dfs(3,3);
	res/=4;
    return cout<<res<<endl,0;
}

Guess you like

Origin blog.csdn.net/weixin_43615816/article/details/114695338