Advertisement
fireLUFFY

MSProblem-1

Jul 24th, 2021
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define inf 2e18
  6. int MOD=1000000007;//998244353;
  7. const int nn=1000050;
  8. bool prime[nn]; //array to store precalculated primes till 10^6
  9. void cal_primes(){memset(prime,true,sizeof(prime)); for(int i=2;i<=sqrt(nn);++i){ if(prime[i]==true){ for(int j=i*i;j<=nn;j+=i){prime[j]=false;}}}}
  10.  
  11. //testcases provided at the end
  12.  
  13. int n,m;
  14. bool check(int x,int y)
  15. {
  16.     if((x>n-1)||(y>m-1)||(x<0)||(y<0))
  17.         return false;
  18.     return true;
  19. }
  20.  
  21. void solve(int t)
  22. {
  23.     int testcases=t;
  24.     while(t--)
  25.     {
  26.     //  cout<<"Case #"<<(testcases-t)<<": "<<endl;
  27.         cin>>n>>m;   //number of rows and columns in the string matrix
  28.         string s;
  29.         vector<string>v;
  30.         for(int i=0;i<n;i++)
  31.         {
  32.             cin>>s;
  33.             v.push_back(s);
  34.         }
  35.  
  36.         bool vis[n][m][4];
  37.         for(int i=0;i<n;i++)
  38.             for(int j=0;j<m;j++)
  39.                 for(int k=0;k<4;k++)
  40.                     vis[i][j][k]=false;
  41.  
  42.         bool flag=true;
  43.         int dir=0,x=0,y=0;
  44.         while(flag)
  45.         {
  46.             if(vis[x][y][dir])
  47.                 flag=false;
  48.             else
  49.             {
  50.                 if((check(x,y))&&(v[x][y]=='.'))
  51.                 {
  52.                     vis[x][y][dir]=true;;
  53.                     if(dir==0)y++;
  54.                     else if(dir==1)x++;
  55.                     else if(dir==2)y--;
  56.                     else x--;
  57.                 }
  58.                 else
  59.                 {
  60.                     if(dir==0)y--;
  61.                     else if(dir==1)x--;
  62.                     else if(dir==2)y++;
  63.                     else x++;
  64.  
  65.                     dir=(dir+1)%4;
  66.                 }
  67.             }
  68.         }
  69.  
  70.         int ans=0;
  71.         for(int i=0;i<n;i++)
  72.             for(int j=0;j<m;j++)
  73.                 if(vis[i][j][0]||vis[i][j][1]||vis[i][j][2]||vis[i][j][3])
  74.                     ans++;
  75.  
  76.         cout<<ans<<endl;       
  77.     }
  78. }
  79.  
  80. main()
  81. {
  82.     auto start=chrono::system_clock::now();
  83.     {
  84.         #ifndef ONLINE_JUDGE
  85.             freopen("input.txt","r",stdin);
  86.             freopen("output.txt","w",stdout);
  87.             freopen("error.txt","w",stderr);
  88.         #endif 
  89.         ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  90.         int t=1;
  91.     //  cin>>t;
  92.         solve(t);
  93.     }
  94.     auto end=chrono::system_clock::now();
  95.     chrono::duration<double> elapsed=end-start;
  96. //  cout<<endl<<"Time taken: "<<elapsed.count()<<" sec";
  97.     return 0;
  98. }
  99.  
  100.  
  101. /*
  102.  
  103. testcase 1:
  104.  
  105. 4 5
  106. ...x.
  107. .x..x
  108. x...x
  109. ..x..
  110.  
  111. testcase 2:
  112.  
  113. 4 7
  114. ....X..
  115. X......
  116. .....X.
  117. .......
  118.  
  119. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement