Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct point
- {
- int x,y;
- };
- char str[52][52];
- string q;
- int n,m;
- bool bfs(point ind,int X,int Y)
- {
- queue<point>Q;
- point dir;
- dir.x = X;
- dir.y = Y;
- Q.push(ind);
- int l = 1;
- while(!Q.empty() && l<q.length())
- {
- point u = Q.front();
- Q.pop();
- point v;
- v.x = u.x+dir.x;
- v.y = u.y+dir.y;
- if(v.x>=0 && v.y>=0 && v.x<m && v.y<n && q[l]==str[v.x][v.y])
- {
- Q.push(v);
- l++;
- }
- }
- if(l==q.length())
- return true;
- return false;
- }
- int main()
- {
- int tc,test,i,j,k,l,t,len;
- scanf("%d",&test);
- for(tc=1; tc<=test; tc++)
- {
- scanf("%d%d",&m,&n);
- getchar();
- for(i=0; i<m; i++)
- {
- gets(str[i]);
- len = strlen(str[i]);
- for(j=0; j<len; j++)
- {
- if(isupper(str[i][j]))
- {
- str[i][j] = tolower(str[i][j]);
- }
- }
- }
- scanf("%d",&t);
- getchar();
- for(i=1; i<=t; i++)
- {
- cin>>q;
- transform(q.begin(),q.end(),q.begin(),::tolower);
- bool dhukse = false;
- for(k=0; k<m; k++)
- {
- for(l=0; l<n; l++)
- {
- if(q[0]==str[k][l])
- {
- point ind;
- ind.x = k;
- ind.y = l;
- if(bfs(ind,1,1)||bfs(ind,0,1)||bfs(ind,1,0)||bfs(ind,1,-1)||bfs(ind,0,-1)||bfs(ind,-1,0)||bfs(ind,-1,1)||bfs(ind,-1,-1))
- {
- printf("%d %d\n",k+1,l+1);
- dhukse = true;
- break;
- }
- }
- }
- if(dhukse)
- {
- break;
- }
- }
- }
- if(tc<test)
- {
- puts("");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment