Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************
- * BISMIintAHIR RAHMANIR RAHIM *
- * Saddam Hossain shishir *
- * Hajee Mohammad Danesh Science & Technology University *
- * *
- ***************************************************************** */
- #include<bits/stdc++.h>
- #define all(v) v.begin(),v.end()
- #define sc scanf
- #define si(t) scanf("%d",&t)
- #define sl(t) scanf("%I64d",&t)
- #define sii(a,b) scanf("%d%d",&a,&b)
- #define pt(a) printf("%d\n",a)
- #define PLN(a) printf("%I64d\n",a)
- #define pf printf
- #define gcd(a,b) __gcd(a,b)
- #define ff first
- #define ss second
- #define pb push_back
- #define pii pair<int,int>
- #define mp make_pair
- #define pi acos(-1.0)
- #define PI 3.1415926535897932385
- #define Sin(a) sin((pi*a)/180)
- #define siz 3000001
- #define mem(ar) memset(ar,0,sizeof ar)
- #define one(x) __builtin_popcount(x)
- #define mod 100000009
- #define faster ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
- typedef long long ll;
- using namespace std;
- //int dx[]= {-1,-1,0,0,1,1};
- //int dy[]= {-1,0,-1,1,0,1};
- //int dx[]= {0,0,1,-1};/*4 side move*/
- //int dy[]= {-1,1,0,0};/*4 side move*/
- int dx[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
- int dy[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
- //int dx[]= {1,1,2,2,-1,-1,-2,-2}; /*knight move*/
- //int dy[]= {2,-2,1,-1,2,-2,1,-1}; /*knight move*/
- //'A'=65,'Z'=90 'a'=97 'z'=122 '0'=48
- //upper bound and lower bound
- #define LB(a,value) (lower_bound(all(a),value)-a.begin())
- #define UB(a,value) (upper_bound(all(a),value)-a.begin())
- //S.insert(lower_bound(S.begin( ),S.end( ),x),x); //S is vector
- int Set(int N,int pos)
- {
- return N=N | (1<<pos);
- }
- int reset(int N,int pos)
- {
- return N= N & ~(1<<pos);
- }
- bool check(int N,int pos)
- {
- return (bool)(N & (1<<pos));
- }
- /* -a%b---------
- ll Mod(ll a,ll b)
- {
- ll c = a % b;
- return (c < 0) ? c + b : c;
- }
- /*
- ll power(ll num,ll p)
- {
- int i;
- ll sum=1;
- for(i=1; i<=p; i++)
- sum*=num;
- return sum;
- }
- */
- int dis[1004][1005],vis[1004][1004];
- int row,col;
- void bfs(int sx,int sy)
- {
- int j,r;
- vis[sx][sy]=1;
- queue<pii>q;
- q.push(pii(sx,sy));
- while(!q.empty())
- {
- pii top=q.front();
- q.pop();
- j=r=0;
- for(int k=0; k<8; k++)
- {
- int tx=top.ff+dx[k];
- int ty=top.ss+dy[k];
- if(tx>=1&&tx<=row &&ty>=1 &&ty<=col&&vis[tx][ty]==0)
- {
- vis[tx][ty]=1;
- dis[tx][ty]=dis[top.ff][top.ss]+1;
- q.push(pii(tx,ty));
- }
- }
- }
- }
- char ch;
- int main()
- {
- int x,ro1,co1,ro2,co2,i,j,t;
- char c1,c2;
- si(t);
- while(t--)
- {
- memset(vis,0,sizeof vis);
- mem(dis);
- si(row),si(col);
- for(i=1; i<=row; i++)
- {
- for(j=1; j<=col; j++)
- {
- scanf(" %c",&ch);
- if(ch=='S')
- {
- ro1=i;
- co1=j;
- }
- else if(ch=='F')
- {
- ro2=i;
- co2=j;
- }
- else if(ch=='X')
- {
- vis[i][j]=1;
- }
- }
- }
- bfs(ro1,co1);
- printf("%d\n",dis[ro2][co2]-1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement