Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- gradle
- 자바
- designpattern
- java
- springboot
- @ModelAttribute
- C
- createQuery
- 데코레이터패턴
- 10844
- Spring
- pscp
- @NotEmpty
- @Spring
- mycp
- decorator
- 여러인수
- Linux
- 숫자야구
- 10951
- 디자인패턴
- 전치행렬 #C
- NamedParameterNotBound
- setParameter
- BubbleSorting
- 백준
- 쉬운 계단 수
- junit
- 점세개
- 자료구조
Archives
- Today
- Total
...
[C++] 1303 전쟁 - 전투 본문
1303번: 전쟁 - 전투
첫째 줄에는 전쟁터의 가로 크기 N, 세로 크기 M(1 ≤ N, M ≤ 100)이 주어진다. 그 다음 두 번째 줄에서 M+1번째 줄에는 각각 (X, Y)에 있는 병사들의 옷색이 띄어쓰기 없이 주어진다. 모든 자리에는
www.acmicpc.net
BFS 문제로, 간단한 탐색 문제이다. 행과 열을 2차원 배열로 받아 B, W 각각 너비우선탐색 후 모여있는 병사들의 값을 제곱해 더하고 결과를 내주면 된다. 진짜 간단한 문제라 해결책 찾는 건 어렵지 않았는데 자꾸 13%에서 죽어버려서 거의 2시간은 헤맨 거 같다ㅜㅜ
결과적으론 문제 제대로 안 읽어서 이렇게 헤맸던 것... 문제를 자세히 보면 가로 길이가 N, 세로 길이가 M. 즉 행이 M 열이 N인데 보통 대부분의 문제들에서 먼저 오는 값이 행, 나중에 오는 값을 열로 입력 받다보니 습관처럼 이렇게 받았다. 하필 예제가 N=M이라 틀린 곳 찾는 데 더 오래걸렸다. (결론 = 문제를 꼼꼼히 읽자..)
[해결답안]
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
int N,M;
char arr[101][101];
int visited[101][101] = {0,};
queue<pair<int,int>> q; //white team
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
int blue = 0; int white = 0;
void BFS(char c){
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
if(!visited[i][j]){
if(arr[i][j]==c){
visited[i][j] = 1;
int cnt = 0;
q.push(make_pair(i,j));
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
cnt++;
for(int k=0; k<4; k++){
int mx = x+dx[k]; int my = y+dy[k];
if (mx < 0 || mx >= N || my < 0 || my >= M)
continue;
if(arr[mx][my]==c&&!visited[mx][my]){
q.push(make_pair(mx,my));
visited[mx][my] = 1;
}
}
}
if(c=='W') white += (cnt*cnt);
else if(c=='B') blue += (cnt*cnt);
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>M>>N;
for(int i=0; i<N; i++){
string s;
cin>>s;
for(int j=0; j<M; j++){
arr[i][j] = s[j];
}
}
BFS('W');
BFS('B');
cout<<white<<" "<<blue;
}
'백준' 카테고리의 다른 글
[C++] 13023 ABCDE (0) | 2024.01.23 |
---|---|
[C++] 10972 다음 순열 (0) | 2023.11.16 |
[C++] 15663 : N과 M(9) (1) | 2023.11.15 |
C++ 백준 1107 리모컨 (1) | 2023.11.08 |
백준 JAVA 10844 : 쉬운 계단 수 (363) | 2022.03.25 |
Comments