문제
풀이
[#M_풀이 보기|접기|
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 | import java.util.Scanner; public class GraphicalEditor { static int m,n; static Object[][] image; static Scanner scan = new Scanner(System.in); static Object xyCol; public static void main(String[] args) { while(true) { String command = scan.next(); if(command.equalsIgnoreCase("I")) { m = scan.nextInt(); n = scan.nextInt(); insert(m,n); while(true) { command = scan.next(); if(command.equalsIgnoreCase("C")) clear(); else if(command.equalsIgnoreCase("L")) color(); else if(command.equalsIgnoreCase("S")) { save(); clear();} else if(command.equalsIgnoreCase("V")) vertical(); else if(command.equalsIgnoreCase("H")) horizon(); else if(command.equalsIgnoreCase("K")) rectang(); else if(command.equalsIgnoreCase("F")) { int x = scan.nextInt(); int y = scan.nextInt(); String col = scan.next(); xyCol = image[y-1][x-1]; fill(x, y, col); } } }else if(command.equalsIgnoreCase("X")) { scan.close(); break; } } } //그림판 생성 public static Object[][] insert(int m, int n) { image = new Object[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { image[i][j] = 0; } } return image; } //그림판 초기화 public static Object[][] clear(){ for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { image[i][j] = 0; } } return image; } // 점 색칠 칠하기 public static Object[][] color() { int x = scan.nextInt(); int y = scan.nextInt(); String col = scan.next(); image[y-1][x-1] = col; return image; } // 그림판 출력 public static void save() { String title = scan.nextLine(); System.out.println(title); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(image[i][j]); } System.out.print("\n"); } } // 수직으로 긋기 public static Object[][] vertical() { int x = scan.nextInt(); int y = scan.nextInt(); int y2 = scan.nextInt(); String col = scan.next(); if(y-y2>0) { for(int i=0;i<=y-y2;i++) { image[y-i-1][x-1] = col; } }else { for(int i=0;i<=y2-y;i++) { image[y+i-1][x-1] = col; } } return image; } //수평으로 긋기 public static Object[][] horizon() { int x = scan.nextInt(); int x1 = scan.nextInt(); int y = scan.nextInt(); String col = scan.next(); if(x-x1>0) { for(int i=0;i<=x-x1;i++) { image[y][x-1-i] = col; } }else { for(int i=0;i<=x1-x;i++) { image[y-1][x-1+i] = col; } } return image; } //채우기 public static void fill(int x, int y, String col) { if(((y-2>=0&&x-1>=0)&&(y-2<n&&x-1<m))&&(xyCol.equals(image[y-2][x-1])||xyCol==image[y-2][x-1])) { image[y-2][x-1] = col; fill(x, y-1, col); } if(((y>=0&&x-1>=0)&&(y<n&&x-1<m))&&(xyCol.equals(image[y][x-1])||xyCol==image[y][x-1])) { image[y][x-1] = col; fill(x, y+1, col); } if(((y-1>=0)&&(x>=0)&&(y-1<n&&x<m))&&(xyCol.equals(image[y-1][x])||xyCol==image[y-1][x])) { image[y-1][x] = col; fill(x+1, y, col); } if(((y-1>=0&&x-2>=0)&&(y-1<n&&x-2<m))&&(xyCol.equals(image[y-1][x-2])||xyCol==image[y-1][x-2])) { image[y-1][x-2] = col; fill(x-1, y, col); } } //네모 만들기 public static Object[][] rectang() { int x = scan.nextInt(); int y = scan.nextInt(); int x1 = scan.nextInt(); int y1 = scan.nextInt(); String col = scan.next(); for(int i=y-1;i<y1-1;i++) { for(int j=x-1;j<x1-1;j++) { image[i][j] = col; } } return image; } } | cs |