注册 登录
编程论坛 C++教室

求高手解答!非常感谢!

yqb139130 发布于 2011-05-31 14:42, 750 次点击
  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  1  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  1  0  0
  0  0  0  0  0  0  0  0  0  0  1  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  1  0  0  0
  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  1  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  1  0  0  0  0  0  0  0
  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0
  0  0  1  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
16*16的矩阵,其中4,8,12,16行都是0,其他行只有一个1,一共12个1.
要求每行每列有且只有一个1(除了4,8,12,16行、列必须为0)。输出所有不同的矩阵。
//同学说可以建一个list,1-16个,将矩阵中1的位置坐标放在list中,分为行号和列号。 我还是不会做,
求高人解答,万分感谢!
16 回复
#2
laigaoat20052011-05-31 20:41
输出所有不同的矩阵。
输出所有不同的矩阵?结果那么多,输出了怎么能看完?不用看完,只求算法?
#3
yqb1391302011-05-31 22:16
没有这么多,每行每列只能有一个1,12*12,一共144种!每个1的行号和列号都可以从1-16变化,其中不包括4,8,12,16,所以每个1的行号和列号各有12种!,组合一共有144种!求高人帮忙编程指点啊!
每行每列只能有一个1!!!!!!
#4
laigaoat20052011-05-31 22:24
晕了,题看错了……  只看到了每行只能有个1,没有看到每行每列只有一个1……
每列啊,哎。这么简单的题让人郁闷半天。还是给个程序出来吧。
程序代码:
#include <stdio.h>
int main(void)
{
    for(int i=0;i<15;i++)
    {      
        for (int j=0;j<15;j++)
            if( i!=j || 3==i || 7==i || 11==i || 15==i ) printf("0");
            else printf("1");
    printf("\n");
    }
    return 0;
}

 
#5
laigaoat20052011-05-31 22:30
你求高手解题,我以为好难,没有注意看 每列都只能有个1,以为会有好多结果哦……真郁闷!
#6
laigaoat20052011-05-31 22:35
急人,怎么还是不对。还没有偏移
#7
yqb1391302011-05-31 22:47
呵呵,你看清楚了,没有那么多情况的,我限定条件了
#8
laigaoat20052011-05-31 23:04
这下应该对了。哎,没有分也没有关系。当做一个练习题。
程序代码:
#include <stdio.h>

void putout(int cnt=1)
{
    for(int count=1, i=1+cnt;count<=16;  count++,i++)
    {   
        if(16==i) i=1;   
        for (int j=1;j<16;j++)
            if( i!=j || 4==count || 8==count || 12==count || 16==count ) printf("0");
            else printf("1");
    printf("\n");
    }   
    printf("\n\n\n");
}

int main(void)
{   
    for (int cnt=0;cnt<15;cnt++)
    {
        putout(cnt);
    }

    return 0;
}

#9
laigaoat20052011-05-31 23:13
哎,还是不对。还是没有打全。真难。收回我上面说简单的话。我暂时做不出来了。能力不够。盼高手。
#10
laigaoat20052011-05-31 23:21
再进化一次:
可能还是不全,但,我觉得这种算法只能算到这这里了。没有学过算法,思维不够。
程序代码:
#include <stdio.h>

void putout(int cnt=1)
{
    for(int count=1, i=1+cnt;count<=16;  count++,i++)
    {   
        if(16==i) i=1;   
        for (int j=1;j<16;j++)
            if( i!=j || 4==count || 8==count || 12==count || 16==count ) printf("0");
            else printf("1");
    printf("\n");
    }   
    printf("\n\n\n");
}

void putout_fan(int cnt=1)
{
    for(int count=1, i=1+cnt;count<=16;  count++,i++)
    {   
        if(16==i) i=1;   
        for (int j=16;j>0;j--)
            if( i!=j || 4==count || 8==count || 12==count || 16==count ) printf("0");
            else printf("1");
    printf("\n");
    }   
    printf("\n\n\n");
}

int main(void)
{   
    for (int cnt=0;cnt<15;cnt++)
    {
        putout(cnt);
        putout_fan(cnt);
    }

    return 0;
}

#11
诸葛修勤2011-06-01 01:26
程序代码:
#include <iostream>
using namespace std;

const size_t MAX=6;

unsigned int array[12] =
    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

void _swap(unsigned int &a, unsigned int &b)
{
    unsigned int temp = a;
    a = b;
    b =  temp;
}
void _show_x(const size_t x)
{
    //输出横向
    for (size_t i=1; i<=MAX; ++i)
    {
        if (i==array[x])
        {
            cout << 1 << ' ';
        }
        else
        {
            cout << 0 << ' ';
        }
        if (!(i%3))
        {
            cout << 0 << ' ';
        }
    }

    cout << endl;
}
void _show()
{
    static size_t counter = 1;
    cout << "counter = " << counter << endl;
    counter++;

    for (size_t i=1; i<=MAX; ++i)
    {
        _show_x(i-1);
        if (!(i%3))
        {
            for (size_t j=1; j<=MAX; ++j)
            {
                if (!(j%3))
                {
                    cout << 0 << ' ';
                }
                cout << 0 << ' ';
            }
            cout << endl;
            //cout << "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" << endl;
        }
    }
    cout << endl;
}
void _function(size_t index)
{
    if (index == MAX-1)
    {
        _show();
    }
    else
    {
        for(size_t i=index; i<MAX; ++i)
        {
            _swap(array[index], array[i]);
            _function(index+1);
            _swap(array[index], array[i]);
        }
    }
}

int main(void)
{
    freopen("text.txt", "w", stdout);
    _function(0);

    return 0;
}
#12
诸葛修勤2011-06-01 08:52
输出的结果太多   
可以设置
const size_t MAX=6;

MAX的值来查看

结果保存到了工程文件下的text.txt中

当MAX=3
counter = 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 0

counter = 2
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 0

counter = 3
0 1 0 0
1 0 0 0
0 0 1 0
0 0 0 0

counter = 4
0 1 0 0
0 0 1 0
1 0 0 0
0 0 0 0

counter = 5
0 0 1 0
0 1 0 0
1 0 0 0
0 0 0 0

counter = 6
0 0 1 0
1 0 0 0
0 1 0 0
0 0 0 0
#13
lz10919149992011-06-01 09:31
应该有12!个矩阵。

[ 本帖最后由 lz1091914999 于 2011-6-1 11:21 编辑 ]
#14
laigaoat20052011-06-01 10:12
呵呵,比我聪明多了。。看来真不能小看任何一个题目,更应该仔细读题。
为自己的无知感到羞愧,为自己的眼高手低感到惋惜!
还是主席说得好,好好学习,天天向上。
#15
yqb1391302011-06-01 10:41
谢谢11楼,非常感谢,我正在运行,好像结果是比较多,还没运行出来~
#16
yqb1391302011-06-02 15:35
11楼牛人,很强悍,可惜我没有积分给你,惭愧了!再次非常感谢!
#17
laigaoat20052011-06-03 14:14
    啊,这题没有做出来, 这两天吃不好,睡不好。
    那天,我用循环的方法做好很长时间都没有做出来,后来灵光一闪,用位运算来做应该要简单些,于是,牺牲了两天的所有休息时间,并复习了递归n次,终于有了下面的代码。
    一个人鼓捣了好几天,工作一空就又泡在这代码里,两天都没有学习新东西了。今天终于可以休息一会了!
说来还是羞愧,下面这代码,调试了很时间,有些特殊数据(代码中的if(0==(cnt++/6)%2)语句中的6,不是我推算出来的。而是我在多次测试结果中找出来的一个规律数。为什么要用6?我不知道。)
    算法不好(我不是直接排列出结果,我是排列出所有结果,再过滤出不用的结果),只是算出了结果。不过,自己鼓励自己说,总算是自己写的吧。
   
程序代码:
#include <stdio.h>
#include <iostream>
using namespace std;
const int SIZE(4);         //结果个数为 (SIZE-2)个
int count_motrix=0;//矩阵计数器
int arr[SIZE];
void initia()//初始化数组
{
    for(int cnt=0;cnt<SIZE;cnt++)
    {
        if (0==cnt)        arr[cnt]=1;
        else    arr[cnt]=arr[cnt-1]*2;
    }
    for(int cnt=3;cnt<SIZE;cnt=cnt+4)    arr[cnt]=0;
}

void bina_out_line(int _in_num)
{
    for(int cnt=0;cnt<SIZE;cnt++)
    {
        bool tem_bt = _in_num & (1<<cnt )    ;
        cout << " " <<  tem_bt ;//put 0 or 1
    }
    cout << endl ;  //There is an "endl" at the end of a line.
}

bool test()//过滤
{
    for (int i=0;i<SIZE;i++)
    {
        if ( 0==(i+1)%4 ) //if on 4/8/12...
        {
            if( 0 != arr[i] )    return false;//如果值不为0 不打印
            else continue;        //如果值为0 可能要打印
        }
        if(0==arr[i]) return false;
    }
    return true;
}


inline void swapit(int& a, int& b)
{
        int temp = a;
        a = b;
        b = temp;   
}

void perm(int arr[], int k, int m)
{    static int cnt=0;
    int i;
    if (k == m)
    {
        if(    test() )
        {
            if(0==(cnt++/6)%2)   
            {
                cout << "counter:" << ++count_motrix << endl;
                for (i = 0; i <= m; i++)
                    bina_out_line(    arr[i]    );  //cout << (arr[i])<< ",";  //         
                cout <<    "\n";               
            }      
        }
    }  
    else
        for (i=k; i <= m; i++)
        {
            swapit (arr[k], arr[i]);
            perm (arr, k+1, m);
            swapit (arr[k], arr[i]);                        
        }   
}
int main()
{    freopen("result.txt", "w", stdout);
    initia();
    perm(arr, 0,SIZE-1);   
    return 0;
}   


1