考试完才发现两处小错误,好亏。。。下面是修改好的,应该是对的吧→_→  import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int RED = 0, GREEN = 1, BLUE = 2, YELLOW = 3, PURPLE = 4; int[][] p = { {RED,RED,BLUE,BLUE,GREEN,YELLOW,BLUE,YELLOW,RED,PURPLE}, {GREEN,GREEN,GREEN,BLUE,RED,PURPLE,RED,YELLOW,YELLOW,BLUE}, {BLUE,RED,RED,YELLOW,YELLOW,PURPLE,BLUE,GREEN,GREEN,BLUE}, {YELLOW,RED,BLUE,YELLOW,BLUE,RED,PURPLE,GREEN,GREEN,RED}, {YELLOW,RED,BLUE,BLUE,PURPLE,GREEN,PURPLE,RED,YELLOW,BLUE}, {PURPLE,YELLOW,RED,RED,YELLOW,RED,PURPLE,YELLOW,RED,RED}, {YELLOW,YELLOW,GREEN,PURPLE,GREEN,RED,BLUE,YELLOW,BLUE,GREEN}, {RED,YELLOW,BLUE,BLUE,YELLOW,GREEN,PURPLE,RED,BLUE,GREEN}, {GREEN,GREEN,YELLOW,YELLOW,RED,RED,PURPLE,BLUE,BLUE,GREEN}, {PURPLE,BLUE,RED,RED,PURPLE,YELLOW,BLUE,RED,RED,GREEN}}; String[] strs = sc.nextLine().split(" "); int[] poss = new int[strs.length]; for (int i=0; i<poss.length; i++) poss[i] = Integer.valueOf(strs[i]); for (int i=0; i<poss.length; i++) { int m = (poss[i] - 1) / 10; int n = (poss[i] - 1) % 10; int num = p[m][n]; // 如果当前位置不为空,则递归消除之 if (num == 5) continue; solve(p, m, n, num); // 如果下方为空,则向下移动 for (int y=0; y<10; y++) { int bottom = 9; int nonempty = 9; while (true) { while (nonempty >= 0 && p[nonempty][y] == 5) nonempty--; if (nonempty < 0) break; if (bottom != nonempty) { p[bottom][y] = p[nonempty][y]; p[nonempty][y] = 5; } bottom--; nonempty--; } } // 如果左侧整列为空,则向左移动 int left = 0; int nonemptyline = 0; while (true) { while (nonemptyline < 10) { boolean flag = true; for (int x=0; x<10; x++) { if (p[x][nonemptyline] != 5) { flag = false; break; } } if (flag) nonemptyline++; else break; } if (nonemptyline >= 10) break; if (left != nonemptyline) { for (int x=0; x<10; x++) { p[x][left] = p[x][nonemptyline]; p[x][nonemptyline] = 5; } } left++; nonemptyline++; } } // 统计分类方块的数量(包含有颜色的方块和空方块) int[] res = new int[6]; for (int x=0; x<10; x++) for (int y=0; y<10; y++) res[p[x][y]]++; for (int i=0; i<5; i++) { System.out.print(res[i]); if (i != 4) System.out.print(" "); else System.out.println(); } } } private static void solve(int[][] p, int m, int n, int num) { int[][] d = {{-1,0}, {1,0}, {0,-1}, {0,1}}; for (int i=0; i<4; i++) { int newm = m + d[i][0]; int newn = n + d[i][1]; if (newm < 0 || newm >= 10 || newn < 0 || newn >= 10) continue; if (p[newm][newn] == num) { p[newm][newn] = 5; solve(p, newm, newn, num); } } } }
点赞 1

相关推荐

04-03 15:12
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务