fork download
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. int r,c,castle[50][50];
  5. bool vis[50][50];
  6. vector<int> ncell;
  7. int solve(int x,int y,int c)
  8. {
  9. if(vis[x][y])
  10. return 0;
  11. vis[x][y]=c;
  12. int ret=1;
  13. if(!(castle[x][y]&1))
  14. ret+=solve(x,y-1,c);
  15. if(!(castle[x][y]&2))
  16. ret+=solve(x-1,y,c);
  17. if(!(castle[x][y]&4))
  18. ret+=solve(x,y+1,c);
  19. if(!(castle[x][y]&8))
  20. ret+=solve(x+1,y,c);
  21. ncell.push_back(ret);
  22. return ret;
  23. }
  24. int main()
  25. {
  26. cin>>c>>r;
  27. for(int i=0;i<r;i++)
  28. for(int j=0;j<c;j++)
  29. cin>>castle[i][j];
  30. int roomcnt=0,maxcell=0;
  31. for(int i=0;i<r;i++)
  32. for(int j=0;j<c;j++)
  33. if(!vis[i][j])
  34. {
  35. roomcnt++;
  36. maxcell=max(maxcell,solve(i,j,roomcnt));
  37. }
  38. int two_rooms=0,x=0,y=0,box=0;
  39. char z;
  40. for(int i=0;i<r;i++)
  41. for(int j=0;j<c;j++)
  42. {
  43. if(j)
  44. if(vis[i][j]!=vis[i][j-1])
  45. {
  46. box=ncell[vis[i][j]]+ncell[vis[i][j-1]];
  47. if(box>two_rooms)
  48. {
  49. two_rooms=box;
  50. z='W';
  51. x=i,y=j;
  52. }
  53. }
  54. if(i)
  55. if(vis[i][j]!=vis[i-1][j])
  56. {
  57. box=ncell[vis[i][j]]+ncell[vis[i-1][j]];
  58. if(box>two_rooms)
  59. {
  60. two_rooms=box;
  61. z='N';
  62. x=i,y=j;
  63. }
  64. }
  65. if(j<c-1)
  66. if(vis[i][j]!=vis[i][j+1])
  67. {
  68. box=ncell[vis[i][j]]+ncell[vis[i][j+1]];
  69. if(box>two_rooms)
  70. {
  71. two_rooms=box;
  72. z='E';
  73. x=i,y=j;
  74. }
  75. }
  76. if(i<r-1)
  77. if(vis[i][j]!=vis[i+1][j])
  78. {
  79. box=ncell[vis[i][j]]+ncell[vis[i+1][j]];
  80. if(box>two_rooms)
  81. {
  82. two_rooms=box;
  83. z='S';
  84. x=i,y=j;
  85. }
  86. }
  87. }
  88. cout<<roomcnt<<endl<<maxcell<<endl<<two_rooms<<endl<<x<<" "<<y<<" "<<z<<endl;
  89. return 0;
  90. }
  91.  
Success #stdin #stdout 0s 15240KB
stdin
7 4
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13
stdout
5
9
0
0 0 �