思路:二进制枚举所有马的状态,然后遍历所有马,标记其管辖点,最后判断当前方案是否合法
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=10, INF=0x3f3f3f3f;
bool in(int a, int b);//判断越界
bool judge();//判断方案是否合法
void get(int k, int &start, int &end);//得到对应不蹩腿方向所能跳到的位置坐标
int res, cnt_min=INF;//方案数, 数量
bool st[N][N], horse[N][N];//马和管辖数组和马位置数组
int dx[8]={-2, -2, -1, 1, 2, 2, 1, -1};//以左上为起点顺时针
int dy[8]={-1, 1, 2, 2, 1, -1, -2, -2};//马能跳到的 位置
int bx[4]={-1, 0, 1, 0}, by[4]={0, 1, 0, -1};//蹩腿位置
signed main()
{
for(int op=0;op<(1<<25);op++)
{
memset(st, false, sizeof st);
memset(horse, false, sizeof horse);
int cnt=0;
for(int i=0;i<25;i++)
if(op>>i&1)//枚举当前方案, 马的位置
{
st[i/5][i%5]=true;//i/5, i%5对应转化
horse[i/5][i%5]=true;
cnt++;
}
if(cnt>cnt_min) continue;//如果数量已经大于存储的数量,可以跳过
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
if(horse[i][j])//枚举马的位置,有马再标记其所管辖的格子
for(int k=0;k<4;k++)//枚举蹩腿位置
{
int a=i+bx[k], b=j+by[k];
if(!in(a, b)||horse[a][b]) continue;//越界或该方向蹩腿, 则跳过
int start, end;
get(k, start, end);//枚举马在当前方向所能跳到的位置坐标
for(int o=start;o<=end;o++)
{
int sa=i+dx[o], sb=j+dy[o];
if(in(sa, sb)) st[sa][sb]=true;//如果在范围内,则标记
}
}
if(judge())
{
if(cnt<cnt_min) cnt_min=cnt, res=0;//判断成功,并且小于已经存储的值,则更新
res++;
}
}
cout<<cnt_min<<' '<<res<<endl;//9, 90
}
void get(int k, int &start, int &end)
{
if(k==0) start=0, end=1;
if(k==1) start=2, end=3;
if(k==2) start=4, end=5;
if(k==3) start=6, end=7;
}
bool judge()
{
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
if(!st[i][j]) return false;
return true;
}
bool in(int a, int b)
{
if(a<0||a>4||b<0||b>4) return false;
return true;
}