deadwing97

ROLLBAR TESTER

Feb 24th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. //teja349
  2. #include <bits/stdc++.h>
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6. #include <string>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <climits>
  10. #include <utility>
  11. #include <algorithm>
  12. #include <cmath>
  13. #include <queue>
  14. #include <stack>
  15. #include <iomanip>
  16. #include <ext/pb_ds/assoc_container.hpp>
  17. #include <ext/pb_ds/tree_policy.hpp>
  18. //setbase - cout << setbase (16); cout << 100 << endl; Prints 64
  19. //setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77
  20. //setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
  21. //cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val
  22.  
  23. using namespace std;
  24. using namespace __gnu_pbds;
  25.  
  26. #define f(i,a,b) for(i=a;i<b;i++)
  27. #define rep(i,n) f(i,0,n)
  28. #define fd(i,a,b) for(i=a;i>=b;i--)
  29. #define pb push_back
  30. #define mp make_pair
  31. #define vi vector< int >
  32. #define vl vector< ll >
  33. #define ss second
  34. #define ff first
  35. #define ll long long
  36. #define pii pair< int,int >
  37. #define pll pair< ll,ll >
  38. #define sz(a) a.size()
  39. #define inf (1000*1000*1000+5)
  40. #define all(a) a.begin(),a.end()
  41. #define tri pair<int,pii>
  42. #define vii vector<pii>
  43. #define vll vector<pll>
  44. #define viii vector<tri>
  45. #define mod (1000*1000*1000+7)
  46. #define pqueue priority_queue< int >
  47. #define pdqueue priority_queue< int,vi ,greater< int > >
  48. #define flush fflush(stdout)
  49. #define primeDEN 727999983
  50. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  51.  
  52. // find_by_order()  // order_of_key
  53. typedef tree<
  54. int,
  55. null_type,
  56. less<int>,
  57. rb_tree_tag,
  58. tree_order_statistics_node_update>
  59. ordered_set;
  60.  
  61. int n,m;
  62. int a[1234][1234];
  63. int dist[3123456];
  64. vector<vi> adj(3123456);
  65. int valid(int i,int j){
  66.     if(i<0 || j<0 || i>=n || j>=m)
  67.         return 0;
  68.     return a[i][j];
  69. }
  70. int bfs(int st){
  71.     int i;
  72.     rep(i,3*n*m){
  73.         dist[i]=inf;
  74.     }
  75.     dist[st]=0;
  76.     queue<int> que;
  77.     que.push(st);
  78.     int cur;
  79.     while(!que.empty()){
  80.         cur=que.front();
  81.         que.pop();
  82.         rep(i,adj[cur].size()){
  83.             if(dist[adj[cur][i]]==inf){
  84.                 dist[adj[cur][i]]=dist[cur]+1;
  85.                 que.push(adj[cur][i]);
  86.             }
  87.         }
  88.     }
  89.     return 0;
  90. }
  91. int main(){
  92.     std::ios::sync_with_stdio(false); cin.tie(NULL);
  93.     int t;
  94.     cin>>t;
  95.     while(t--){
  96.         //int n,m;
  97.         cin>>n>>m;
  98.         int i,j;
  99.         int x,y;
  100.         cin>>x>>y;
  101.         x--;
  102.         y--;
  103.         rep(i,3*n*m+10){
  104.             adj[i].clear();
  105.         }
  106.         string s;
  107.         rep(i,n){
  108.             cin>>s;
  109.             rep(j,m){
  110.                 a[i][j]=s[j]-'0';
  111.             }
  112.         }
  113.         int pos;
  114.         rep(i,n){
  115.             rep(j,m){
  116.                 if(!a[i][j])
  117.                     continue;
  118.                 pos=i*m+j;
  119.                 if(valid(i+1,j) && valid(i+2,j)){
  120.                     adj[pos].pb(2*n*m+(i+2)*m+j);
  121.                 }
  122.                 if(valid(i-1,j) && valid(i-2,j)){
  123.                     adj[pos].pb(2*n*m+(i-1)*m+j);
  124.                 }
  125.  
  126.                 if(valid(i,j+1) && valid(i,j+2)){
  127.                     adj[pos].pb(n*m+i*m+j+2);
  128.                 }
  129.                 if(valid(i,j-1) && valid(i,j-2)){
  130.                     adj[pos].pb(n*m+i*m+j-1);
  131.                 }
  132.             }
  133.         }
  134.         rep(i,n){
  135.             rep(j,m){
  136.                 if(!valid(i,j) || !valid(i-1,j))
  137.                     continue;
  138.                 pos=2*n*m+i*m+j;
  139.                 if(valid(i+1,j)){
  140.                     adj[pos].pb((i+1)*m+j);
  141.                 }
  142.                 if(valid(i-2,j)){
  143.                     adj[pos].pb((i-2)*m+j);
  144.                 }
  145.                 if(valid(i,j-1) && valid(i-1,j-1)){
  146.                     adj[pos].pb(pos-1);
  147.                 }
  148.                 if(valid(i,j+1) && valid(i-1,j+1)){
  149.                     adj[pos].pb(pos+1);
  150.                 }
  151.             }
  152.         }
  153.  
  154.         rep(i,n){
  155.             rep(j,m){
  156.                 if(!valid(i,j) || !valid(i,j-1))
  157.                     continue;
  158.                 pos=n*m+i*m+j;
  159.                 if(valid(i,j+1)){
  160.                     adj[pos].pb(i*m+j+1);
  161.                 }
  162.                 if(valid(i,j-2)){
  163.                     adj[pos].pb(i*m+j-2);
  164.                 }
  165.                 if(valid(i-1,j) && valid(i-1,j-1)){
  166.                     adj[pos].pb(pos-m);
  167.                 }
  168.                 if(valid(i+1,j) && valid(i+1,j-1)){
  169.                     adj[pos].pb(pos+m);
  170.                 }
  171.             }
  172.         }
  173.  
  174.         bfs(x*m+y);
  175.         rep(i,n){
  176.             rep(j,m){
  177.                 if(dist[i*m+j]==inf)
  178.                     dist[i*m+j]=-1;
  179.                 cout<<dist[i*m+j]<<" ";
  180.             }
  181.             cout<<endl;
  182.         }
  183.     }
  184.     return 0;  
  185. }
Add Comment
Please, Sign In to add comment