标签:des style http color io os ar for 2014

1 5 4 0 1 0 0 1 0 1 0 1 0 2 1 1 0 0 0 3 1 0 0 1 1 1 0 1 0 1 0 1 0
4
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
int mp[55][55],ans,n,m,sum;
map<double,bool>db;//用来根据斜率判重
void dfs(int dep,int remain)
{
if(dep>=ans) return;
if(!remain)
{
ans=min(ans,dep);
return;
}
int i,j,p,q,cnt;
bool flag=0;
for(i=0;i<n && !flag;i++)
{
for(j=0;j<m && !flag;j++)
{
if(mp[i][j])
{
flag=1;//找到一个点即可,我们要枚举的是经过它的可行直线
if(i==0)//经过改点的竖直直线
{
for(p=i;p<n;p++) if(!mp[p][j]) break;
if(p==n)
{
for(p=i;p<n;p++) mp[p][j]--;
dfs(dep+1,remain-n);
for(p=i;p<n;p++) mp[p][j]++;
}
}
if(j==0)//经过改点的水平直线
{
for(p=j;p<m;p++) if(!mp[i][p]) break;
if(p==m)
{
for(p=j;p<m;p++) mp[i][p]--;
dfs(dep+1,remain-m);
for(p=j;p<m;p++) mp[i][p]++;
}
}
db.clear();//清空状态
for(p=i+1;p<n;p++)
{
for(q=0;q<m;q++)
{
if(mp[p][q] && p!=i && q!=j)
{
int x=p-i,y=q-j;
if(!db[double(y)/x]) db[double(y)/x]=1;//该斜率直线是否已经找过
else continue;
cnt=1;
while(i+cnt*x>=0 && i+cnt*x<n && j+cnt*y>=0 && j+cnt*y<m && mp[i+cnt*x][j+cnt*y]) cnt++;
if(i+cnt*x>=0 && i+cnt*x<n && j+cnt*y>=0 && j+cnt*y<m) continue;//如果后面的不能连续
if(i-x>=0 && i-x<n && j-y>=0 && j-y<m) continue;//如果前面的不能连续
if(cnt<3) continue;//经过的点少于3个
cnt=1;
while(i+cnt*x>=0 && i+cnt*x<n && j+cnt*y>=0 && j+cnt*y<m)
{
mp[i+cnt*x][j+cnt*y]--;
cnt++;
}
mp[i][j]--;
dfs(dep+1,remain-cnt);
mp[i][j]++;
cnt=1;
while(i+cnt*x>=0 && i+cnt*x<n && j+cnt*y>=0 && j+cnt*y<m)
{
mp[i+cnt*x][j+cnt*y]++;
cnt++;
}
}
}
}
}
}
}
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
n++;
m++;
sum=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&mp[i][j]);
sum+=mp[i][j];
}
}
ans=min(14,sum/3);
dfs(0,sum);
printf("%d\n",ans);
}
}标签:des style http color io os ar for 2014
原文地址:http://blog.csdn.net/faithdmc/article/details/39437721