Chinaunix首页 | 论坛 | 博客
  • 博客访问: 999795
  • 博文数量: 633
  • 博客积分: 30780
  • 博客等级: 大将
  • 技术积分: 7532
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-12 21:07
文章分类

全部博文(633)

文章存档

2011年(10)

2010年(500)

2009年(47)

2008年(76)

我的朋友

分类:

2008-05-30 16:35:08

八个方向的暴力搜索,没啥算法可言

#include <stdio.h>
#include <stdlib.h>

#define N 100
#define LENGTH 100

int get_size()
{
    int n;
    printf("please input the size of squaer:");
    scanf("%d", &n);
    if (n < 1)
        return 0;
    return n;    
}

int get_input(char **p, int n)
{
    if (NULL == p)
        return 0;

    int i, j;
    for (i = 0; i < n; i++)
    {
        p[i] = (char*)malloc(sizeof(char) * n);
        if (NULL == p[i])
            return 0;
    }
    getchar(); /*get the return key*/
    printf("please input the chars in order:\n");
    for (i = 0; i < n; i++)
    {
        for(j = 0; j < n; j++)
        {
            scanf("%c", &p[i][j]);    
        }
        getchar();
    }
    return 1;
}

int show_square(char **p, int n)
{
    if (NULL == p)
        return 0;

    int i, j;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
            printf("%c", p[i][j]);
        printf("\n");
    }
    return 1;
}

int get_words(char **p)
{
    printf("please input the words by order:\n");
    int i = 0;
    for (; i < N; i++)
    {
        p[i] = (char*)malloc(sizeof(char) * LENGTH);
        if (NULL == p[i])
        {
            fprintf(stderr, "stevens: malloc error!\n");
            exit(1);
        }
        memset(p[i], 0, LENGTH);
     gets(p[i]);
        if ('\0' == *(p[i]))
            break;
    }
}

int show_words(char **p)
{    
    if (NULL == p)
        return 0;
    int i = 0;
    for (; i < N; i++)
    {
        if (*(p[i]) != '\0')
            puts(p[i]);
        else
            break;
    }
    return 1;
}

int match_to_right(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
    int i, j;
    int len = strlen(p);
    if ((n - col) < len)
        return 0;
    
    for (i = col, j = 0; j < len; i++, j++)
        if (p_arriy[row][i] != p[j])
            return 0;    
    *a = row;
    *b = --j;    
    return 1;
}

int match_to_left(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
    int i, j;
    int len = strlen(p);
    if ((col + 1) < len)
        return 0;

    for (i = col, j = 0; j < len; i--, j++)
        if (p_arriy[row][i] != p[j])
            return 0;
    *a = row;
    *b = ++i;
    return 1;
}

int match_to_up(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
    int i, j;
    int len = strlen(p);
    if ((row + 1) < len)
        return 0;

    for (i = row, j = 0; j < len; i--, j++)
        if (p_arriy[i][col] != p[j])
            return 0;
    *a = ++i;
    *b = col;
    return 1;
}

int match_to_down(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
    int i, j;
    int len = strlen(p);
    if ((n - row) < len)
        return 0;

    for (i = row, j = 0; j < len; i++, j++)
        if (p_arriy[i][col] != p[j])
            return 0;
    *a = --i;
    *b = col;
    return 1;
}

int match_to_up_left(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
    int i, j, k;
    int len = strlen(p);
    
    if (((row + 1) < len) || ((col + 1) < len))
        return 0;

    for (i = row, j = col, k = 0; k < len; i--, j--, k++)
        if (p_arriy[i][j] != p[k])
            return 0;
    *a = ++i;
    *b = ++j;
    return 1;
}

int match_to_up_right(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
    int i, j, k;
    int len = strlen(p);

    if (((row + 1) < len) || ((n - col) < n))
        return 0;

    for (i = row, j = col, k = 0; k < len; i--, j++, k++)
        if (p_arriy[i][j] != p[k])
            return 0;
    *a = ++i;
    *b = --j;
    return 1;
}

int match_to_down_left(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
    int i, j, k;
    int len = strlen(p);

    if (((col + 1) < len) || ((n - row) < len))
        return 0;

    for (i = row, j = col, k = 0; k < len; i++, j--, k++)
        if (p_arriy[i][j] != p[k])
            return 0;
    *a = --i;
    *b = ++j;
    return 1;
}

int match_to_down_right(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
    int i, j, k;
    int len = strlen(p);

    if (((n - row) < len)|| ((n - col) < len))    
        return 0;

    for (i = row, j = col, k = 0; k < len; i++, j++, k++)
        if (p_arriy[i][j] != p[k])
            return 0;
    *a = --i;
    *b = --j;
    return 1;
}

int find_a_word(char **p_arriy, char *p, int n)
{
    if (p_arriy == NULL || p == NULL)
        return 0;
    int i, j, len, a, b;
    len = strlen(p);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
        {
            if (p_arriy[i][j] != p[0])
                continue;

            if(1 == match_to_right(p_arriy, p, i, j, n, &a, &b)
            || 1 == match_to_left(p_arriy, p, i, j, n, &a, &b)
            || 1 == match_to_up(p_arriy, p, i, j, n, &a, &b)
            || 1 == match_to_down(p_arriy, p, i, j, n, &a, &b)
            || 1 == match_to_up_left(p_arriy, p, i, j, n, &a, &b)
            || 1 == match_to_up_right(p_arriy, p, i, j, n, &a, &b)
            || 1 == match_to_down_left(p_arriy, p, i, j, n, &a, &b)
            || 1 == match_to_down_right(p_arriy, p, i, j, n, &a, &b)            )
            {
                printf("the words \"%s\" is from [%d][%d] to [%d][%d]\n", p, i, j, a, b);
                return 1;
            }
        }

    return 0;
}    

int find_words(char **p_arriy, char **p_words, int n)
{
    int i = 0;
    while (*(p_words[i]) != '\0')
    {
        find_a_word(p_arriy, p_words[i], n);
        i++;
    }
}

int main (int argc, char *argv[])
{
    int n;
    if (0 == (n = get_size()))
    {
        printf("the size of n should greater thant 1!\n");
        exit(1);
    }

    char **p = (char**)malloc(sizeof(char*) * n);
    if (NULL == p)
    {
        fprintf(stderr, "stevens: malloc error!\n");
        exit(1);
    }
    if (0 == get_input(p, n))
    {
        fprintf(stderr, "stevens: input error!\n");
        exit(1);
    }

    char **q = (char**)malloc(sizeof(char*) * N);
    if (NULL == q)
    {
        fprintf(stderr, "stevens: malloc error!\n");
        exit(1);
    }
    get_words(q);
    find_words(p, q, n);
    return 1;
}

阅读(1119) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~