#include <iostream.h>
using namespace std;
void tree_search(string max_string,int length,int len, int pos_x, int pos_y, int &count);
void input_string();
int n;
char **matrix=0;
int main() {
int i,j,total=0,length=0;
string des_str;
input_string();
cout<<"input the string to compare! des_str:";
cin>>des_str;
length=des_str.length();
for(i=0; i<n; i++){
for(j=0; j<n; j++){
tree_search(des_str,length,0,i,j,total);
}
}
cout<<"seach des_str's "<<des_str<<" total ways is "<<total;
return 0;
}
void input_string(){
int i,j;
cout<<"input the layer of matrix!n:";
cin>>n;
char letter[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M'
,'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
matrix=new char *[n];
for(i=0; i<n; i++){
matrix[i]=new char[n];
}
cout<<"the n! matrix is:"<<endl;
srand((unsigned) time(NULL));
for(i=0; i<n; i++){
for(j=0; j<n; j++){
matrix[i][j] = letter[rand()%26];
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
void tree_search(string max_string,int length,int len, int pos_x, int pos_y, int &count){
if(0 <= pos_x && pos_x <= n-1 && 0 <= pos_y && pos_y <= n-1){
if(len == length-1 && matrix[pos_x][pos_y] == max_string.at(len)){
count++;
}
else if(matrix[pos_x][pos_y] == max_string.at(len)){
tree_search(max_string, length, len+1, pos_x, pos_y-1, count);
tree_search(max_string, length, len+1, pos_x, pos_y+1, count);
tree_search(max_string, length, len+1, pos_x+1, pos_y-1, count);
tree_search(max_string, length, len+1, pos_x+1, pos_y, count);
tree_search(max_string, length, len+1, pos_x+1, pos_y+1, count);
tree_search(max_string, length, len+1, pos_x-1, pos_y-1, count);
tree_search(max_string, length, len+1, pos_x-1, pos_y, count);
tree_search(max_string, length, len+1, pos_x-1, pos_y+1, count);
}
}
}
using namespace std;
void tree_search(string max_string,int length,int len, int pos_x, int pos_y, int &count);
void input_string();
int n;
char **matrix=0;
int main() {
int i,j,total=0,length=0;
string des_str;
input_string();
cout<<"input the string to compare! des_str:";
cin>>des_str;
length=des_str.length();
for(i=0; i<n; i++){
for(j=0; j<n; j++){
tree_search(des_str,length,0,i,j,total);
}
}
cout<<"seach des_str's "<<des_str<<" total ways is "<<total;
return 0;
}
void input_string(){
int i,j;
cout<<"input the layer of matrix!n:";
cin>>n;
char letter[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M'
,'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
matrix=new char *[n];
for(i=0; i<n; i++){
matrix[i]=new char[n];
}
cout<<"the n! matrix is:"<<endl;
srand((unsigned) time(NULL));
for(i=0; i<n; i++){
for(j=0; j<n; j++){
matrix[i][j] = letter[rand()%26];
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
void tree_search(string max_string,int length,int len, int pos_x, int pos_y, int &count){
if(0 <= pos_x && pos_x <= n-1 && 0 <= pos_y && pos_y <= n-1){
if(len == length-1 && matrix[pos_x][pos_y] == max_string.at(len)){
count++;
}
else if(matrix[pos_x][pos_y] == max_string.at(len)){
tree_search(max_string, length, len+1, pos_x, pos_y-1, count);
tree_search(max_string, length, len+1, pos_x, pos_y+1, count);
tree_search(max_string, length, len+1, pos_x+1, pos_y-1, count);
tree_search(max_string, length, len+1, pos_x+1, pos_y, count);
tree_search(max_string, length, len+1, pos_x+1, pos_y+1, count);
tree_search(max_string, length, len+1, pos_x-1, pos_y-1, count);
tree_search(max_string, length, len+1, pos_x-1, pos_y, count);
tree_search(max_string, length, len+1, pos_x-1, pos_y+1, count);
}
}
}