问题描述
消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进行,棋盘的每一行每一列的方格上放着一个有颜色的棋子,当一行或一列上有连续三个或更多的相同颜色的棋子时,这些棋子都被消除。当有多处可以被消除时,这些地方的棋子将同时被消除。
现在给你一个n行m列的棋盘,棋盘中的每一个方格上有一个棋子,请给出经过一次消除后的棋盘。
请注意:一个棋子可能在某一行和某一列同时被消除。
输入格式
输入的第一行包含两个整数n, m,用空格分隔,分别表示棋盘的行数和列数。
接下来n行,每行m个整数,用空格分隔,分别表示每一个方格中的棋子的颜色。颜色使用1至9编号。
输出格式
输出n行,每行m个整数,相邻的整数之间使用一个空格分隔,表示经过一次消除后的棋盘。如果一个方格中的棋子被消除,则对应的方格输出0,否则输出棋子的颜色编号。
样例输入
4 5
2 2 3 1 2
3 4 5 1 4
2 3 2 1 3
2 2 2 4 4
样例输出
2 2 3 0 2
3 4 5 0 4
2 3 2 0 3
0 0 0 4 4
样例说明
棋盘中第4列的1和第4行的2可以被消除,其他的方格中的棋子均保留。
样例输入
4 5
2 2 3 1 2
3 1 1 1 1
2 3 2 1 3
2 2 3 3 3
样例输出
2 2 3 0 2
3 0 0 0 0
2 3 2 0 3
2 2 0 0 0
样例说明
棋盘中所有的1以及最后一行的3可以被同时消除,其他的方格中的棋子均保留。
评测用例规模与约定
所有的评测用例满足:1 ≤ n, m ≤ 30。
分析
对二维数组的每一行,每列进行如下考察:
假设对于第一行:
设count初始为1,从第二个元素开始,如果和前一个元素相等count++,如果和前一个元素不等,看count是否大于等于3,如果不是,count重新赋值为1,如果是,使用一个map,记录下连在一起的元素,map的key是整数,保存行号,值是个list,保存该行连在一起的列号。
对于每列,做类似处理。
最后,根据map,重新对二维数组赋值。
第一种解法:
import java.util.Scanner;
public class RemoveGame {
public static void main(String[] args) {
// 2 3
// 1 5 3
// 3 2 4
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt();
int column = scanner.nextInt();
String br = scanner.nextLine();
int[][] arr = new int[row][column];
for(int i = 0; i < row; i++){
String line = scanner.nextLine();
String[] str_arr = line.split(" ");
for(int j = 0; j < column; j++){
arr[i][j] = Integer.parseInt(str_arr[j]);
}
}
boolean[][] bFlagArr = new boolean[row][column];
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[0].length; j++){
int horizontal_count = 0;
int vertical_count = 0;
if(i - 1 >= 0 && arr[i-1][j] == arr[i][j]){
vertical_count++;
if(i - 2 >= 0 && arr[i-2][j] == arr[i][j]){
vertical_count++;
}
}
if(i + 1 < row && arr[i+1][j] == arr[i][j]){
vertical_count++;
if(i + 2 < row && arr[i+2][j] == arr[i][j]){
vertical_count++;
}
}
if(vertical_count >= 2){
bFlagArr[i][j] = true;
continue;
}
if(j - 1 >= 0 && arr[i][j-1] == arr[i][j]){
horizontal_count++;
if(j - 2 >= 0 && arr[i][j-2] == arr[i][j]){
horizontal_count++;
}
}
if(j + 1 < column && arr[i][j+1] == arr[i][j]){
horizontal_count++;
if(j + 2 < column && arr[i][j+2] == arr[i][j]){
horizontal_count++;
}
}
if(horizontal_count >= 2){
bFlagArr[i][j] = true;
continue;
}
}
}
for(int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if(bFlagArr[i][j]){
System.out.print("0"+" ");
}
else{
System.out.print(arr[i][j] + " ");
}
}
System.out.println();
}
}
}
判断一行可以消去的范围集合
import java.util.ArrayList;
import java.util.List;
public class DeleteDemo {
public static void main(String[] args) {
List<String> lst = new ArrayList<>();
String info = "1 2 2 2 1 2 2 2 2";//1,4
info += " " + "#";
String[] arr = info.split(" ");
int count = 1;
for(int i = 0; i < arr.length - 1; i++){
if(arr[i+1].equals(arr[i])){
count++;
}
else{
if(count >= 3){
lst.add((i-count+1)+","+i);
}
count = 1;
}
}
System.out.println(lst);
for(String range : lst){
String[] arr2 = range.split(",");
int start = Integer.parseInt(arr2[0]);
int end = Integer.parseInt(arr2[1]);
for(int j = start; j <= end; j++){
arr[j] = String.valueOf((char)(arr[j].charAt(0) - Integer.parseInt(arr[j])));
}
}
for(String s : arr){
System.out.print(s + " ");
}
}
}
本题代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, List<String>> mapRow = new TreeMap<>();
Map<Integer, List<String>> mapCol = new TreeMap<>();
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int col = sc.nextInt();
sc.nextLine();
int[][] board = new int[row][col];
int[][] boardEx = new int[row+1][col+1];
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
board[i][j] = sc.nextInt();
boardEx[i][j] = board[i][j];
}
sc.nextLine();
}
//处理行
for(int i = 0; i < row; i++){
int count = 1;
for(int j = 0; j < col; j++){
if(boardEx[i][j+1]==boardEx[i][j]){
count++;
}
else{
if(count >= 3){
if(mapRow.containsKey(i)){
List<String> lst = mapRow.get(i);
lst.add((j-count+1)+","+j);
}
else{
List<String> lst = new ArrayList<>();
lst.add((j-count+1)+","+j);
mapRow.put(i, lst);
}
}
count = 1;
}
}
}
//处理列
for(int i = 0; i < col; i++){
int count = 1;
for(int j = 0; j < row; j++){
if(boardEx[j+1][i]==boardEx[j][i]){
count++;
}
else{
if(count >= 3){
if(mapCol.containsKey(i)){
List<String> lst = mapCol.get(i);
lst.add((j-count+1)+","+j);
}
else{
List<String> lst = new ArrayList<>();
lst.add((j-count+1)+","+j);
mapCol.put(i, lst);
}
}
count = 1;
}
}
}
//根据map中记录的信息,对二维数组的行列置0
for(Map.Entry<Integer, List<String>> entry : mapRow.entrySet()){
int _row = entry.getKey();
List<String> range = entry.getValue();
for(String tmp : range){
String[] arr = tmp.split(",");
int start = Integer.parseInt(arr[0]);
int end = Integer.parseInt(arr[1]);
for(int a = start; a <= end; a++){
board[_row][a] = 0;
}
}
}
for(Map.Entry<Integer, List<String>> entry : mapCol.entrySet()){
int _col = entry.getKey();
List<String> range = entry.getValue();
for(String tmp : range){
String[] arr = tmp.split(",");
int start = Integer.parseInt(arr[0]);
int end = Integer.parseInt(arr[1]);
for(int a = start; a <= end; a++){
board[a][_col] = 0;
}
}
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
}