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 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 0 0
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 2
2 2 0 0 0 0 0 0 0 0 0 0 2 2 2 2 0 2 2 2 2 1 2 0 0 0 0 2 0 0
0 0 0 2 0 2 0 0 0 2 2 0 0 0 0 0 0 2 0 0 0 2 1 0 0 0 0 0 0 2
2 0 0 0 2 0 0 0 0 0 0 0 2 0 0 2 0 0 0 0 2 1 0 0 0 0 0 0 2 0
0 0 2 2 0 0 2 2 0 0 2 0 2 2 0 0 0 0 0 1 0 2 0 0 1 0 2 2 0 1
2 0 0 0 2 0 0 1 0 0 0 0 1 0 0 0 2 0 1 0 2 0 2 0 2 0 0 0 0 0
2 0 0 0 2 0 0 2 0 0 0 0 0 0 0 2 0 2 0 0 2 2 0 0 0 0 0 0 0 0
0 2 2 1 0 0 0 0 1 0 2 1 0 2 0 2 0 0 0 1 0 0 0 0 2 0 0 0 0 0
0 0 1 0 0 2 2 2 2 0 0 2 0 0 1 0 2 2 2 0 0 0 2 0 0 0 0 1 0 2
0 0 4 2 0 0 0 0 1 0 2 0 0 2 1 2 0 0 0 2 0 2 0 0 2 0 0 0 0 0
2 2 0 1 0 2 0 2 2 0 0 2 0 0 2 0 2 0 1 0 1 0 0 0 0 0 0 0 0 0
out:
71
2040
我的输出:
71
1800
代码:
# include <bits/stdc++.h>
# define int long long
using namespace std ;
const int L = 3e1 + 7 ;
const int N = 9e2 + 7 ;
const int M = 2e6 + 7 ;
const int dx[8] = { 1 , 1 , -1 , -1 , 2 , 2 , -2 , -2 } ;
const int dy[8] = { 2 , -2 , 2 , -2 , 1 , -1 , 1 , -1 } ;
struct edge {
int nxt , v , w ;
} e[M] ;
int h[N] , cnt ;
inline void add_edge ( int u , int v , int w ) {
e [ ++ cnt ] . nxt = h[u] , e[cnt] . v = v , e[cnt] . w = w ;
h[u] = cnt ;
}
int n , m ; int st[N][N] ;
int from[2] , to[2] ;
int ans , sum ;
struct node {
int x , y ;
inline int t () {
return ( x - 1 ) * m + y ;
}
} tmp ;
bool in[N] ;
inline void bfs () {
queue < node > q ; tmp . x = from[0] , tmp . y = from[1] ; q . push ( tmp ) ;
in [ tmp . t () ] = 1 ;
while ( ! q . empty () ) {
// cout << 2 ;
node u = q . front () ; q . pop () ;
int x = u . x , y = u . y ;
for ( int i = 0 ; i < 8 ; i ++ ) {
int xx = x + dx[i] , yy = y + dy[i] ;
if ( xx > n || xx < 1 || yy > m || yy < 1 ) continue ;
tmp . x = xx , tmp . y = yy ;
if ( st[xx][yy] == 1 ) add_edge ( u . t () , tmp . t () , 0 ) ;
if ( st[xx][yy] == 0 ) add_edge ( u . t () , tmp . t () , 1 ) ;
if ( st[xx][yy] != 2 && ! in [ tmp . t () ] ) in [ tmp . t () ] = 1 , q . push ( tmp ) ;
}
}
}
int dis[N] , f[N] ;
bool vis[N] ;
inline void spfa () {
queue < int > q ;
memset ( dis , 0x3f , sizeof dis ) ;
tmp . x = from[0] , tmp . y = from[1] ;
dis [ tmp . t () ] = 0 ; q . push ( tmp . t () ) ;
vis [ tmp . t () ] = f [ tmp . t () ] = 1 ;
tmp . x = to[0] , tmp . y = to[1] ;
int _ = tmp . t () ;
while ( ! q . empty () ) {
// cout << 1 ;
int u = q . front () ; q . pop () ; vis[u] = 0 ;
for ( int i = h[u] ; i ; i = e[i] . nxt ) {
int v = e[i] . v , w = e[i] . w ;
if ( dis[v] > dis[u] + w ) {
dis[v] = dis[u] + w ;
f[v] = f[u] ;
if ( ! vis[v] ) vis[v] = 1 , q . push ( v ) ;
} else if ( dis[v] == dis[u] + w ) f[v] += f[u] ;
}
}
ans = dis[_] , sum = f[_] ;
}
signed main () {
ios :: sync_with_stdio ( false ) ; cin . tie ( 0 ) , cout . tie ( 0 ) ;
cin >> n >> m ;
for ( int i = 1 ; i <= n ; i ++ ) {
for ( int j = 1 ; j <= m ; j ++ ) {
cin >> st[i][j] ;
if ( st[i][j] == 3 ) from[0] = i , from[1] = j , st[i][j] = 1 ;
if ( st[i][j] == 4 ) to[0] = i , to[1] = j , st[i][j] = 1 ;
}
}
bfs () ;
spfa () ;
if ( ans < 0x3f3f3f3f ) cout << ans << "\n" << sum << "\n" ;
else cout << "-1\n" ;
return 0 ;
}