답안 #1062365

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1062365 2024-08-17T04:13:41 Z danielzhu Tracks in the Snow (BOI13_tracks) C++17
100 / 100
437 ms 28312 KB
#include <bits/stdc++.h>
using namespace std;
#define MX 4001
typedef long long LL;
typedef pair<int, int> PR;
typedef tuple<int, int, int> TP;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 5;
const LL MAXV = (LL)1e18; 
using vi = vector<int>;
using vii = vector<vi>;

int H, W;
string md[MX]; //snow meadow
//idea: the footprints (not all) of an animal can be covered by all its successors' footprints.
//Also can be solved using Disjoint Sets instead of BFS/DFS?  
int solve() {
	cin >> H >> W;
	//read the snow meadow info
	for (int i = 0; i < H; i++)
		cin >> md[i]; 
	queue<PR> Q[2]; //two queues for BFS responding to R and F
	int ans = 0; 
  if (md[0][0] == '.')
    return ans;
  vector<char> animal(1, md[0][0]);
  animal.push_back(md[0][0] == 'F' ? 'R' : 'F'); //alternate animal's footprints to discover
  int cur = 0; //an index of animial to reveal the current animal in backward order
  Q[cur].push({0, 0});
	md[0][0] = 'X'; //visited 
  while (!Q[cur].empty()) {
		ans++;
		//BFS to find the CC for current animal's footprints
		while (!Q[cur].empty()) {
			int r = Q[cur].front().first, c = Q[cur].front().second;
			Q[cur].pop();
			int dr = 0, dc = 1;
			for (int k = 0; k < 4; k++) { //4 directions
				int nr = r + dr, nc = c + dc;
				if (nr >= 0 && nr < H && nc >= 0 && nc < W && md[nr][nc] != '.' && md[nr][nc] != 'X') {
					if (md[nr][nc] == animal[cur]) { //current animial footprint
						Q[cur].push({nr, nc});
					} else { //immediately preceding animial footprint
						Q[cur^1].push({nr, nc}); //different use
						// Or you can do: Q[1-cur].emplace_back(nr, nc);
					}
					md[nr][nc] = 'X'; //visited
				}
				//the following 2 statements for iterating thru 4 directions
				// you can use: int dr[] = {0, 1, 0, -1}, dc[] = {1, 0, -1, 0};
				swap(dr, dc);
				dc = -dc;
			}
		}
		cur ^= 1; //for immediately preceding animal. or cur = 1 - cur;
	}	
	return ans;
}   


int main() {
	ios::sync_with_stdio(false);
  cin.tie(nullptr);
	cin.exceptions(cin.failbit);
  cout << solve() << endl;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 860 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 4 ms 816 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 6 ms 860 KB Output is correct
16 Correct 7 ms 856 KB Output is correct
17 Correct 5 ms 604 KB Output is correct
18 Correct 4 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 27 ms 2140 KB Output is correct
3 Correct 137 ms 18012 KB Output is correct
4 Correct 34 ms 4444 KB Output is correct
5 Correct 76 ms 10076 KB Output is correct
6 Correct 436 ms 28312 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 27 ms 2140 KB Output is correct
14 Correct 15 ms 1372 KB Output is correct
15 Correct 7 ms 1388 KB Output is correct
16 Correct 13 ms 1368 KB Output is correct
17 Correct 71 ms 4704 KB Output is correct
18 Correct 30 ms 4700 KB Output is correct
19 Correct 30 ms 4444 KB Output is correct
20 Correct 29 ms 4188 KB Output is correct
21 Correct 76 ms 10604 KB Output is correct
22 Correct 77 ms 10076 KB Output is correct
23 Correct 135 ms 9048 KB Output is correct
24 Correct 70 ms 10332 KB Output is correct
25 Correct 148 ms 18224 KB Output is correct
26 Correct 211 ms 13916 KB Output is correct
27 Correct 301 ms 19860 KB Output is correct
28 Correct 437 ms 28232 KB Output is correct
29 Correct 434 ms 26568 KB Output is correct
30 Correct 388 ms 25624 KB Output is correct
31 Correct 351 ms 12052 KB Output is correct
32 Correct 274 ms 18772 KB Output is correct