蓝桥杯-方格分割

蓝桥杯-方格分割

解题思路:以中心点坐标为起始点深度优先搜索,沿四个方向走,每走一步,将以中心点对称的坐标标记为已访问,因为旋转对称的属于同一种方法,因此最终的结果要除以4.


#include<iostream>
using namespace std;
int ans;
int next[4][2]={0,-1,-1,0,0,1,1,0};
int a[7][7],vst[7][7];
void dfs(int x,int y)
{
    int tx,ty; 
    if(x == 0 || x == 6 || y == 0 || y== 6 )
    {
        ans++;
        return ; 
    }
    vst[x][y] = 1;
    vst[6-x][6-y] = 1;
    for(int i=0;i<4;i++)
    {
     tx = x + next[i][0];
     ty = y + next[i][1];
    if(tx < 0 || tx > 6 || ty <0 || ty >6)
    continue;
    if(!vst[tx][ty])
    dfs(tx,ty); 
    }
    vst[x][y] = 0;
    vst[6-x][6-y] = 0;
}
int main()
{
    dfs(3,3);
    cout<<ans/4<<endl;
    return 0;
}

猜你喜欢

转载自blog.51cto.com/14472348/2468530
今日推荐