Advertisement
a53

castel3

a53
Feb 19th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define pii pair<int,int>
  3. #define mp make_pair
  4. #define nmax 155
  5. #define nmax2 25555
  6. using namespace std;
  7. int n,m,k,nr,l,c,ans,ii,jj,nl,nc;
  8. short a[nmax][nmax],num[nmax][nmax];
  9. bool visited[nmax][nmax],haveKey[nmax2],checked[nmax][nmax];
  10. vector<pii> notEntered[nmax2];
  11. int dl[]={-1,0,1,0},dc[]={0,1,0,-1};
  12.  
  13. bool interior(int i,int j)
  14. {
  15. return i>=1&&j>=1&&i<=n&&j<=m;
  16. }
  17.  
  18. void filll(int l,int c){
  19. queue<pii>q;
  20. pii act;
  21. int i,j;
  22. visited[l][c]=true;
  23. haveKey[num[l][c]]=true;
  24. q.push(mp(l, c));
  25. while(!q.empty())
  26. {
  27. act=q.front();q.pop();
  28. i=act.first,j=act.second;
  29. for(unsigned int t=0;t<notEntered[num[i][j]].size();++t)
  30. {
  31. nl=notEntered[num[i][j]][t].first;
  32. nc=notEntered[num[i][j]][t].second;
  33. haveKey[num[nl][nc]] = true;
  34. if(!visited[nl][nc])
  35. visited[nl][nc]=true,q.push(mp(nl,nc));
  36. }
  37. for(int k=0;k<4;++k)
  38. {
  39. ii=i+dl[k];
  40. jj=j+dc[k];
  41. if(!interior(ii,jj))
  42. continue;
  43. if(haveKey[a[ii][jj]])
  44. {
  45. haveKey[num[ii][jj]]=true;
  46. if(!visited[ii][jj])
  47. q.push(mp(ii,jj)),visited[ii][jj]=true;
  48. }
  49. else
  50. {
  51. if(!checked[ii][jj])
  52. checked[ii][jj]=true,notEntered[a[ii][jj]].push_back(mp(ii,jj));
  53. }
  54. }
  55. }
  56. }
  57.  
  58. int main()
  59. {
  60. freopen("castel.in","r",stdin);
  61. freopen("castel.out","w",stdout);
  62. cin>>n>>m>>k;
  63. for(int i=1;i<=n;++i)
  64. for(int j=1;j<=m;++j)
  65. {
  66. cin>>a[i][j];
  67. num[i][j]=++nr;
  68. if(nr==k)
  69. l=i,c =j;
  70. }
  71. filll(l,c);
  72. for(int i=1;i<=n;++i)
  73. for(int j=1;j<=m;++j)
  74. if(visited[i][j])
  75. ++ans;
  76. printf("%d\n",ans);
  77. fclose(stdin);
  78. fclose(stdout);
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement