fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int B, D;
  9. cin >> B >> D;
  10. vector<string> shape(D);
  11. for (int j = 0; j < D; j++) cin >> shape[j];
  12.  
  13. int W, H;
  14. cin >> W >> H;
  15. vector<string> park(H);
  16. for (int y = 0; y < H; y++) cin >> park[y];
  17.  
  18. // UFO가 차지하는 상대 좌표
  19. vector<pair<int,int>> cells;
  20. for (int j=0;j<D;j++) for (int i=0;i<B;i++)
  21. if (shape[j][i]=='O') cells.push_back({i,j});
  22.  
  23. // 결과 지도
  24. vector<string> result = park;
  25.  
  26. // 간단한 배치 전략: (B+1, D+1) 간격으로 타일링 시도
  27. for (int y=0; y+D<=H; y+=(D+1)){
  28. for (int x=0; x+B<=W; x+=(B+1)){
  29. bool ok=true;
  30. for (auto [dx,dy]:cells){
  31. int gx=x+dx, gy=y+dy;
  32. if (park[gy][gx] != '.') { ok=false; break; }
  33. // 인접에 이미 UFO 있는지 확인
  34. static int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
  35. for(auto &d:dir){
  36. int nx=gx+d[0], ny=gy+d[1];
  37. if(0<=nx && nx<W && 0<=ny && ny<H){
  38. if(result[ny][nx]=='O'){ ok=false; break; }
  39. }
  40. }
  41. if(!ok) break;
  42. }
  43. if(ok){
  44. for (auto [dx,dy]:cells){
  45. int gx=x+dx, gy=y+dy;
  46. result[gy][gx]='O';
  47. }
  48. }
  49. }
  50. }
  51.  
  52. for(int y=0;y<H;y++) cout<<result[y]<<"\n";
  53. }
Success #stdin #stdout 0s 5324KB
stdin
4 3
O..O
OOOO
.O..
20 10
....................
.w......w.......ww..
....ww......w.w.....
.w.....w............
...w......w.........
w................ww.
....w...............
.w.....w........w.ww
............w.......
....w..............w
stdout
..........O..O......
.w......w.OOOO..ww..
....ww.....Ow.w.....
.w.....w............
...w.O..O.w.........
w....OOOO........ww.
....w.O.............
.w.....w........w.ww
............w.......
....w..............w