在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<s id="cmphk"><label id="cmphk"></label></s>
    <span id="cmphk"><var id="cmphk"></var></span>
    <dfn id="cmphk"><var id="cmphk"></var></dfn>
    <menu id="cmphk"><thead id="cmphk"></thead></menu>

    <address id="cmphk"></address>

      <dfn id="cmphk"></dfn>
      
      
      <span id="cmphk"></span>

      <object id="cmphk"><tt id="cmphk"></tt></object>
      1. 新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 馬踏棋盤的實(shí)現(xiàn)

        馬踏棋盤的實(shí)現(xiàn)

        作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
        馬踏棋盤是經(jīng)典的程序設(shè)計(jì)問題之一,主要的解決方案有兩種:一種是基于深度優(yōu)先搜索的方法,另一種是基于貪婪算法的方法。第一種基于深度優(yōu)先搜索的方法是比較常用的算法,深度優(yōu)先搜索算法也是數(shù)據(jù)結(jié)構(gòu)中的經(jīng)典算法之一,主要是采用遞歸的思想,一級(jí)一級(jí)的尋找,最后找到合適的解。而基于貪婪的算法則是依據(jù)貪婪算法的思想設(shè)置一種標(biāo)準(zhǔn),然后依據(jù)標(biāo)準(zhǔn)進(jìn)行選擇,從而得到解,但是他不一定能夠得到最優(yōu)解。


        關(guān)于馬踏棋盤的基本過程:國際象棋的棋盤為8*8的方格棋盤。現(xiàn)將"馬"放在任意指定的方格中,按照"馬"走棋的規(guī)則將"馬"進(jìn)行移動(dòng)。要求每個(gè)方格只能進(jìn)入一次,最終使得"馬"走遍棋盤的64個(gè)方格。

        深度優(yōu)先搜索屬于圖算法的一種,英文縮寫為DFS即Depth First Search.其過程簡要來說是對(duì)每一個(gè)可能的分支路徑深入到不能再深入為止,而且每個(gè)節(jié)點(diǎn)只能訪問一次. (來自百度)

        本文引用地址:http://www.biyoush.com/article/201612/324536.htm

        貪心算法(又稱貪婪算法)是指,在對(duì)問題求解時(shí),總是做出在當(dāng)前看來是最好的選擇。也就是說,不從整體最優(yōu)上加以考慮,他所做出的僅是在某種意義上的局部最優(yōu)解。貪心算法不是對(duì)所有問題都能得到整體最優(yōu)解,但對(duì)范圍相當(dāng)廣泛的許多問題他能產(chǎn)生整體最優(yōu)解或者是整體最優(yōu)解的近似解。(來自百度)

        其中基于深度優(yōu)先搜索的算法就是依據(jù)當(dāng)前點(diǎn)找到下一個(gè)可能的點(diǎn),然后對(duì)這個(gè)點(diǎn)進(jìn)行深度優(yōu)先搜索,然后依次遞歸,當(dāng)出現(xiàn)條件不滿足時(shí),退回來,采用其他的路勁進(jìn)行搜索,最后肯定能夠得到對(duì)應(yīng)的結(jié)果。實(shí)現(xiàn)的基本過程如下:

        /*deepsearch to solve the horse chessproblem*/
        #include
        #include
        #include
        #define ROWS 8
        #define COLS 8

        int chess[ROWS][COLS];

        /*eight directions of x moved*/
        const int x_move[] = {-2,-1,1,2,2,1,-1,-2};
        /*eight directions of y moved*/
        const int y_move[] = {1,2,2,1,-1,-2,-2,-1};

        void print_matrix()
        {
        int i = 0,j = 0;
        for (i = 0; i < ROWS; ++ i)
        {
        for (j = 0; j < COLS; ++ j)
        {
        printf("%d ",chess[i][j]);
        }
        printf("");
        }
        }

        /*find the next point*/
        intnextxy(int *x,int *y,int count)
        {
        if(count > 7 && count < 0)
        return -1;
        switch(count)
        {
        case 0:
        /*check the conditions*/
        if(*x + x_move[0] < ROWS &&
        *x + x_move[0]>= 0 &&
        *y + y_move[0]< COLS &&
        *y + y_move[0]>= 0 &&
        chess[*x + x_move[0]][*y + y_move[0]] == 0)
        {
        *x += x_move[0];
        *y += y_move[0];
        break;
        }
        else/*failed*/
        return 0;
        case 1:
        if(*x + x_move[1] < ROWS &&
        *x + x_move[1]>= 0 &&
        *y + y_move[1]< COLS &&
        *y + y_move[1]>= 0 &&
        chess[*x + x_move[1]][*y + y_move[1]] == 0)
        {
        *x += x_move[1];
        *y += y_move[1];
        break;
        }
        else
        return 0;
        case 2:
        if(*x + x_move[2] < ROWS &&
        *x + x_move[2]>= 0 &&
        *y + y_move[2]< COLS &&
        *y + y_move[2]>= 0 &&
        chess[*x + x_move[2]][*y + y_move[2]] == 0)
        {
        *x += x_move[2];
        *y += y_move[2];
        break;
        }
        else
        return 0;
        case 3:
        if(*x + x_move[3] < ROWS &&
        *x + x_move[3]>= 0 &&
        *y + y_move[3]< COLS &&
        *y + y_move[3]>= 0 &&
        chess[*x + x_move[3]][*y + y_move[3]] == 0)
        {
        *x += x_move[3];
        *y += y_move[3];
        break;
        }
        else
        return 0;
        case 4:
        if(*x + x_move[4] < ROWS &&
        *x + x_move[4]>= 0 &&
        *y + y_move[4]< COLS &&
        *y + y_move[4]>= 0 &&
        chess[*x + x_move[4]][*y + y_move[4]] == 0)
        {
        *x += x_move[4];
        *y += y_move[4];
        break;
        }
        else
        return 0;
        case 5:
        if(*x + x_move[5] < ROWS &&
        *x + x_move[5]>= 0 &&
        *y + y_move[5]< COLS &&
        *y + y_move[5]>= 0 &&
        chess[*x + x_move[5]][*y + y_move[5]] == 0)
        {
        *x += x_move[5];
        *y += y_move[5];
        break;
        }
        else
        return 0;
        case 6:
        if(*x + x_move[6] < ROWS &&
        *x + x_move[6]>= 0 &&
        *y + y_move[6]< COLS &&
        *y + y_move[6]>= 0 &&
        chess[*x + x_move[6]][*y + y_move[6]] == 0)
        {
        *x += x_move[6];
        *y += y_move[6];
        break;
        }
        else
        return 0;
        case 7:
        if(*x + x_move[7] < ROWS &&
        *x + x_move[7]>= 0 &&
        *y + y_move[7]< COLS &&
        *y + y_move[7]>= 0 &&
        chess[*x + x_move[7]][*y + y_move[7]] == 0)
        {
        *x += x_move[7];
        *y += y_move[7];
        break;
        }
        else
        return 0;
        default:
        return 0;
        }
        return 1;
        }
        int deepsearch(int x,int y, int j)
        {
        /*save the value x,y*/
        int x1 = x, y1 = y;
        int tag = 0, i = 0;
        /*save j on chess[x][y]*/
        chess[x][y] = j;

        /*recursion exit condition*/
        if(j == COLS*ROWS)
        {
        return 1;
        }
        /*find the next point in eight directions*/
        tag = nextxy(&x1,&y1,i);
        /*find the nextx,nexty */
        while (tag == 0 && i < 7)
        {
        i ++;
        tag =nextxy(&x1,&y1, i);
        }

        /*the nextxy be found*/
        while(tag)
        {
        if(deepsearch(x1,y1,j+1))
        return 1;

        /*if failed, a new finding process */
        x1 = x; y1 = y;
        i ++;
        tag = nextxy(&x1,&y1,i);
        while (tag == 0 && i < 7)
        {
        i ++;
        tag = nextxy(&x1,&y1,i);
        }
        }
        /*no direction can findnextpoint*/
        if(tag == 0)
        chess[x][y] = 0;
        return 0;
        }

        void main()
        {
        clock_t start = clock();
        deepsearch(2,0,1);
        print_matrix();
        int seconds = (clock()-start)/CLOCKS_PER_SEC;

        printf("%d",seconds);
        return;
        }


        上一頁 1 2 下一頁

        評(píng)論


        技術(shù)專區(qū)

        關(guān)閉
        ×

        “芯”朋友见面大会
        珠海|11.14|泰克“芯”朋友见面大会珠海站|泰克带您从测试角度看半导体的整条产业链,快来报名抢位吧>>