关注
考试完才发现两处小错误,好亏。。。下面是修改好的,应该是对的吧→_→
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
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 如何成为1个AI工程师? #
6932次浏览 316人参与
# 秋招拿一个offer可以躺平吗 #
277965次浏览 1412人参与
# 26届春招投递记录 #
41367次浏览 353人参与
# 一人分享一个skill #
34991次浏览 317人参与
# 27届实习投递记录 #
128836次浏览 1442人参与
# 机械人求职现状 #
44184次浏览 329人参与
# 你觉得第一学历对求职有影响吗? #
277151次浏览 1497人参与
# 我在大厂见过的最低学历 #
6898次浏览 71人参与
# 产品2023笔面经 #
89369次浏览 472人参与
# 第一次找实习,我建议__ #
87648次浏览 875人参与
# 秋招白月光 #
819684次浏览 5695人参与
# 虹软科技求职进展汇总 #
18615次浏览 141人参与
# 想给25届机械人的秋招建议 #
54448次浏览 264人参与
# 上班苦还是上学苦呢? #
350784次浏览 2088人参与
# 给26届的秋招建议 #
391464次浏览 4407人参与
# 要毕业了,再不说就来不及了 #
11775次浏览 173人参与
# HR面都在聊什么? #
48859次浏览 333人参与
# 机械人你觉得今年行情怎么样? #
9945次浏览 100人参与
# 找工作中的意难平 #
1106582次浏览 6532人参与
# 运营来爆料 #
106145次浏览 519人参与
