#include <bits/stdc++.h>
#define ff first
#define ss second
#define mkt make_tuple
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
#define ll long long
#define lp(i,a,b) for(int i = a ; i < b ; i++ )
#define pii pair<int,int>
#define mk make_pair
#define pb push_back
#define tiiii tuple<int,int,int, int>
const int MAX = 2010 ;
const int inf = 1e9+10 ;
using namespace std ;
int N , M ;
int dx[4] = { 0, 1 , 0 , -1} , dy[4] = {1,0,-1,0} ;
int grid[MAX][MAX] , dist[MAX][MAX][4] ;
tuple<int,int,int> par[MAX][MAX][4 ] ;
pair<int,int> S , T ;
deque<tiiii> fila ;
bool valid(int x, int y) { return 1 <= min(x,y) && x <= N && y <= M; }
int main()
{
scanf("%d %d", &N, &M ) ;
for(int i = 1 ; i <= N ; i++ )
for(int j = 1 ; j <= M ; j++ )
{
char c ;
scanf(" %c", &c ) ;
if(c == 'v') grid[i][j] = 1 ;
if( c == '<' ) grid[i][j] = 2 ;
if( c == '^' ) grid[i][j] = 3 ;
if( c == 'o' ) S = mk(i,j) , grid[i][j] = -1 ;
if( c == 'x' ) T = mk(i,j) , grid[i][j] = -2 ;
if(c == '.') grid[i][j] = 4 ;
}
for(int i = 1 ; i <= N ; i++ )
for(int j = 1 ; j <= M ; j++ )
for(int g = 0 ; g < 4 ; g++ ) dist[i][j][g] = inf ;
for(int g = 0 ; g < 4 ; g++ )
{
dist[S.first][S.second][g] = 0 ;
fila.push_back( mkt(0, S.ff, S.ss,g) ) ;
}
tuple<int,int,int> cur ;
while(!fila.empty())
{
int d = get<0>(fila.front()) ;
int x = get<1>(fila.front()) ;
int y = get<2>(fila.front());
int g = get<3>(fila.front()) ;
fila.pop_front() ;
if( d != dist[x][y][g] ) continue ;
if(x == T.ff && y == T.ss )
{
printf("%d\n" , dist[x][y][g]) ;
cur = par[x][y][g] ;
break ;
}
int nx = x + dx[g] ;
int ny = y + dy[g] ;
if(!valid(nx, ny)) continue ;
for(int i = 0 , nd ; i < 4 ; i++ )
{
nd = dist[x][y][g] ;
if( i != grid[nx][ny] && grid[nx][ny] >= 0 ) nd++ ;
if(nd >= dist[nx][ny][i]) continue ;
par[nx][ny][i] = mkt(x,y,g) ;
if(nd == dist[x][y][g])
fila.push_front( mkt( dist[nx][ny][i] = nd , nx, ny, i ) ) ;
else
fila.pb( mkt( dist[nx][ny][i] = nd , nx, ny, i ) ) ;
}
}
int x = get<0>(cur) , y = get<1>(cur) , g = get<2>(cur) ;
while(!(mk(x,y) == S))
{
grid[x][y] = g ;
cur = par[x][y][g] ;
x = get<0>(cur) , y = get<1>(cur), g = get<2>(cur) ;
}
lp(i,1,N+1)
{
lp(j,1,M+1)
{
if( grid[i][j] == 0 ) printf(">") ;
if( grid[i][j] == 1 ) printf("v") ;
if( grid[i][j] == 2 ) printf("<") ;
if( grid[i][j] == 3 ) printf("^") ;
if( grid[i][j] == -1 ) printf("o") ;
if( grid[i][j] == -2 ) printf("x") ;
if( grid[i][j] == 4 ) printf(".") ;
}
printf("\n") ;
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf("%d %d", &N, &M ) ;
| ~~~~~^~~~~~~~~~~~~~~~~~
Main.cpp:36:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf(" %c", &c ) ;
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
100 ms |
30052 KB |
Output is correct |
7 |
Correct |
116 ms |
52680 KB |
Output is correct |
8 |
Correct |
319 ms |
81156 KB |
Output is correct |
9 |
Correct |
484 ms |
171852 KB |
Output is correct |
10 |
Correct |
830 ms |
198448 KB |
Output is correct |
11 |
Correct |
997 ms |
302832 KB |
Output is correct |