Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<string> res;
  6. vector<string> s;
  7. char mc[101][101];
  8. bool visited[101][101];
  9. int xqX[] = {0,0,1,1,1,-1,-1,-1};
  10. int xqY[] = {1,-1,0,1,-1,0,1,-1};
  11. int k,m,n;
  12.  
  13. void Try(int row, int col, string t){
  14.     visited[row][col] = true;
  15.     t+=mc[row][col];
  16.     for(int i=0;i<s.size();i++){
  17.         if (s[i] == t){
  18.             res.push_back(t);
  19.             break;
  20.         }
  21.     }
  22.     for(int i=0;i<8;i++){
  23.         int r = row + xqX[i];
  24.         int c = col + xqY[i];
  25.         if (r > 0 && r <= m && c > 0 && c <= n && visited[r][c] == false){
  26.             Try(r,c,t);
  27.         }
  28.     }
  29.     t.pop_back();
  30.     visited[row][col] = false;
  31. }
  32.  
  33. void clear(){
  34.     res.clear();
  35.     s.clear();
  36.     for(int i=0;i<101;i++){
  37.         for(int j=0;j<101;j++){
  38.             visited[i][j] = false;
  39.         }
  40.     }
  41. }
  42.  
  43. void process(){
  44.    
  45.     cin >> k >> m >> n;
  46.     s.resize(k);
  47.     for(int i=0;i<k;i++) cin >> s[i];
  48.     for(int i=1;i<=m;i++){
  49.         for(int j=1;j<=n;j++){
  50.             cin >> mc[i][j];
  51.         }
  52.     }
  53.     string t="";
  54.     for(int i=1;i<=m;i++){
  55.         for(int j=1;j<=n;j++){    
  56.             Try(i,j,t);
  57.         }
  58.     }
  59.     if ( !res.size() ){
  60.         cout << -1 << endl;
  61.         return;
  62.     }
  63.     for( auto z: res){
  64.         cout << z << " ";
  65.     }
  66.     cout << endl;
  67. }
  68.  
  69. int main(){
  70.  
  71.     int T;
  72.     cin >> T;
  73.     while(T--) process();
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement