a53

Acces2

a53
May 10th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cassert>
  4. using namespace std;
  5.  
  6. ifstream fin("acces2.in");
  7. ofstream fout("acces2.out");
  8.  
  9. int n , m;
  10. short a[1001][1001];
  11. int x[1000001], y[1000001];
  12. char P[1001][1001];
  13. int nrechipe;
  14.  
  15. const int dx[] = {0 , 0 , 1 , -1},
  16. dy[] = {1 , -1 , 0 , 0};
  17.  
  18. void fill(int istart , int jstart , int nrz)
  19. {
  20. //parcurgem zona curenta si determinam cate echipe sunt in ea
  21. int st = 1 , dr = 1;
  22. a[istart][jstart] = nrz;
  23. x[1] = istart, y[1] = jstart;
  24. nrechipe = 0;
  25. while(st <= dr)
  26. {
  27. int i = x[st], j = y[st];
  28. for(int k = 0 ; k < 4 ; k ++)
  29. {
  30. int ii = i + dx[k], jj = j + dy[k];
  31. if( ii > 0 && ii <= n && jj > 0 && jj <= m && P[ii][jj] != '#' && a[ii][jj] == 0)
  32. {
  33. dr ++;
  34. x[dr] = ii, y[dr] = jj;
  35. a[ii][jj] = nrz;
  36. }
  37. }
  38. if(P[i][j] == 'P')
  39. nrechipe ++;
  40. st ++;
  41. }
  42. }
  43.  
  44. int main(){
  45. fin >> n >> m;
  46. for(int i = 1 ; i <= n ; i ++)
  47. for(int j = 1 ; j <= m ; j ++)
  48. fin >> P[i][j];
  49. fin.close();
  50.  
  51. int nrzone = 0 , echipe_in_plus = 0;
  52. int nrz = 0;
  53. for(int i = 1 ; i <= n ; i ++)
  54. for(int j = 1 ; j <= m ; j ++)
  55. if(a[i][j] == 0 && P[i][j] != '#')
  56. {
  57. nrz ++;
  58. fill(i, j, nrz);
  59. if(nrechipe == 0)
  60. {
  61. nrzone ++;
  62. }
  63. else
  64. echipe_in_plus += nrechipe - 1;
  65. }
  66.  
  67. if(echipe_in_plus >= nrzone)
  68. fout << nrzone;
  69. else
  70. fout << echipe_in_plus + 2 * (nrzone - echipe_in_plus);
  71. fout.close();
  72. return 0;
  73. }
Add Comment
Please, Sign In to add comment