按14楼的思路,写了一个代码,大家可以试一下:
#include "stdio.h"
int a[3][6]={{8,4,5,6,4,4},{7,3,4,3,3,3},{3,2,2,1,1,2}};
int b[3][6]={{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0}};
int d[6][6]={{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0}};
int r=3,c=6,bj=0;
/*以上是数据,为了调试方便,直接赋值,有兴趣的朋友,可以补充一下。*/
void f1(int i,int j){
int k;
b[i][j]=1;
for(k=j;k<c-1;k++)
if(a[i][k]>a[i][k+1]&&b[i][k+1]==0)f1(i,k+1);
else break;
for(k=i;k<r-1;k++)
if(a[k][j]>a[k+1][j]&&b[k+1][j]==0)f1(k+1,j);
else break;
for(k=j;k>0;k--)
if(a[i][k]>a[i][k-1]&&b[i][k-1]==0)f1(i,k-1);
else break;
for(k=i;k>0;k--)
if(a[k][j]>a[k-1][j]&&b[k-1][j]==0)f1(k-1,j);
else break;
}
void f2(int k,int n){
int i,j,m;
for(i=k;i<c&&bj==0;i++){
for(j=0;j<c;j++)b[r-1][j]=b[r-1][j]+d[i][j];
if(n>1)f2(i+1,n-1);
else{
for(j=0;j<c;j++)if(b[r-1][j]==0)break;
if(j==c)bj=1;
}
for(j=0;j<c;j++)b[r-1][j]=b[r-1][j]-d[i][j];
}
}
void f5(){
int i,j,k;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
b[i][j]=0;
}
void main(){
int i,j,m=0;
clrscr();
for(j=0;j<c;j++){
f1(0,j);
for(i=0;i<c;i++)d[j][i]=b[r-1][i];
f5();
}
for(j=1;j<=c;j++){
f2(0,j);
if(bj==1){
printf("%d\n%d",1,j);
break;
}
}
if(j>c){
for(i=0;i<c;i++){
b[r-1][i]=0;
for(j=0;j<c;j++)
b[r-1][i]=b[r-1][i]+d[j][i];
if(b[r-1][i]==0)m=m+1;
}
printf("%d \n%d",0,m);
}
getch();
}
[
本帖最后由 netlin 于 2012-8-19 10:02 编辑 ]