Maruf_Hasan

crystallllllllll mara khaisi

Feb 2nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define M 505
  4. vector<char>adj[M];
  5. bool visited[M][M];
  6. int cell[M][M];
  7. int fx[]={1,-1,0,0};
  8. int fy[]={0,0,1,-1};
  9. int m,n;
  10. int mark[M][M];
  11. int bfs(int x,int y)
  12. {
  13. visited[x][y]=true;
  14. pair<int,int>p,p1;
  15. int count=0;
  16. queue<pair<int,int> >q;
  17. q.push(make_pair(x,y));
  18. if(adj[x][y]=='C')
  19. count++;
  20. //dis[x][y]=1;
  21. else
  22. count=0;
  23. //dis[x][y]=0;
  24. while(!q.empty())
  25. {
  26. p=q.front();
  27. q.pop();
  28. for(int i=0;i<4;i++)
  29. {
  30. int tx=p.first+fx[i];
  31. int ty=p.second+fy[i];
  32. if(tx>=0 && tx<m && ty>=0 && ty<n && visited[tx][ty]==false && mark[tx][ty]==0)
  33. {
  34. if(adj[tx][ty]=='C'){
  35. count++;
  36. //dis[tx][ty]=dis[p.first][p.second]+1;
  37. }
  38. visited[tx][ty]=true;
  39. q.push(make_pair(tx,ty));
  40. }
  41. }
  42. }
  43.  
  44. return count;
  45. }
  46.  
  47.  
  48. int main()
  49. {
  50. int t,kase=0;
  51. scanf("%d",&t);
  52. while(kase<t)
  53. {
  54. int q,i,j;
  55. //char c;
  56. scanf("%d %d %d",&m,&n,&q);
  57. for(i=0;i<m;i++)
  58. {
  59. getchar();
  60. for(j=0;j<n;j++)
  61. {
  62. char c;
  63. scanf(" %c",&c);
  64. //getchar();
  65. if(c=='#'){
  66. //visited[i][j]=true;
  67. mark[i][j]=1;
  68. //cell[i][j]=-1;
  69. }
  70. else
  71. {
  72. mark[i][j]=0;
  73. //cell[i][j]=1;
  74. //visited[i][j]=false;
  75. }
  76. adj[i].push_back(c);
  77.  
  78. }
  79. }
  80. //getchar();
  81. //for(i=0;i<m;i++)
  82. //{
  83. // for(j=0;j<n;j++)
  84. // {
  85. // cout<<adj[i][j]<<" ";
  86. // }
  87. // cout<<endl;
  88. // }
  89.  
  90.  
  91. printf("Case %d:\n",kase+1);
  92. int ans;
  93.  
  94. while(q--)
  95. {
  96. int x,y;
  97. scanf("%d %d",&x,&y);
  98. if(cell[x-1][y-1]!=0)
  99. {
  100. ans=cell[x-1][y-1];
  101. }
  102. else
  103. {
  104. memset(visited,false,sizeof visited);
  105. memset(cell,0,sizeof cell);
  106.  
  107. ans=bfs(x-1,y-1);
  108.  
  109.  
  110. for(i=0;i<m;i++)
  111. {
  112. for(j=0;j<n;j++)
  113. {
  114. if(visited[i][j]==true)
  115. {
  116. cell[i][j]=ans;
  117.  
  118. }
  119. }
  120. }
  121. }
  122. printf("%d\n",ans);
  123. }
  124.  
  125. for(i=0;i<M;i++)
  126. {
  127. adj[i].clear();
  128. }
  129. memset(mark,0,sizeof mark);
  130. //memset(visited,false,sizeof visited);
  131. memset(cell,0,sizeof cell);
  132.  
  133. kase++;
  134. }
  135.  
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment