//chockolateman
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int H,W,di[4] = {1,-1,0,0},dj[4] = {0,0,1,-1},dist[16000005];
char grid[4005][4005];
vector<pair<int,int>> adj[16000005];
int id(int i,int j)
{
return (i-1)*W + j;
}
bool is_ok(int i,int j)
{
if(i >= 1 && i <= H && j >= 1 && j <= W && grid[i][j] != '.')
return true;
return false;
}
void connect_neighs()
{
for(int i = 1 ; i <= H ; i++)
for(int j = 1 ; j <= W ; j++)
{
if(grid[i][j]=='.')
continue;
for(int k = 0 ; k <= 3 ; k++)
{
int new_i = i + di[k];
int new_j = j + dj[k];
if(is_ok(new_i,new_j))
{
if(grid[i][j]==grid[new_i][new_j])
adj[id(i,j)].push_back({id(new_i,new_j),0});
else
adj[id(i,j)].push_back({id(new_i,new_j),1});
}
}
}
}
void BFS(int start)
{
for(int i = 1 ; i <= H ; i++)
for(int j = 1 ; j <= W ; j++)
if(grid[i][j]!='.')
dist[id(i,j)] = INF;
deque<int> q;
dist[start] = 0;
if(grid[1][1]!='.')
dist[start] = 1;
q.push_back(start);
while(!q.empty())
{
int v = q.front();
q.pop_front();
for(auto e : adj[v])
{
int u = e.first;
int w = e.second;
if(dist[v] + w < dist[u])
{
dist[u] = dist[v] + w;
if(w==0)
q.push_front(u);
else
q.push_back(u);
}
}
}
}
int main()
{
scanf("%d%d",&H,&W);
for(int i = 1 ; i <= H ; i++)
for(int j = 1 ; j <= W ; j++)
scanf(" %c",&grid[i][j]);
connect_neighs();
BFS(id(1,1));
int ans = 0;
for(int i = 1 ; i <= H ; i++)
for(int j = 1 ; j <= W ; j++)
ans = max(ans,dist[(id(i,j))]);
printf("%d\n",ans);
return 0;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | scanf("%d%d",&H,&W);
| ~~~~~^~~~~~~~~~~~~~
tracks.cpp:83:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf(" %c",&grid[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
390040 KB |
Output is correct |
2 |
Correct |
134 ms |
376148 KB |
Output is correct |
3 |
Correct |
148 ms |
376184 KB |
Output is correct |
4 |
Correct |
159 ms |
385964 KB |
Output is correct |
5 |
Correct |
139 ms |
378452 KB |
Output is correct |
6 |
Correct |
134 ms |
376148 KB |
Output is correct |
7 |
Correct |
135 ms |
376380 KB |
Output is correct |
8 |
Correct |
131 ms |
376404 KB |
Output is correct |
9 |
Correct |
143 ms |
376676 KB |
Output is correct |
10 |
Correct |
181 ms |
378448 KB |
Output is correct |
11 |
Correct |
135 ms |
378964 KB |
Output is correct |
12 |
Correct |
147 ms |
381524 KB |
Output is correct |
13 |
Correct |
149 ms |
378448 KB |
Output is correct |
14 |
Correct |
145 ms |
378448 KB |
Output is correct |
15 |
Correct |
172 ms |
387668 KB |
Output is correct |
16 |
Correct |
177 ms |
390224 KB |
Output is correct |
17 |
Correct |
172 ms |
383572 KB |
Output is correct |
18 |
Correct |
169 ms |
386132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
391780 KB |
Output is correct |
2 |
Correct |
369 ms |
417028 KB |
Output is correct |
3 |
Correct |
1057 ms |
602048 KB |
Output is correct |
4 |
Correct |
404 ms |
422224 KB |
Output is correct |
5 |
Correct |
732 ms |
561128 KB |
Output is correct |
6 |
Runtime error |
1766 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Correct |
150 ms |
392332 KB |
Output is correct |
8 |
Correct |
150 ms |
391760 KB |
Output is correct |
9 |
Correct |
154 ms |
377684 KB |
Output is correct |
10 |
Correct |
143 ms |
376568 KB |
Output is correct |
11 |
Correct |
149 ms |
392020 KB |
Output is correct |
12 |
Correct |
143 ms |
377172 KB |
Output is correct |
13 |
Correct |
291 ms |
416852 KB |
Output is correct |
14 |
Correct |
211 ms |
400616 KB |
Output is correct |
15 |
Correct |
191 ms |
393808 KB |
Output is correct |
16 |
Correct |
211 ms |
396372 KB |
Output is correct |
17 |
Correct |
539 ms |
473932 KB |
Output is correct |
18 |
Correct |
386 ms |
438100 KB |
Output is correct |
19 |
Correct |
391 ms |
422292 KB |
Output is correct |
20 |
Correct |
341 ms |
434644 KB |
Output is correct |
21 |
Correct |
638 ms |
517180 KB |
Output is correct |
22 |
Correct |
783 ms |
561236 KB |
Output is correct |
23 |
Correct |
832 ms |
559416 KB |
Output is correct |
24 |
Correct |
664 ms |
511316 KB |
Output is correct |
25 |
Correct |
1091 ms |
612692 KB |
Output is correct |
26 |
Correct |
1551 ms |
1048576 KB |
Output is correct |
27 |
Runtime error |
1600 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
1608 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
1610 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
1671 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Execution timed out |
2044 ms |
918164 KB |
Time limit exceeded |
32 |
Runtime error |
1601 ms |
1048576 KB |
Execution killed with signal 9 |