String Statistics
描述
You have an n × n matrix of lower-case letters. You start from somewhere in the matrix, walk in one of the eight directions (left, right, up, down, up-left, up-right, down-left, down-right) and concatenate every letter you meet, in the order you see them, to form a string. You can stop anywhere (possibly not moving at all!), but you cannot go out of the matrix, or change direction in the middle of the journey.How many different non-empty strings can you get?
输入
The first line contains t (1 ≤ t ≤ 10), the number of test cases followed. Each test case begins with one integer n(1 ≤ n ≤ 30), followed by n lines, containing the matrix. The matrix will contain lower-case letters only.
输出
For each test case, print the number of different strings you can get.
样例输入
样例输出
#include<iostream> #include<string> #include<set> using namespace std;int dx[8]={0,0,1,-1,1,1,-1,-1}; //八个方向
int dy[8]={1,-1,0,0,-1,1,-1,1}; int n; char op[40][40]; string str; set<string>p;void dfs(int i,int j,int k) //按同方向搜索
{if(i>0&&j>0&&i<=n&&j<=n){str+=op[i][j];p.insert(str);dfs(i+dx[k],j+dy[k],k);} } int main() {int i,j,k,t;cin>>t;while(t--){cin>>n;getchar();for(i=1;i<=n;i++)for(j=1;j<=n;j++)cin>>op[i][j];p.clear(); //每寻找一次需要清除容器里的东西
for(i=1;i<=n;i++)for(j=1;j<=n;j++)for(k=0;k<8;k++)
{
str = "";str+=op[i][j];p.insert(str);dfs(i+dx[k],j+dy[k],k);}cout<<p.size()<<endl;}return 0; }