最近做的几条题(4)Secret Word

 Decription:

The following is one of the most famous secret letters:

 

3rd March

Dear George,

Greetings to all at Oxford . Many thanks for your

letter and for the Summer examination package.

All Entry Forms and Fees Forms should be ready

for final despatch to the syndicate by Friday

20th or at the very latest, I’m told, by the 21st.

Admin has improved here, though there’s room

for improvement still; just give us all two or three

more years and we’ll really show you! Please

don’t let these wretched 16+ proposals destroy

your basic O and A pattern. Certainly this

sort of change, if implemented immediately,

would bring chaos.

Sincerely yours,

 

If you take the last word from every line, you will get the following message:

Your package ready, Friday 21st, room 3. Please destroy this immediately.

 

Now is another example:

 

Some people like drama

the most. Some technic

of it is very dramati-

cally,and most of them

require the artists no

other but talents. Sir

Mooncarl is one of the

best actors.

Quite not understand? It doesn’t matter, and take the last letter from every line. Yes, it’s “ac-more”, and we wish all of you accepting more.

 

Now it’s your turn to find the secret word.

 

You are given a section paper (you can safely assume that the section paper is a grid which has the same number of cells of each line). Each cell holds a letter, including lowercase letters”a”~”z”, capital letters(“A”~”Z”), commas(,), full stops(“.”), and hyphens(“-“). You are also given a string, a secret word you are to find within the paper. The starting point may be anywhere in the paper. The path may move up, down, left, right, or diagonally from one letter to the next, and may use letters in the paper more than once, but you can not stay on the same cell without moving.

 

You are to calculate the number of ways the secret word can be found within the paper.

这题没完全做好,对于输入输出格式有所不同,而且开始没看好题目,仅对方矩阵寻找,忘记对于不同维数的矩阵处理,不过修改还是可以的,但不想再改了,大概思想都差不多了,应该有高手有更好的算法,我这个仅是利用递归,然后利用穷举所有起始点来进行寻找secret的个数,经过一些例子测试,答案都对了。

ABABA

BABAB

ABABA

BABAB

ABABA

ABABABBA

输出56448

 

AA

AA

AAAA

输出:108

程序:

#include<iostream>
#include<vector>
#include<string>
using namespace std;

enum Direction{U,D,L,R,RU,RD,LU,LD};

bool Move(int &i,int &j,int dim,Direction Dir) //移动函数
{
 if(Dir == U)
 {
  if(i>1)
  {
   i--;
   return true;
  }
 }
 else if(Dir == D)
 {
  if(i<dim)
  {
   i++;
   return true;
  }
 }
 else if(Dir == L)
 {
  if(j>1)
  {
   j--;
   return true;
  }
 }
 else if(Dir == R)
 {
  if(j<dim)
  {
   j++;
   return true;
  }
 }
 else if(Dir == RU)
 {
  if((j<dim)&&(i>1))
  {
   j++;
   i--;
   return true;
  }
 }
 else if(Dir == RD)
 {
  if((j<dim)&&(i<dim))
  {
   j++;
   i++;
   return true;
  }
 }
 else if(Dir == LU)
 {
  if((j>1)&&(i>1))
  {
   j--;
   i--;
   return true;
  }
 }
 else if(Dir == LD)
 {
  if((j>1)&&(i<dim))
  {
   i++;
   j--;
   return true;
  }
 }
 return false;
}

bool Match(char ch[],int i,int j,int dim,string str) //是否匹配
{
 char test = ch[(dim*(i-1)+j)];
 char test1 = (str.c_str())[0];
 if(test == test1)
  return true;
 else
  return false;
}

void Method(char ch[],int i,int j,int dim,string str,int &result)  //递归求得由某点(i,j)开始的路径
{
 int a(0),b(0);
 string store;
 
 if((str.size() == 1)&&(Match(ch,i,j,dim,str)))
 {
  result++;
 }
 else
 {
  if(Match(ch,i,j,dim,str))
  {
   a = i;
   b = j;
   str = str.substr(1,str.size());
   store = str;
   
   if(Move(i,j,dim,RU))
   {
    Method(ch,i,j,dim,str,result);
    i = a;
    j = b;
    str = store;
   }
   if(Move(i,j,dim,R))
   {
    Method(ch,i,j,dim,str,result);
    i = a;
    j = b;
    str = store;
   }
   if(Move(i,j,dim,RD))
   {
    Method(ch,i,j,dim,str,result);
    i = a;
    j = b;
    str = store;
   }
   if(Move(i,j,dim,D))  
   {
    Method(ch,i,j,dim,str,result);
    i = a;
    j = b;
    str = store;
   }
   if(Move(i,j,dim,LD))
   {
    Method(ch,i,j,dim,str,result);
    i = a;
    j = b;
    str = store;
   }
   if(Move(i,j,dim,L))  
   {
    Method(ch,i,j,dim,str,result);
    i = a;
    j = b;
    str = store;
   }
   if(Move(i,j,dim,LU)) 
   {
    Method(ch,i,j,dim,str,result);
    i = a;
    j = b;
    str = store;
   }
   if(Move(i,j,dim,U))
   {
    Method(ch,i,j,dim,str,result);
    i = a;
    j = b;
    str = store;
   }
  }
 }
}

int Total(char ch[],int dim,string str)  //循环所有切入点
{
 int result = 0;
 int sum = 0;
 for(int i = 1;i<=dim;i++)
 {
  for(int j = 1;j<=dim;j++)
  {
   Method(ch,i,j,dim,str,result);
   sum += result;
   result = 0;
  }
 }

 return sum;
}

void main()    //测试程序
{
 
 string secret;
 cout<<"How many dim...."<<endl;
 int dim;
 cin>>dim;
 char *ch = new char[dim*dim+1];
 ch[0] = '0';
 char test;
 cout<<endl;
 cout<<"Array..."<<endl;
 for(int j = 1;j<=dim;j++)
 {
  for(int i = 1;i<=dim;i++)
  {
   cin>>test;
   ch[((i-1)*dim+j)] = test;
  }
 }
 cout<<endl;
 cout<<"Secret word..."<<endl;
 cin>>secret;
 int result = 0;
 result = Total(ch,dim,secret);
 cout<<endl;
 cout<<"Find:"<<result<<endl;
 delete []ch;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值