https://www.acmicpc.net/problem/17144
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class robot{
int x;
int y;
int dir;
robot(int x, int y, int dir){
this.x=x;
this.y=y;
this.dir=dir;
}
}
public class Main {
static int N;
static int M;
static int[][]arr;
static int[]dx= {-1,0,1,0};
static int[]dy= {0,1,0,-1};
static int count;
static Queue<robot> q;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N=sc.nextInt();
M=sc.nextInt();
arr=new int[N][M];
int locationx=sc.nextInt();
int locationy=sc.nextInt();
int dir=sc.nextInt();
q=new LinkedList<robot>();
for(int i=0;i<N;i++) {
for(int j=0;j<M;j++) {
arr[i][j]=sc.nextInt();
}
}
arr[locationx][locationy]=9;
q.add(new robot(locationx, locationy, dir));
bfs();
count=0;
for(int i=0;i<N;i++) {
for(int j=0;j<M;j++) {
if(arr[i][j]==9) {
count++;
}
}
}
System.out.println(count);
}
private static void bfs() {
while(!q.isEmpty()) {
robot temp = q.poll();
int x=temp.x;
int y= temp.y;
int dir= temp.dir;
boolean check = false;
for(int i=0;i<4;i++) {
dir= (dir+3)%4;
int rx= x+dx[dir];
int ry=y+dy[dir];
if(rx<0||ry<0||rx>=N||ry>=M) {
continue;
}if(arr[rx][ry]==0) {
q.add(new robot(rx,ry,dir));
arr[rx][ry]=9;
check=true;
break;
}
}
if(check==false) {
int backdir=(dir+2)%4;
int rx= x+dx[backdir];
int ry= y+dy[backdir];
if(arr[rx][ry]!=1) {
arr[rx][ry]=9;
q.add(new robot(rx,ry,dir));
}
}
}
}
}
'코딩테스트(백준)' 카테고리의 다른 글
[백준] 16236 : 아기 상어 - JAVA(자바) - 사좋배 공유 (0) | 2020.07.27 |
---|---|
[백준] 14889 : 스타트와 링크 - JAVA(자바) - 사좋배 공유 (0) | 2020.07.27 |
[백준] 14503 : 로봇 청소기 - JAVA(자바) - 사좋배 공유 (0) | 2020.07.27 |
[백준] 15683 : 감시 - JAVA(자바) - 사좋배 공유 (0) | 2020.07.27 |
[백준] 15650 : N과M(2) - JAVA(자바) - 사좋배 공유 (0) | 2020.03.03 |