Submission

Status:
PPPPPPPP

Score: 100

User: admin

Problemset: Sirabyrinth 2

Language: cpp

Time: 0.000 second

Submitted On: 2024-11-29 13:29:36

#include <bits/stdc++.h>
#define pii pair<int, int>

using namespace std;

int N, M, sx, sy;
char a[105][105];
bool vis[105][105] = {0};
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin >> N >> M;
	for (int i=0; i<N; i++) {
		for (int j=0; j<M; j++) {
			cin >> a[i][j];
			if (a[i][j] == 'S') {
				sx = i;
				sy = j;
			}
		}
	}
	
	// Breadth-first search
	
	queue<pii> q;
	q.push({sx, sy});
	int R = 0;
	while (!q.empty()) {
		auto [x, y] = q.front();
		// auto p = q.front();
		// int x = p.first, int y = p.second;
		R+=1;
		q.pop();
		for (int i=0; i<4; i++) {
			int next_x = x+dx[i];
			int next_y = y+dy[i];
			if (next_x < 0 || next_x>=N || next_y < 0 || next_y >= M) {
				continue;
			}
			if (a[next_x][next_y] == '.' && !vis[next_x][next_y]) {
				vis[next_x][next_y] = true;
				q.push({next_x, next_y});
			}
		}
		
	}
	cout << R;
	return 0; 
}