이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void IO(string name = "") {
cin.tie(0)->sync_with_stdio(0);
if (name.size()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
const int inf = 1e9+7;
vector<string> grid;
int H, W;
bool is_valid(int row, int col) {
if(row>=0 && row<H && col>=0 && col<W && grid[row][col]!='.') return true;
return false;
}
int main() {
// IO("pcb");
cin >> H >> W;
grid.resize(H);
for(int i = 0; i < H; i++) cin >> grid[i];
vector<vector<bool>> visited(H, vector<bool>(W));
vector<vector<int>> dist(H, vector<int>(W, inf));
dist[0][0] = 0;
deque<pair<int,int>> dq;
dq.push_back({0, 0});
while(dq.size()) {
auto [row, col] = dq.front(); dq.pop_front();
if(visited[row][col]) continue;
pair<int,int> dirs[] = {{0,1},{0,-1},{1,0},{-1,0}};
for(auto[dirrow, dircol] : dirs) {
int nrow = row + dirrow;
int ncol = col + dircol;
if(is_valid(nrow, ncol) && !visited[nrow][ncol]) {
if(grid[row][col]==grid[nrow][ncol]) { // 0
if(dist[row][col]+0<dist[nrow][ncol]) {
dist[nrow][ncol] = dist[row][col]+0;
dq.push_front({nrow, ncol});
}
} else { // 1
if(dist[row][col]+0<dist[nrow][ncol]) {
dist[nrow][ncol] = dist[row][col]+1;
dq.push_back({nrow, ncol});
}
}
}
}
}
int ans = 0;
for(int i = 0; i < H; i++) {
for(int j = 0; j < W; j++) {
if(dist[i][j]<inf) {
ans = max(ans, dist[i][j]);
}
}
}
cout << ans+1;
}
컴파일 시 표준 에러 (stderr) 메시지
tracks.cpp: In function 'void IO(std::string)':
tracks.cpp:9:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:10:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |