boggle

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


#define ROW 3
#define COLUMN 3
#define DICTIONARYSIZE 2

struct BOGGLE{
    char *dictionary[DICTIONARYSIZE];
    char boggle[3][3];
};


struct BOGGLE boggle2 = {
    .dictionary = {"GEEKS", "FOR", "QUIZ", "GO"},
    .boggle = { {'G', 'I', 'Z'},
                {'U', 'E', 'K'},
                {'Q', 'S', 'E'}}
};

struct BOGGLE boggle1 = {
    .dictionary = {"GEEKS", "ABCFIHGDE"},
    .boggle = { {'A', 'B', 'C'},
                {'D', 'E', 'F'},
                {'G', 'H', 'I'}}
};
bool findWay(char *input, int row ,int col){
    char c = *(++input);
    if(c == '\0')
        return true;
    if(col < 2 && c == boggle1.boggle[row][col + 1])
        if(findWay(input , row, col + 1))
            return true;
    if(col > 0 && c == boggle1.boggle[row][col - 1])
        if(findWay(input , row, col - 1))
            return true;
    if(row < 2 && c == boggle1.boggle[row + 1][col])
        if(findWay(input , row + 1, col))
            return true;
    if(row > 0 && c == boggle1.boggle[row - 1][col])
        if(findWay(input , row - 1, col))
            return true;
            
    if(row < 2 && col < 2){
        if(boggle1.boggle[row + 1][col + 1] == c)
            if(findWay(input , row + 1, col + 1))
                return true;
    }
    if(row > 0 && col > 0){
        if(boggle1.boggle[row - 1][col - 1] == c)
            if(findWay(input , row - 1, col - 1))
                return true;
    }
    
    if(row > 0 && col < 2){
        if(boggle1.boggle[row - 1][col + 1] == c)
            if(findWay(input , row - 1, col + 1))
                return true;
    }
    
    if(col > 0 && row < 2){
        if(boggle1.boggle[row + 1][col - 1] == c)
            if(findWay(input , row + 1, col - 1))
                return true;
    }
    
    return false;
};

bool findStart(char *input, int *row, int *col){
    for(*row = 0; *row < ROW; (*row)++){
        for(*col = 0; *col < COLUMN; (*col)++)
            if(boggle1.boggle[*row][*col] == *input)
                return false;
    }
    return true;
}

void solveBoggle(){
    for(int i = 0; i < DICTIONARYSIZE; i++){
        char *input =  boggle1.dictionary[i];
        char *input_ =  boggle1.dictionary[i];
        int row_, col_, *row, *col;
        row_ = col_ = 0;
        row = &row_;
        col = &col_;
        if(findStart(input, row, col))
            continue ;
        row_ = *row;
        col_ = *col;
        if(findWay(input, row_, col_))
            printf("%s\n", boggle1.dictionary[i]);
    }
}


int main(){
    solveBoggle();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值