on
[2194] 유닛 이동시키기
[2194] 유닛 이동시키기
#include < bits / stdc + + .h >
using namespace std ;
int n, m, a, b, k, ey, ex, dy[ 4 ] = { 0 , 1 , 0 , - 1 }, dx[ 4 ] = { 1 , 0 , - 1 , 0 };
bool check[ 503 ][ 503 ], visited[ 503 ][ 503 ];
typedef struct {
int y, x, cnt;
} state;
int bfs( int sy, int sx) {
queue < state > q;
q.push({sy, sx, 0 });
visited[sy][sx] = true ;
while ( ! q.empty()) {
state cur = q. front ();
q. pop ();
bool flag = false ;
for ( int i = cur.y; i < cur.y + a; i + + ) {
for ( int j = cur.x; j < cur.x + b; j + + ) {
if (i < 0 | | j < 0 | | i > = n | | j > = m) {
flag = true ;
goto skip;
}
if (check[i][j]) {
flag = true ;
goto skip;
}
}
}
skip:
if (flag) continue ;
if (cur.y = = ey & & cur.x = = ex) return cur.cnt;
for ( int i = 0 ; i < 4 ; i + + ) {
int y = cur.y + dy[i];
int x = cur.x + dx[i];
if (visited[y][x] = = false ) {
visited[y][x] = true ;
q.push({y, x, cur.cnt + 1 });
}
}
}
return - 1 ;
}
int main( void ) {
cin > > n > > m > > a > > b > > k;
int i, j;
for (i = 0 ; i < k; i + + ) {
int y, x;
cin > > y > > x;
check[y - 1 ][x - 1 ] = true ;
}
int sy, sx;
cin > > sy > > sx;
cin > > ey > > ex;
sy - - ;
sx - - ;
ey - - ;
ex - - ;
cout < < bfs(sy, sx);
return 0 ;
}
from http://dizlet.tistory.com/203 by ccl(A) rewrite - 2021-11-08 05:27:20