Maruf_Hasan

crystal last

Feb 2nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define M 1000
  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.  
  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 && cell[tx][ty]==1)
  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. cell[i][j]=-1;
  68. }
  69. else
  70. {
  71. cell[i][j]=1;
  72. }
  73. adj[i].push_back(c);
  74.  
  75. }
  76. }
  77. //getchar();
  78. //for(i=0;i<m;i++)
  79. //{
  80. // for(j=0;j<n;j++)
  81. // {
  82. // cout<<adj[i][j]<<" ";
  83. // }
  84. // cout<<endl;
  85. // }
  86.  
  87.  
  88. printf("Case %d:\n",kase+1);
  89. while(q--)
  90. {
  91. int x,y;
  92. scanf("%d %d",&x,&y);
  93. int ans=bfs(x-1,y-1);
  94. for(i=0;i<m;i++)
  95. {
  96. for(j=0;j<n;j++)
  97. {
  98. if(cell[i][j]==-1)
  99. visited[i][j]=true;
  100. else
  101. visited[i][j]=false;
  102. }
  103. }
  104. printf("%d\n",ans);
  105. }
  106.  
  107. for(i=0;i<M;i++)
  108. {
  109. adj[i].clear();
  110. }
  111. memset(visited,false,sizeof visited);
  112. memset(cell,0,sizeof cell);
  113.  
  114. kase++;
  115. }
  116.  
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment