Chinaunix首页 | 论坛 | 博客
  • 博客访问: 58338
  • 博文数量: 10
  • 博客积分: 520
  • 博客等级: 中士
  • 技术积分: 232
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-11 17:00
文章分类

全部博文(10)

文章存档

2010年(1)

2009年(5)

2008年(4)

我的朋友

分类: C/C++

2008-12-17 17:48:11

 

#include <stdio.h>

#define X 10
#define Y 10

int chess[10][10] = {
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};

int curx = 0;
int cury = 0;


void print_chess(void)
{
    int i,j;
    for(i=0; i < 10; i++) {
        for(j=0; j < 10; j++) {
            printf("%d ",chess[i][j]);
        }
        printf("\n");
    }
}

int get_son(int x, int y)
{
    int number = 0;
    if( (x<X-1) && (chess[x+1][y] == 0) )
    {
        number++;
    }
    if( (x>0) && (chess[x-1][y] == 0) )
    {
        number++;
    }
    if( (y<Y-1) && (chess[x][y+1] == 0) )
    {
        number++;
    }
    if( (y>0) && (chess[x][y-1] == 0) )
    {
        number++;
    }
    return number;
}

int get_next(int x, int y)
{
    int min = 100;
    int have_node = -1;
    int temp = 0;
    //pos->上下左右 1,2,3,4

    int pos = 0;
    int len = 100;
    if((x != X-2)&&(x != 1)) {
        if(x >= X >> 1) {
            //下

            len = X - x;
            pos = 2;
        }else{
            //上

            pos = 1;
            len = x;
        }
    }
    if((y != Y-2)&&(y != 1))
    {
        if(y >= Y >> 1) {
            //右

            if((Y-y) < len)
            {
                pos = 4;
            }
        }else{
            //左

            if(y < pos)
            {
                pos = 3;
            }
        }
    }

    if( (x>0) && (chess[x-1][y] == 0) )
    {
        have_node++;
        min = get_son(x-1, y);
        curx = x-1;
        cury = y;
    }
    if( (y>0) && (chess[x][y-1] == 0) )
    {
        have_node++;
        temp = get_son(x, y-1);
        //相等的时候,如果是左边优先策略的时候,往左走。

        if((min > temp) || ((min == temp)&&(pos == 3)))
        {
            min = temp;
            curx = x;
            cury = y-1;
        }
    }
    if( (y<Y-1) && (chess[x][y+1] == 0) )
    {
        have_node++;
        temp = get_son(x, y+1);
        if((min > temp)||((min == temp)&&(pos == 4)))
        {
            min = temp;
            curx = x;
            cury = y+1;
        }
    }

    if( (x<X-1) && (chess[x+1][y] == 0) )
    {
        have_node++;
        temp = get_son(x+1, y);
        if((min > temp) || ((min == temp)&&(pos == 2)))
        {
            min = temp;
            curx = x+1;
            cury = y;
        }
    }
    chess[curx][cury] = 2;
    return have_node;
}


int main(void)
{
    curx = 4;
    cury = 6;
    chess[curx][cury] = 2;
    while(get_next(curx, cury) >= 0)
    {
        print_chess();
    }

    return 0;
}

用的是贪心算法,自己写的,有点乱。

参考资料在:

阅读(504) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:栈的实现(C语言)

给主人留下些什么吧!~~