QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#33309#4231. Rafting Tripflower_and_qingyu#WA 3ms3644kbC++142.9kb2022-05-31 11:07:132022-05-31 11:07:15

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-31 11:07:15]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3644kb
  • [2022-05-31 11:07:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int main() {
	// freopen("in.txt", "r", stdin);
	int n, m;
	cin >> n >> m;
	vector<string> a(n);
	vector<vector<pair<int, int>>> G(n, vector<pair<int, int>> (m));
	vector<vector<vector<pair<int, int>>>> E(n, vector<vector<pair<int, int>>> (m));
	vector<vector<int>> deg(n, vector<int> (m));
	for(int i = 0; i < n; i ++) cin >> a[i];
	map<char, pair<int, int>> mp;
	mp['^'] = {-1, 0};
	mp['<'] = {0, -1};
	mp['>'] = {0, 1};
	mp['v'] = {1, 0};
	constexpr int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
	for(int i = 0; i < n; i ++) {
		for(int j = 0; j < m; j ++) {
			if(mp.count(a[i][j])) {
				G[i][j] = {i + mp[a[i][j]].first, j + mp[a[i][j]].second};
				if(G[i][j].first >= 0 && G[i][j].first < n && G[i][j].second >= 0 && G[i][j].second < m) {
					deg[G[i][j].first][G[i][j].second] ++;
				}
			}
		}
	}
	queue<pair<int, int>> q;
	for(int i = 0; i < n; i ++) for(int j = 0; j < m; j ++) if(mp.count(a[i][j]) && !deg[i][j]) {
		q.emplace(i, j);
	}
	while(!q.empty()) {
		auto [x, y] = q.front(); q.pop();
		if(!mp.count(a[x][y])) continue;
		// cout << x << ' ' << y << ' ' << deg[x][y] << '\n';;
		if(G[x][y].first >= 0 && G[x][y].first < n && G[x][y].second >= 0 && G[x][y].second < m) {
			E[G[x][y].first][G[x][y].second].emplace_back(x, y);
			if(-- deg[G[x][y].first][G[x][y].second] == 0) {
				q.emplace(G[x][y]);
			}
		}
	}
	vector<vector<int>> vis(n, vector<int> (m)), use(n, vector<int> (m));
	int ans = 0, curans = 0;
	auto dfs = [&] (auto &self, int x, int y) -> void {
		vector<pair<int, int>> del;
		for(int c = 0; c < 4; c ++) {
			auto dx = x + d[c][0], dy = y + d[c][1];
			if(dx >= 0 && dx < n && dy >= 0 && dy < m && 
				a[dx][dy] == '#' && !use[dx][dy]) curans ++, use[dx][dy] = 1, del.emplace_back(dx, dy);
		}
		ans = max(ans, curans);
		for(auto [dx, dy] : E[x][y]) {
			self(self, dx, dy);
		}
		for(auto [dx, dy] : del) {
		curans --;
			use[dx][dy] = 0;
		}
	};
	for(int i = 0; i < n; i ++) {
		for(int j = 0; j < m; j ++) if(deg[i][j] && !vis[i][j]) {
			int curans = 0;
			pair<int, int> u = {i, j};
			vector<pair<int, int>> cur, c;
			while(!vis[u.first][u.second]) {
				vis[u.first][u.second] = 1;
				for(int c = 0; c < 4; c ++) {
					auto dx = u.first + d[c][0], dy = u.second + d[c][1];
					if(dx >= 0 && dx < n && dy >= 0 && dy < m && 
						a[dx][dy] == '#' && !use[dx][dy]) cur.emplace_back(dx, dy), curans ++, use[dx][dy] = 1;
				}
				c.emplace_back(u);
				u = G[u.first][u.second];
			}
			for(auto u : c) {
				dfs(dfs, u.first, u.second);
			}
			for(auto [x, y] : cur) use[x][y] = 0;
		}
	}
	for(int i = 0; i < n; i ++) for(int j = 0; j < m; j ++) {
		if(mp.count(a[i][j]) && (G[i][j].first < 0 || G[i][j].first >= n || 
			G[i][j].second < 0 || G[i][j].second >= m || !mp.count(a[G[i][j].first][G[i][j].second]))) {
			dfs(dfs, i, j);
		}
	}
	cout << ans << '\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3644kb

input:

5 6
v<<<#v
v#v<.>
>>v<<<
..v##^
#<<<.^

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3524kb

input:

4 5
>v<<.
^<..#
#...#
.#>^#

output:

2

result:

ok single line: '2'

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 3560kb

input:

4 6
>>v#>v
^#>>^v
^<<#v<
.#^<<#

output:

0

result:

wrong answer 1st lines differ - expected: '5', found: '0'