| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1364265 | kai | Tracks in the Snow (BOI13_tracks) | C++20 | 1250 ms | 356376 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define el '\n'
#define all(x) begin(x), end(x)
#define sz(s) (int)(s.size())
inline void setIO(string s) {
if(fopen((s + ".in").c_str(), "r")){
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
}
const int MOD = 1e9+7; //998244353;
const int N = 4000 + 36;
int h, w;
char a[N][N];
bool vis[N][N];
vector<int> adj[N*N];
int root[N][N], dist[N*N];
int comp = 1;
int dx[4] = {-1, 0, 1, 0},
dy[4] = {0, 1, 0, -1};
void bfs(){
queue<int> q;
q.push(1);
dist[1] = 1;
while(sz(q)){
int u = q.front(); q.pop();
for(int v: adj[u]){
if(dist[v]) continue;
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
void Solve(){
cin>> h>> w;
for(int i=1; i<=h; i++) for(int j=1; j<=w; j++) cin>> a[i][j];
for(int i=1; i<=h; i++){
for(int j=1; j<=w; j++){
if(root[i][j] || a[i][j] == '.') continue;
// cout<< root[i][j]<<el;
queue<pair<int, int>> q;
q.push({i, j});
root[i][j] = comp;
while(sz(q)){
auto [x, y] = q.front(); q.pop();
for(int k=0; k<4; k++){
int nx = x + dx[k];
int ny = y + dy[k];
// cerr<<x<<" "<<y<<' '<<nx<<" "<<ny<<' '<<a[x][y]<<' '<<a[nx][ny]<<' '<<root[x][y]<<el;
if(nx >= 1 && nx <= h && ny >= 1 && ny <= w && a[x][y] == a[nx][ny] && !root[nx][ny]){
q.push({nx, ny});
root[nx][ny] = comp;
}
}
}
comp++;
}
}
// for(int i=1; i<=h; i++){
// for(int j=1; j<=w; j++) cout<< root[i][j]<<' ';
// cout<<el;
// }
// cout<< comp<<el;
queue<pair<int, int>> q;
q.push({1, 1});
vis[1][1] = 1;
while(sz(q)){
auto [x, y] = q.front(); q.pop();
int u = root[x][y];
for(int k=0; k<4; k++){
int nx = x + dx[k];
int ny = y + dy[k];
// cout<< u<<": "<<x<<' '<<y<<el;
// cout<< nx<<' '<<ny<<el;
if(nx < 1 || nx > h || ny < 1 || ny > w || root[nx][ny] == 0 || vis[nx][ny]) continue;
int v = root[nx][ny];
if(u != v){
adj[u].push_back(v);
adj[v].push_back(u);
}
vis[nx][ny] = 1;
q.push({nx, ny});
}
}
for(int i=1; i<comp; i++){
// cout<< i<<": ";
sort(all(adj[i]));
adj[i].erase(unique(all(adj[i])), adj[i].end());
// for(int j: adj[i]) cout<< j<<' '; cout<<el;
}
bfs();
int ans = 0;
for(int i=1; i<comp; i++) ans = max(ans, dist[i]);
cout<< ans<<el;
}
signed main (){
setIO("Rem");
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T(1);
// cin >> T;
while(T--) Solve();
return 0;
}컴파일 시 표준 에러 (stderr) 메시지
| # | 결과 | 실행 시간 | 메모리 | 채점기 출력 |
|---|---|---|---|---|
| 결과를 불러오는 중입니다… | ||||
| # | 결과 | 실행 시간 | 메모리 | 채점기 출력 |
|---|---|---|---|---|
| 결과를 불러오는 중입니다… | ||||
