QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#413124 | #4231. Rafting Trip | ishmeal# | RE | 1ms | 3472kb | C++20 | 3.5kb | 2024-05-17 06:24:57 | 2024-05-17 06:24:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
int r, c;
vector<string> vec;
map<pii, pii> graph;
map<pii, vector<pii>> rev_graph;
vector<pii> heads;
map<pii, map<pii, int>> sites;
vector<vector<int>> visited;
map<char, pii> dirs = {{'v', {1, 0}}, {'^', {-1, 0}}, {'<', {0, -1}}, {'>', {0, 1}}};
bool valid(int x, int y){
return (x >= 0 && x < r && y >= 0 && y < c);
}
bool land(int x, int y){
return (vec[x][y] == '.' || vec[x][y] == '#');
}
int dir1[] = {0, 0, 1, -1};
int dir2[] = {-1, 1, 0, 0};
int maxAm = 0;
void add_sites(pii curr, pii head){
for(int i = 0; i < 4; i++){
int x = curr.f + dir1[i];
int y = curr.s + dir2[i];
if(!valid(x, y)) continue;
if(vec[x][y] == '#') sites[head][{x, y}]++;
}
}
void remove_sites(pii curr, pii head){
for(int i = 0; i < 4; i++){
int x = curr.f + dir1[i];
int y = curr.s + dir2[i];
if(!valid(x, y)) continue;
if(vec[x][y] == '#'){
sites[head][{x, y}]--;
if(sites[head][{x, y}] == 0) sites[head].erase({x, y});
}
}
}
void find_head(pii curr);
void comp_cycles(pii curr, pii head, int head_cnt);
void dfs(pii curr, pii head);
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> r >> c;
for(int i = 0; i < r; i++){
string str;
cin >> str;
vec.pb(str);
}
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
if(land(i, j)) continue;
graph[{i, j}] = {-1, -1};
rev_graph[{i, j}] = {};
}
}
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
if(land(i, j)) continue;
int x = i + dirs[vec[i][j]].f;
int y = j + dirs[vec[i][j]].s;
//cout << "(" << i << ", " << j << ") (" << x << ", " << y << ")\n";
if(!valid(x, y) || land(x, y)) continue;
graph[{i, j}] = {x, y};
rev_graph[{x, y}].pb({i, j});
}
}
visited.assign(r, vector<int>(c, 0));
for(auto p : graph){
pii curr = p.f;
if(visited[curr.f][curr.s]) continue;
find_head(curr);
//cout << "\n";
}
//cout << heads.size() << "\n";
for(int i = 0; i < heads.size(); i++){
sites[heads[i]] = {};
int h_cnt = 0;
comp_cycles(heads[i], heads[i], h_cnt);
}
for(int i = 0; i < heads.size(); i++){
dfs(heads[i], heads[i]);
//cout << "\n";
}
cout << maxAm << "\n";
}
void find_head(pii curr){
if(visited[curr.f][curr.s] == 2) return;
if(visited[curr.f][curr.s] == 1){
heads.pb(curr);
return;
}
//cout << "(" << curr.f << ", " << curr.s << ") ";
visited[curr.f][curr.s] = 1;
if(graph[curr] != (pii){-1, -1}) find_head(graph[curr]);
else heads.pb(curr);
visited[curr.f][curr.s] = 2;
}
void comp_cycles(pii curr, pii head, int head_cnt){
if(curr == head){
if(head_cnt == 0) head_cnt++;
else return;
}
add_sites(curr, head);
if(curr != head){
for(auto p : rev_graph[curr]){
rev_graph[head].pb(p);
}
}
}
void dfs(pii curr, pii head){
if(curr != head) add_sites(curr, head);
//cout << "(" << curr.f << ", " << curr.s << "): " << sites[head].size() << "\n";
maxAm = max(maxAm, (int)sites[head].size());
for(auto p : rev_graph[curr]){
dfs(p, head);
}
if(curr != head) remove_sites(curr, head);
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3472kb
input:
5 6 v<<<#v v#v<.> >>v<<< ..v##^ #<<<.^
output:
4
result:
ok single line: '4'
Test #2:
score: -100
Runtime Error
input:
4 5 >v<<. ^<..# #...# .#>^#