Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define int long long
- #define inf 2e18
- int MOD=1000000007;//998244353;
- const int nn=1000050;
- bool prime[nn]; //array to store precalculated primes till 10^6
- 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;}}}}
- //testcases provided at the end
- int n,m;
- bool check(int x,int y)
- {
- if((x>n-1)||(y>m-1)||(x<0)||(y<0))
- return false;
- return true;
- }
- void solve(int t)
- {
- int testcases=t;
- while(t--)
- {
- // cout<<"Case #"<<(testcases-t)<<": "<<endl;
- cin>>n>>m; //number of rows and columns in the string matrix
- string s;
- vector<string>v;
- for(int i=0;i<n;i++)
- {
- cin>>s;
- v.push_back(s);
- }
- bool vis[n][m][4];
- for(int i=0;i<n;i++)
- for(int j=0;j<m;j++)
- for(int k=0;k<4;k++)
- vis[i][j][k]=false;
- bool flag=true;
- int dir=0,x=0,y=0;
- while(flag)
- {
- if(vis[x][y][dir])
- flag=false;
- else
- {
- if((check(x,y))&&(v[x][y]=='.'))
- {
- vis[x][y][dir]=true;;
- if(dir==0)y++;
- else if(dir==1)x++;
- else if(dir==2)y--;
- else x--;
- }
- else
- {
- if(dir==0)y--;
- else if(dir==1)x--;
- else if(dir==2)y++;
- else x++;
- dir=(dir+1)%4;
- }
- }
- }
- int ans=0;
- for(int i=0;i<n;i++)
- for(int j=0;j<m;j++)
- if(vis[i][j][0]||vis[i][j][1]||vis[i][j][2]||vis[i][j][3])
- ans++;
- cout<<ans<<endl;
- }
- }
- main()
- {
- auto start=chrono::system_clock::now();
- {
- #ifndef ONLINE_JUDGE
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- freopen("error.txt","w",stderr);
- #endif
- ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- int t=1;
- // cin>>t;
- solve(t);
- }
- auto end=chrono::system_clock::now();
- chrono::duration<double> elapsed=end-start;
- // cout<<endl<<"Time taken: "<<elapsed.count()<<" sec";
- return 0;
- }
- /*
- testcase 1:
- 4 5
- ...x.
- .x..x
- x...x
- ..x..
- testcase 2:
- 4 7
- ....X..
- X......
- .....X.
- .......
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement