알고리즘 공부
백준 현수막
컴퓨터과학
2023. 7. 30. 20:54
https://www.acmicpc.net/problem/14716
14716번: 현수막
혁진이의 생각대로 프로그램을 구현했을 때, 현수막에서 글자의 개수가 몇 개인지 출력하여라.
www.acmicpc.net
쉽네요
#include <iostream>
#include <vector>
#include <queue>
#include<string>
#include<algorithm>
#include<cmath>
#include<unordered_map>
#include<map>
#include<stack>
using namespace std;
struct Point {
int i, j, t, cnt, power;
char c;
};
void Prints(vector<vector<int>>maps, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void Prints(vector<vector<bool>>maps, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[i][j]<<" ";
}
cout << endl;
}
cout << endl;
}
void Prints(vector<vector<char>>maps, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[i][j];
}
cout << endl;
}
cout << endl;
}
void Prints(vector<vector<vector<char>>>maps, int n, int m, int k) {
for (int t = 0; t < k; t++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[t][i][j];
}
cout << endl;
}
cout << endl;
}
cout << endl;
}
void Prints(vector<vector<vector<bool>>>maps, int n, int m, int k) {
for (int t = 0; t < k; t++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[t][i][j];
}
cout << endl;
}
cout << endl;
}
cout << endl;
}
void Prints(string str, int n) {
for (int i = 0; i < n; i++) {
cout << str[i] << ",";
}
cout << endl;
}
struct Data {
unsigned long long index;
unsigned long long value;
};
int main() {
int n, m;
cin >> m >> n;
//m<=1
//n<=250
vector<vector<int>>maps;
vector<bool>tmpbVisited(n,false);
vector<vector<bool>>bVisited(m, tmpbVisited);
for (int i = 0; i < m; i++) {
vector<int>tmpMaps;
for (int j = 0; j < n; j++) {
int tmp;
cin >> tmp;
tmpMaps.push_back(tmp);
if (tmp == 1) {
bVisited[i][j] = true;
}
}
maps.push_back(tmpMaps);
}
//Prints(maps, m, n);
int diri[] = { 1,-1,0,0, 1,1,-1,-1};
int dirj[] = { 0,0,1,-1, 1,-1,1,-1 };
int gCount = 0;
for (int t = 0; t < m; t++) {
for (int k = 0; k < n; k++) {
if (bVisited[t][k] == true) {
Point p;
p.i = t;
p.j = k;
queue<Point>q;
q.push(p);
bVisited[t][k] = false;
gCount++;
while (!q.empty()) {
Point start = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
int tmpDiri = start.i + diri[i];
int tmpDirj = start.j + dirj[i];
if (tmpDiri == -1 || tmpDirj == -1 || tmpDiri >= m || tmpDirj >= n) {
continue;
}
if (bVisited[tmpDiri][tmpDirj] == false) {
continue;
}
bVisited[tmpDiri][tmpDirj] = false;
Point tmpP;
tmpP.i = tmpDiri;
tmpP.j = tmpDirj;
q.push(tmpP);
//Prints(bVisited, m, n);
}
}
}
}
}
cout << gCount;
return 0;
}