Can be solved easily by considering both horizontal,vertical and both diagonals..
If match is found output the answer else print Not found.
//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m;
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p[500],q;
si(n);
f(i,0,n)
cin>>p[i];
while(cin>>q){
m=q.size();
if(q=="0")
break;
c=0;
f(i,0,n){
c=0;
f(j,0,n){
if(p[i][j]==q[0]){
if(j+m<=n){
string s;
f(k,j,j+m)
s+=p[i][k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+1<<","<<j+m<<endl;
c=1;
break;
}
}
if(j-m+1>=0){
string s;
rf(k,j,j-m+1)
s+=p[i][k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+1<<","<<j+2-m<<endl;
c=1;
break;
}
}
if(i+m<=n){
string s;
f(k,i,i+m)
s+=p[k][j];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+m<<","<<j+1<<endl;
c=1;
break;
}
}
if(i-m+1>=0){
string s;
rf(k,i,i-m+1)
s+=p[k][j];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+2-m<<","<<j+1<<endl;
c=1;
break;
}
}
if(i+m<=n && j+m<=n){
string s;
f(k,0,m)
s+=p[i+k][j+k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+m<<","<<j+m<<endl;
c=1;
break;
}
}
if(i-m+1>=0 && j-m+1>=0){
string s;
f(k,0,m)
s+=p[i-k][j-k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+2-m<<","<<j+2-m<<endl;
c=1;
break;
}
}
if(i+m<=n && j-m+1>=0){
string s;
f(k,0,m)
s+=p[i+k][j-k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+m<<","<<j+2-m<<endl;
c=1;
break;
}
}
if(i-m+1>=0 && j+m<=n){
string s;
f(k,0,m)
s+=p[i-k][j+k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+2-m<<","<<j+m<<endl;
c=1;
break;
}
}
}
}
if(c)
break;
}
if(!c)
printf("Not found\n");
}
return 0;
}
If match is found output the answer else print Not found.
//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m;
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p[500],q;
si(n);
f(i,0,n)
cin>>p[i];
while(cin>>q){
m=q.size();
if(q=="0")
break;
c=0;
f(i,0,n){
c=0;
f(j,0,n){
if(p[i][j]==q[0]){
if(j+m<=n){
string s;
f(k,j,j+m)
s+=p[i][k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+1<<","<<j+m<<endl;
c=1;
break;
}
}
if(j-m+1>=0){
string s;
rf(k,j,j-m+1)
s+=p[i][k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+1<<","<<j+2-m<<endl;
c=1;
break;
}
}
if(i+m<=n){
string s;
f(k,i,i+m)
s+=p[k][j];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+m<<","<<j+1<<endl;
c=1;
break;
}
}
if(i-m+1>=0){
string s;
rf(k,i,i-m+1)
s+=p[k][j];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+2-m<<","<<j+1<<endl;
c=1;
break;
}
}
if(i+m<=n && j+m<=n){
string s;
f(k,0,m)
s+=p[i+k][j+k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+m<<","<<j+m<<endl;
c=1;
break;
}
}
if(i-m+1>=0 && j-m+1>=0){
string s;
f(k,0,m)
s+=p[i-k][j-k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+2-m<<","<<j+2-m<<endl;
c=1;
break;
}
}
if(i+m<=n && j-m+1>=0){
string s;
f(k,0,m)
s+=p[i+k][j-k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+m<<","<<j+2-m<<endl;
c=1;
break;
}
}
if(i-m+1>=0 && j+m<=n){
string s;
f(k,0,m)
s+=p[i-k][j+k];
if(s==q){
cout<<i+1<<","<<j+1<<" "<<i+2-m<<","<<j+m<<endl;
c=1;
break;
}
}
}
}
if(c)
break;
}
if(!c)
printf("Not found\n");
}
return 0;
}
No comments:
Post a Comment