Advertisement
Saleh127

Light oj(LO) 1337

Feb 3rd, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. ///LO 1337
  2. ///idea: pre calculation of crystals reachable from each cell
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #define ll long long
  6. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  7. bool v[505][505],v1[505][505];
  8. char a[505][505];
  9. ll xx[505][505];
  10. ll n,m,x,y,i,j,k,l,q,ans;
  11.  
  12. void pre(ll x,ll y,ll c)
  13. {
  14. if(v1[x][y]==1 || x<0 || x==m || y<0 || y==n || a[x][y]=='#') return;
  15. v1[x][y]=1;
  16. xx[x][y]=c;
  17. pre(x+1,y,c);
  18. pre(x-1,y,c);
  19. pre(x,y+1,c);
  20. pre(x,y-1,c);
  21. }
  22. void dfs(ll x,ll y)
  23. {
  24. if(v[x][y]==1 || x<0 || x==m || y<0 || y==n || a[x][y]=='#') return;
  25. v[x][y]=1;
  26. if(a[x][y]=='C') ans++;
  27. dfs(x+1,y);
  28. dfs(x-1,y);
  29. dfs(x,y+1);
  30. dfs(x,y-1);
  31. }
  32.  
  33. int main()
  34. {
  35. ios_base::sync_with_stdio(0);
  36. cin.tie(0);
  37. cout.tie(0);
  38.  
  39. test
  40. {
  41. cin>>m>>n>>q;
  42. for(i=0; i<m; i++)
  43. {
  44. for(j=0; j<n; j++)
  45. {
  46. cin>>a[i][j];
  47. }
  48. }
  49.  
  50. memset(v,0,sizeof(v));
  51. memset(v1,0,sizeof(v1));
  52.  
  53. for(i=0; i<m; i++)
  54. {
  55. for(j=0; j<n; j++)
  56. {
  57. ans=0;
  58. if(v[i][j]==0)
  59. {
  60. dfs(i,j);
  61. pre(i,j,ans);
  62. }
  63. }
  64. }
  65.  
  66. cout<<"Case "<<cs<<":"<<endl;
  67. while(q--)
  68. {
  69. cin>>x>>y;
  70. x--,y--;
  71. cout<<xx[x][y]<<endl;
  72.  
  73. }
  74. }
  75.  
  76.  
  77. return 0;
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement