[프로그래머스] 거리두기 확인하기

[프로그래머스] 거리두기 확인하기

#include < string >

#include < vector >

#include < queue >

#include < string .h >

using namespace std ;

typedef struct { int y, x, cnt; }state;

queue < state > q;

int dy[ 4 ] = { 0 , 1 , 0 , - 1 }, dx[ 4 ] = { 1 , 0 , - 1 , 0 };

int bfs( int sy, int sx, vector < string > map) {

int i, y, x;

bool visit[ 5 ][ 5 ];

while ( ! q.empty())q. pop ();

memset(visit, false , sizeof (visit));

q.push({ sy,sx, 0 });

visit[sy][sx] = true ;

while ( ! q.empty()) {

state cur = q. front ();

q. pop ();

if (cur.cnt = = 2 ) continue ;

for (i = 0 ; i < 4 ; i + + ) {

y = cur.y + dy[i];

x = cur.x + dx[i];

if (y < 0 | | x < 0 | | y > = 5 | | x > = 5 ) continue ;

if (visit[y][x] = = false & & map[y][x] = = 'O' ) {

q.push({ y,x,cur.cnt + 1 });

visit[y][x] = true ;

}

if (visit[y][x] = = false & & map[y][x] = = 'P' ) {

return 0 ;

}

}

}

return 1 ;

}

int solve( vector < string > map) {

int i, j;

for (i = 0 ; i < 5 ; i + + ) {

for (j = 0 ; j < 5 ; j + + ) {

if (map[i][j] = = 'P' ) {

int res = bfs(i, j, map);

if (res = = 0 )

return 0 ;

}

}

}

return 1 ;

}

vector < int > f( vector < vector < string > > map) {

vector < int > ans;

int i;

for (i = 0 ; i < map. size (); i + + ) {

ans. push_back (solve(map[i]));

}

return ans;

}

vector < int > solution( vector < vector < string > > places) {

vector < int > answer;

answer = f(places);

return answer;

}

from http://dizlet.tistory.com/67 by ccl(A) rewrite - 2021-09-13 02:26:51