Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //teja349
- #include <bits/stdc++.h>
- #include <vector>
- #include <set>
- #include <map>
- #include <string>
- #include <cstdio>
- #include <cstdlib>
- #include <climits>
- #include <utility>
- #include <algorithm>
- #include <cmath>
- #include <queue>
- #include <stack>
- #include <iomanip>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- //setbase - cout << setbase (16); cout << 100 << endl; Prints 64
- //setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77
- //setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
- //cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val
- using namespace std;
- using namespace __gnu_pbds;
- #define f(i,a,b) for(i=a;i<b;i++)
- #define rep(i,n) f(i,0,n)
- #define fd(i,a,b) for(i=a;i>=b;i--)
- #define pb push_back
- #define mp make_pair
- #define vi vector< int >
- #define vl vector< ll >
- #define ss second
- #define ff first
- #define ll long long
- #define pii pair< int,int >
- #define pll pair< ll,ll >
- #define sz(a) a.size()
- #define inf (1000*1000*1000+5)
- #define all(a) a.begin(),a.end()
- #define tri pair<int,pii>
- #define vii vector<pii>
- #define vll vector<pll>
- #define viii vector<tri>
- #define mod (1000*1000*1000+7)
- #define pqueue priority_queue< int >
- #define pdqueue priority_queue< int,vi ,greater< int > >
- #define flush fflush(stdout)
- #define primeDEN 727999983
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- // find_by_order() // order_of_key
- typedef tree<
- int,
- null_type,
- less<int>,
- rb_tree_tag,
- tree_order_statistics_node_update>
- ordered_set;
- int n,m;
- int a[1234][1234];
- int dist[3123456];
- vector<vi> adj(3123456);
- int valid(int i,int j){
- if(i<0 || j<0 || i>=n || j>=m)
- return 0;
- return a[i][j];
- }
- int bfs(int st){
- int i;
- rep(i,3*n*m){
- dist[i]=inf;
- }
- dist[st]=0;
- queue<int> que;
- que.push(st);
- int cur;
- while(!que.empty()){
- cur=que.front();
- que.pop();
- rep(i,adj[cur].size()){
- if(dist[adj[cur][i]]==inf){
- dist[adj[cur][i]]=dist[cur]+1;
- que.push(adj[cur][i]);
- }
- }
- }
- return 0;
- }
- int main(){
- std::ios::sync_with_stdio(false); cin.tie(NULL);
- int t;
- cin>>t;
- while(t--){
- //int n,m;
- cin>>n>>m;
- int i,j;
- int x,y;
- cin>>x>>y;
- x--;
- y--;
- rep(i,3*n*m+10){
- adj[i].clear();
- }
- string s;
- rep(i,n){
- cin>>s;
- rep(j,m){
- a[i][j]=s[j]-'0';
- }
- }
- int pos;
- rep(i,n){
- rep(j,m){
- if(!a[i][j])
- continue;
- pos=i*m+j;
- if(valid(i+1,j) && valid(i+2,j)){
- adj[pos].pb(2*n*m+(i+2)*m+j);
- }
- if(valid(i-1,j) && valid(i-2,j)){
- adj[pos].pb(2*n*m+(i-1)*m+j);
- }
- if(valid(i,j+1) && valid(i,j+2)){
- adj[pos].pb(n*m+i*m+j+2);
- }
- if(valid(i,j-1) && valid(i,j-2)){
- adj[pos].pb(n*m+i*m+j-1);
- }
- }
- }
- rep(i,n){
- rep(j,m){
- if(!valid(i,j) || !valid(i-1,j))
- continue;
- pos=2*n*m+i*m+j;
- if(valid(i+1,j)){
- adj[pos].pb((i+1)*m+j);
- }
- if(valid(i-2,j)){
- adj[pos].pb((i-2)*m+j);
- }
- if(valid(i,j-1) && valid(i-1,j-1)){
- adj[pos].pb(pos-1);
- }
- if(valid(i,j+1) && valid(i-1,j+1)){
- adj[pos].pb(pos+1);
- }
- }
- }
- rep(i,n){
- rep(j,m){
- if(!valid(i,j) || !valid(i,j-1))
- continue;
- pos=n*m+i*m+j;
- if(valid(i,j+1)){
- adj[pos].pb(i*m+j+1);
- }
- if(valid(i,j-2)){
- adj[pos].pb(i*m+j-2);
- }
- if(valid(i-1,j) && valid(i-1,j-1)){
- adj[pos].pb(pos-m);
- }
- if(valid(i+1,j) && valid(i+1,j-1)){
- adj[pos].pb(pos+m);
- }
- }
- }
- bfs(x*m+y);
- rep(i,n){
- rep(j,m){
- if(dist[i*m+j]==inf)
- dist[i*m+j]=-1;
- cout<<dist[i*m+j]<<" ";
- }
- cout<<endl;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment