답안 #535534

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535534 2022-03-10T13:02:19 Z Koful123 Tracks in the Snow (BOI13_tracks) C++17
100 / 100
838 ms 890684 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define endl "\n"
#define mod 1000000007
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

const int N = 4005;
int vis[N][N],cnt,n,m,ans;
string s[N];
queue<pair<int,int>> q,tmp;

void dfs(int x,int y,char c){
	if(x >= n || y >= m || y < 0 || x < 0) return;
	if(vis[x][y]) return;
	if(!vis[x][y] && s[x][y] != c){
		tmp.push({x,y});
		return;
	}

	if(!vis[x][y]){
		vis[x][y] = 1;
		cnt++;
	}

	dfs(x+1,y,c);
	dfs(x,y+1,c);
	dfs(x,y-1,c);
	dfs(x-1,y,c);
}

void solve(){		

	cin >> n >> m;

	for(int i=0;i<n;i++)
		cin >> s[i];

	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(s[i][j] == '.'){
				vis[i][j] = 1;
				cnt++;
			}
		}
	}

	char c = s[n-1][m-1];
	q.push({n-1,m-1});

	while(cnt != n*m){
		while(q.size()){
			auto tp = q.front();
			dfs(tp.ff,tp.ss,c);
			q.pop();
		}

		while(tmp.size()){
			q.push(tmp.front());
			tmp.pop();
		}

		if(c == 'F') c = 'R';
		else c = 'F';
		ans++;
	}

	cout << ans << endl;
}

signed main(){

	ios_base::sync_with_stdio(0);	
	cin.tie(0);	

	#ifndef ONLINE_JUDGE
	//	freopen("in.txt","r",stdin);
	//	freopen("out.txt","w",stdout);
	#endif
 
	int t = 1;
	//cin >> t;

 	for(int i=1;i<=t;i++){
 		solve();
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 4820 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 10 ms 5356 KB Output is correct
5 Correct 4 ms 2388 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 3 ms 2004 KB Output is correct
11 Correct 3 ms 2388 KB Output is correct
12 Correct 5 ms 2516 KB Output is correct
13 Correct 2 ms 2388 KB Output is correct
14 Correct 3 ms 2388 KB Output is correct
15 Correct 12 ms 4840 KB Output is correct
16 Correct 15 ms 4808 KB Output is correct
17 Correct 8 ms 4564 KB Output is correct
18 Correct 9 ms 5428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 16336 KB Output is correct
2 Correct 50 ms 19584 KB Output is correct
3 Correct 238 ms 143916 KB Output is correct
4 Correct 78 ms 41560 KB Output is correct
5 Correct 248 ms 93008 KB Output is correct
6 Correct 791 ms 261772 KB Output is correct
7 Correct 8 ms 16980 KB Output is correct
8 Correct 8 ms 16352 KB Output is correct
9 Correct 2 ms 1364 KB Output is correct
10 Correct 1 ms 1016 KB Output is correct
11 Correct 8 ms 16728 KB Output is correct
12 Correct 1 ms 1236 KB Output is correct
13 Correct 45 ms 19584 KB Output is correct
14 Correct 28 ms 12368 KB Output is correct
15 Correct 19 ms 13588 KB Output is correct
16 Correct 30 ms 8120 KB Output is correct
17 Correct 140 ms 45088 KB Output is correct
18 Correct 78 ms 44328 KB Output is correct
19 Correct 69 ms 41652 KB Output is correct
20 Correct 58 ms 38308 KB Output is correct
21 Correct 187 ms 96256 KB Output is correct
22 Correct 206 ms 92988 KB Output is correct
23 Correct 232 ms 80112 KB Output is correct
24 Correct 158 ms 94444 KB Output is correct
25 Correct 425 ms 144048 KB Output is correct
26 Correct 696 ms 890684 KB Output is correct
27 Correct 838 ms 779984 KB Output is correct
28 Correct 801 ms 261536 KB Output is correct
29 Correct 772 ms 281468 KB Output is correct
30 Correct 725 ms 368720 KB Output is correct
31 Correct 655 ms 106896 KB Output is correct
32 Correct 647 ms 484440 KB Output is correct