加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

word Puzzle 的程序实现

(2010-01-20 21:47:14)
标签:

word

puzzle

找单词

it

分类: IT

    给出一个字母方阵,从中找出单词。输入:wordpuzzle.txt和word.txt,其中puzzle.txt放的是字母方阵,word.txt 放的是要寻找的单词。可以横竖斜着找。

wordpuzzle.txt如下:

R I R R G T S J R H K E I T V I D B Z O
M G N I F F U T S I O Y R V A G F F X Z
P D O Z L Z J L T A S Y E S E M N V X L
P D D E H J F K P K X Z E U H T C C K L
U O J P N G O R K G R I S I S T Q Z L A
Y H P M I K G F D V R E W O L F Y A M F
B T F E F M I E R R C M P E P L B U Z X
B N C X C S R E E E Z A D P I T B F P U
Y R X A O A B B P U Y Y T M O Z J F I U
O O M N X M N O D W V B A O F S L E B V
Y C T N E A T P E M I F F H P A R A D E
D L A V R A A A I T Q W Q R O Z G S X C
G S O C T X H I R E C S M V A G F T M H
E N M O T A Z D D J C J T H A N K F U L
H Q E I E I P N I K P M U P T U J B F U
J S O J R Z G O D R W Z G Z U K K U K C
J W X M V G W X A D I N N E R Y J H B F
N M F V A N L N T S U B B Y K J O H P P
K C Z M D H J I B F S Y B A E V F U J E
T U I G K T M D P C F V Z C Y Y M K Z B
word.txt如下:

NOVEMBER
TURKEY
STUFFING
HAM
PUMPKINPIE
CRANBERRIES
POTATOES
PECANPIE
PILGRIMS
THANKFUL
MAYFLOWER
CORN
DINNER
FAMILY
FOOTBALL
FEAST
PARADE

程序实现代码:

// wordPuzzle.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

char puzzle[20][20];
char word[17][15];
bool isHere[20][20];
ofstream outfile("result.txt");
void initArray()
{
 ifstream ifile("wordPuzzle.txt");
 char ch;
 for(int i=0;i<20;i++)
    for(int j=0;j<20;j++)  
  {
  ifile.get(ch);
  puzzle[i][j]=ch;
  ifile.get();
  }
   ifstream file("word.txt");  
   char str[15];
   {
    for(int i=0;i<17;i++)
    {
    file.getline(str,20);
       strcpy_s(word[i],str);
    }
   }
  ifile.close();
  file.close(); 
}
void display(bool Spuzzle=true,bool Sword=false)
{
 if(Spuzzle)
 {
  for(int i=0;i<20;i++)
  {
   for(int j=0;j<20;j++)
    //outfile<<puzzle[i][j]<<" ";
   //outfile<<endl;
   cout<<puzzle[i][j]<<" ";
   cout<<endl;
  }
 }
 outfile<<endl;
 if(Sword)
 {
  for(int i=0;i<17;i++)
   outfile<<word[i]<<endl;
 }
 outfile<<endl;
}
bool search(const char *wordChar,int row,int col, int pos,int direction)
{
 if(wordChar[pos]=='\0')//这句判断必须在下面的那个判断的前面,否则若一个单词的最后一个字母位于边界位置,就会返回错误
 {
  return true;
 }
 if(row<0||row>19||col<0||col>19)
 return false;

 if(wordChar[pos]!=puzzle[row][col])
 return false;
 else
 {
 isHere[row][col]=true;
 switch(direction)
 {
 case 0:search(wordChar,row-1,col,pos+1,0);//up
  break;
 case 1: return search(wordChar,row-1,col-1,pos+1,1);//up left
  break;
 case 2: return search(wordChar,row-1,col+1,pos+1,2);//up right
  break;
 case 3: return search(wordChar,row,col-1,pos+1,3);//left
  break;
 case 4: return search(wordChar,row,col+1,pos+1,4);//right
   break;
 case 5: return search(wordChar,row+1,col,pos+1,5);//down
   break;
 case 6: return search(wordChar,row+1,col-1,pos+1,6);//down left
   break;
 case 7: return search(wordChar,row+1,col+1,pos+1,7);//down right
   break;
 }
  }
    //return true;
}
void resetIshere()
{
 for(int i=0;i<20;i++)
  for(int j=0;j<20;j++)
   isHere[i][j]=false;
}

void outputResult(char *wordth)
{
 string str(wordth);
   outfile<<str<<endl;
   for(int i=0;i<20;i++)
  {
   for(int j=0;j<20;j++)
    {
     if(isHere[i][j])
     outfile<<puzzle[i][j];
        else
   outfile<<'#'<<' ';
    }
 outfile<<endl;
 }
}
void findWord(char *wordth)
{
 resetIshere();
 bool alreadyFind=false;
    for(int i=0;i<20;i++)
 {
  if(alreadyFind)
   break;
  for(int j=0;j<20;j++)
      {
     if(!search(wordth,i,j,0,0))
    if(!search(wordth,i,j,0,1))
     if(!search(wordth,i,j,0,2))
      if(!search(wordth,i,j,0,3))
       if(!search(wordth,i,j,0,4))
        if(!search(wordth,i,j,0,5))
         if(!search(wordth,i,j,0,6))
          alreadyFind=search(wordth,i,j,0,7);
         else
          alreadyFind=true;
        else
             alreadyFind=true;
       else
        alreadyFind=true;
      else
       alreadyFind=true;
     else
      alreadyFind=true;
    else
     alreadyFind=true;
     else
      alreadyFind=true;
   
   if(alreadyFind)
  {
   outputResult(wordth);
   break;//只能跳出单层循环
  }
  else
  resetIshere();
   }
  }
   if(!alreadyFind)
   {
  string str(wordth);
  outfile<<str<<"   not found"<<endl;
   }
}
int _tmain(int argc, _TCHAR* argv[])
{
 initArray();
 display();
 for(int i=0;i<17;i++)
 {
  findWord(word[i]); 
  outfile<<"================================="<<endl;
 }
 return 0;
}

最终输出如下:

 

NOVEMBER
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # R# # # # # # # # # # #
# # # # # # # E# # # # # # # # # # # #
# # # # # # B# # # # # # # # # # # # #
# # # # # M# # # # # # # # # # # # # #
# # # # E# # # # # # # # # # # # # # #
# # # V# # # # # # # # # # # # # # # #
# # O# # # # # # # # # # # # # # # # #
# N# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
=================================
TURKEY
。。。以下省略.

 

0

阅读 收藏 喜欢 打印举报/Report
后一篇:Funny China
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有