fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef vector<string> vs;
  4. typedef vector<vector<int>> vvi;
  5. typedef vector<list<int>> vli;
  6. typedef vector<int> vi;
  7. typedef pair<int,int> pii;
  8. typedef vector<string> vs;
  9. typedef vector<bool> vb;
  10. typedef long long ll;
  11. typedef double db;
  12. typedef priority_queue<int> pq;
  13. typedef vector<vector<pii>> vvp;
  14. typedef vector<pii> vpi;
  15. #define fi first
  16. #define se second
  17. #define FOR(i,s,e,d) for(int i=s;i<e;i+=d)
  18. #define FORL(i,s,e,d) for(ll i=s;i<e;i+=d)
  19. #define RFOR(i,s,e,d) for(int i=s;i>=e;i+=d)
  20. #define RFORL(i,s,e,d) for(ll i=s;i>=e;i+=d)
  21. int n;
  22. int dir[][2]{{1,0},{-1,0},{0,1},{0,-1}};
  23. char cmap[101][101]; // (0,0)부터
  24. bool visit[101][101];
  25. void ndfs(int y, int x){
  26. visit[y][x]=true;
  27. FOR(i,0,4,1){
  28. int nexty=y+dir[i][0];
  29. int nextx=x+dir[i][1];
  30. if(nexty>=0&&nexty<n&&nextx>=0&&nextx<n&&!visit[nexty][nextx]&&cmap[nexty][nextx]==cmap[y][x])
  31. ndfs(nexty,nextx);
  32. }
  33. }
  34. void ddfs(int y, int x){
  35. visit[y][x]=true;
  36. FOR(i,0,4,1){
  37. int nexty=y+dir[i][0];
  38. int nextx=x+dir[i][1];
  39. if(nexty>=0&&nexty<n&&nextx>=0&&nextx<n&&!visit[nexty][nextx]){
  40. if((cmap[y][x]=='R'||cmap[y][x]=='G')&&(cmap[nexty][nextx]=='R'||cmap[nexty][nextx]=='G'))
  41. ndfs(nexty,nextx);
  42. else if(cmap[y][x]==cmap[nexty][nextx])
  43. ndfs(nexty,nextx);
  44. }
  45. }
  46. }
  47. int main(){
  48. cin.tie(NULL);
  49. cout.tie(NULL);
  50. ios_base::sync_with_stdio(false);
  51. cin>>n;
  52. FOR(i,0,n,1){
  53. string in;
  54. cin>>in;
  55. FOR(j,0,n,1)
  56. cmap[i][j]=in[j];
  57. }
  58. // 정상인
  59. FOR(i,0,n,1)
  60. fill(visit[i],visit[i]+n,false);
  61. int nret=0;
  62. FOR(i,0,n,1)
  63. FOR(j,0,n,1)
  64. if(!visit[i][j]){
  65. nret++;
  66. ndfs(i,j);
  67. }
  68. // 색맹
  69. FOR(i,0,n,1)
  70. fill(visit[i],visit[i]+n,false);
  71. int dret=0;
  72. FOR(i,0,n,1)
  73. FOR(j,0,n,1)
  74. if(!visit[i][j]){
  75. dret++;
  76. ddfs(i,j);
  77. }
  78. cout<<nret<<" "<<dret<<'\n';
  79. return 0;
  80. }
Success #stdin #stdout 0s 15256KB
stdin
5
RRRBB
GGBBB
BBBRR
BBRRR
RRRRR
stdout
4 3