madidino

10kindsofmadi

Aug 3rd, 2022 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. int a[1000][1000],b[1000][1000];
  5. int dx[]= {1,-1,0,0};
  6. int dy[]= {0,0,1,-1};
  7. void dfs(int r,int c,int x,int y,int col,int val)
  8. {
  9. b[x][y]=col;
  10. for(int i=0; i<4; i++)
  11. {
  12. int xx=x+dx[i];
  13. int yy=y+dy[i];
  14. if(xx >= 0 && yy >= 0 && xx < r && yy < c && a[xx][yy] == val && b[xx][yy] == -1)
  15. {
  16. dfs(r,c,xx,yy,col,val);
  17. }
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. int r,c;
  24. cin>>r>>c;
  25. for(int i=0; i<r; i++)
  26. {
  27. for(int j=0; j<c; j++)
  28. {
  29. char cr;cin>>cr;a[i][j]=cr-'0';//explicatie???
  30. b[i][j]=-1;
  31. }
  32. }
  33. int col(2);
  34. for(int i=0; i<r; i++)
  35. {
  36. for(int j=0; j<c; j++)
  37. {
  38. if(b[i][j]==-1)
  39. {
  40. dfs(r,c,i,j,col,a[i][j]);
  41. col++;
  42. }
  43. }
  44. }
  45. // int n;cin>>n;
  46. // for(int i=0;i<n;i++)
  47. // {
  48. // int x1,x2,y1,y2;cin>>x1>>y1>>x2>>y2;
  49. //// if(b[x1][y1]!=b[x2][y2])
  50. //// {
  51. //// cout<<"neither";
  52. //// }
  53. //// else
  54. //// {
  55. //// //cum caut eficient ca i primul si ultimul?
  56. ////
  57. //// else
  58. //// {
  59. //// cout<<"decimal";
  60. //// }
  61. //// }
  62. // }
  63. for (int i = 0; i < r; i++)
  64. {
  65. for (int j = 0; j < c; j++)
  66. {
  67. cout << b[i][j] << " ";
  68. }
  69. cout << endl;
  70. }
  71. return 0;
  72. }
  73.  
Add Comment
Please, Sign In to add comment