Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 신설동역 맛집
- 의정부역 콩나물국밥
- 서울 흑산도홍어
- Running 연습
- 가민 크로노스
- 인터벌 러닝
- 10KM 러닝
- 가민 크로노스 인터벌 러닝
- 알고리즘
- java 알고리즘
- 10k 마라톤
- 의정부 전주콩나물국밥
- Garmin Chronos
- 가민
- 신대방삼거리역 흑산도홍어
- java lombok
- github command
- MongoDB mongoimport
- 의정부시청역 콩나물국밥
- MongoDB foreach
- 의정부 콩나물국밥 맛집
- lombok
- 신대방삼거리역 홍어
- 의정부 전주본가
- 가민 크로노스 러닝
- 인터벌러닝
- 마라톤
- 가민 인터벌 러닝
- 서울 홍어삼합
- 러너
Archives
- Today
- Total
나의 Winding Road
로봇 청소기 본문
[2017-04-16 일요일]
* 내용: 로봇 청소기
1. 문제
2. 해결 방법
1. 문제
* 내용
- 문제: 생략
* 예제 입출력
번호 |
입력 |
출력 |
1 |
생략 |
* 과정 (중략)
* 최종 결과 |
2. 해결 방법
* 이슈
A. 배열의 index의 y, x를 잘 구별해야한다.
B. 로봇을 움직인다고 생각하기보다는 지도를 움직인다고 생각한다.
C. 지도와 마찬가지로 로봇도 적절히 좌표 값을 변경해주어야 한다.
* 소스코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | import java.util.*; /** * Created by WindingRoad on 2017-04-15. */ public class RobotCleaner { @SuppressWarnings("resource") public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int times = scanner.nextInt(); int execTimes = 0; while (execTimes < times) { int size = scanner.nextInt(); int curX = scanner.nextInt(); int curY = scanner.nextInt(); int nesw = scanner.nextInt(); int[][] mapTemp = new int[size][size]; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { mapTemp[y][x] = scanner.nextInt(); } } Robot robot = new Robot(curX, curY, nesw, size); Map map = new Map(size, mapTemp, robot); robot.setMap(map); map.setMapByNesw(nesw); // 로봇 시작 동서남북 위치 결정 robot.clean(); // 청소 시작 execTimes++; } } } class Robot { public int curX; public int curY; public int realX; public int realY; public int nesw; // 0: public int size; private Map map; public int rotateCnt = 0; public Robot(int curX, int curY, int nesw, int size) { this.curX = curX; this.curY = curY; this.nesw = nesw; this.size = size; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public void roateCurYX(int cnt) { for(int i = 0; i < cnt; i++) { int temp = curY; curY = curX; curX = size - 1 - temp; } } public void setRealYX(int curY, int curX) { this.realY = curY; this.realX = curX; } public void roateRealYX(int cnt) { this.realY = curY; this.realX = curX; for(int i = 0; i < 4 - cnt; i++) { int temp = realY; realY = realX; realX = size - 1 - temp; } } public void clean() { map.setMapYX(curY, curX, 2); // 2가 클린 상태 while(true) { if(map.getMapYX(curY, curX - 1) == 0) { // 왼 curX = curX - 1; map.setMapYX(curY, curX, 2); map.rotateMap(1); roateCurYX(1); rotateCnt += 1; } else if(map.getMapYX(curY - 1, curX) == 0) { // 위 curY = curY - 1; map.setMapYX(curY, curX, 2); setRealYX(curY, curX); } else if(map.getMapYX(curY, curX + 1) == 0) { // 오른 curX = curX + 1; map.setMapYX(curY, curX, 3); map.rotateMap(3); roateCurYX(3); rotateCnt += 3; } else if(map.getMapYX(curY + 1, curX) == 0) { // 아래 curY = curY + 1; map.setMapYX(curY, curX, 2); map.rotateMap(2); roateCurYX(2); rotateCnt += 2; } else { if(map.getMapYX(curY + 1, curX) != 1) { // 후진 curY = curY + 1; map.setMapYX(curY, curX, 2); setRealYX(curY, curX); } else { break; } } if(rotateCnt % 4 != 0) { map.rotateRealMap(rotateCnt % 4); roateRealYX(rotateCnt % 4); map.printRealMap(); } else { setRealYX(curY, curX); map.syncRealMap(); map.printRealMap(); } } } } class Map { public int size; private int[][] map; int[][] realMap; Robot robot; public Map(int size, int[][] map, Robot robot) { this.size = size; this.map = map; this.robot = robot; this.realMap = new int[size][size]; } public int[][] getMap() { return map; } public void setMap(int[][] map) { this.map = map; } public void setMapYX(int y, int x, int v) { map[y][x] = v; } public int getMapYX(int y, int x) { return map[y][x]; } public void printMap() { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (x == robot.curX && y == robot.curY) System.out.print("■ "); else System.out.print(String.valueOf(map[y][x]) + " "); } System.out.println(""); } System.out.println(""); } public void syncRealMap() { // 회전이 없을 시 현재 map 상태를 그대로 가져온다 for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { realMap[y][x] = map[y][x]; } } } public void printRealMap() { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (x == robot.realX && y == robot.realY) System.out.print("■ "); else if (realMap[y][x] == 1) System.out.print("# "); else if (realMap[y][x] == 0) System.out.print("0 "); else System.out.print(" "); } System.out.println(""); } System.out.println(""); } public void rotateMap(int cnt) { int[][] mapTemp = new int[size][size]; for (int i = 0; i < cnt; i++) { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { mapTemp[y][x] = map[y][x]; // map 복사 } } for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { map[x][size - 1 - y] = mapTemp[y][x]; // map 회전 } } } } public void rotateRealMap(int rotateCnt) { // 이전에 회전된 횟수를 파라미터로 받는다 int[][] mapTemp = new int[size][size]; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { realMap[y][x] = map[y][x]; // map 복사 } } for (int i = 0; i < 4 - rotateCnt; i++) { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { mapTemp[y][x] = realMap[y][x]; // map 복사 } } for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { realMap[x][size - 1 - y] = mapTemp[y][x]; // map 회전 } } } } public void setMapByNesw(int nesw) { if (nesw == 0) { // 위 // 위 방향 기준으로 맵을 세팅하기 때문에 위 방향일 때는 고치지 않는다 } else if (nesw == 1) { // 오른 rotateMap(3); rotateRealMap(3); robot.rotateCnt += 3; } else if (nesw == 2) { // 아래 rotateMap(2); rotateRealMap(2); robot.rotateCnt += 2; } else if (nesw == 3) { // 왼 rotateMap(1); rotateRealMap(1); robot.rotateCnt += 1; } } } | cs |
'개발 > Algorithm' 카테고리의 다른 글
BAEKJOON 10825번: 국영수 (0) | 2018.09.04 |
---|---|
BAEKJOON 9465번: 스티커 (0) | 2018.09.02 |
BAEKJOON 1463번: 1로 만들기 (0) | 2018.08.30 |
BAEKJOON 1260번: DFS와 BFS (1) | 2017.04.17 |
BAEKJOON 13458번: 시험 감독 (0) | 2017.04.09 |
Comments