Untitled
unknown
plain_text
3 years ago
7.2 kB
9
Indexable
import java.io.*;
import java.util.*;
import javax.sound.sampled.SourceDataLine;
public class App {
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
private static InputStream inS = System.in;
private static OutputStream outS = System.out;
private static InputReader in = new InputReader(inS);
private static PrintWriter out = new PrintWriter(outS);
static int M;
static int N;
static int ultTot = 0;
static{
M = in.nextInt();
N = in.nextInt();
}
private static boolean[][] pernah = new boolean[M][N];
private static char[][] resultArray = new char[M][N];
private static char[][] tempArray = new char[M][N];
public static void main(String[] args) {
char[][] array = readArray2D(M, N);
// jawBreakDrop(array, 0);
// out.println(ultTot);
// for(int i = 0;i<array.length;i++){
// for(int j = 0;j<array[i].length;j++){
// int Number = array[i][j];
// resultArray = array;
// int total = 0;
// out.println("=====================");
// out.println("i = " + i + " j = " + j);
// while (true){
// fillFalse();
// tempArray = copyArray(resultArray);
// int sum = jawBreaker(i, j, Number, tempArray);
// if(sum == 1 || sum == 0){
// break;
// }
// total += sum;
// resultArray = tempArray;
// dropDown();
// printArray2D();
// }
// out.close();
// }
// out.println(total);
// out.println("=====================");
// }
// }
// ultTot += T;
// while(true){
// int T = 0;
// for(int i = 0;i<resultArray.length;i++){
// for(int j = 0;j<resultArray[i].length;j++){
// int Number = resultArray[i][j];
// fillFalse();
// tempArray = copyArray(resultArray);
// int sum = jawBreaker(i, j, Number, tempArray);
// if(sum > T){
// T = sum;
// resultArray = tempArray;
// }
// }
// }
// if(T == 0 ){
// break;
// }
// ultTot += T;
// resultArray = dropDown(resultArray);
// out.println("===============================");
// out.println("CurTot = " + ultTot);
// printArray2D();
// out.println("===============================");
// }
jawBreakDropResq(array, 0, 0);
out.println(ultTot);
out.close();
}
private static char[][] readArray2D(int M,int N){
char[][] locArray = new char[M][N];
for(int i = 0;i<M;i++){
for(int j = 0;j<N;j++){
locArray[i][j] = in.next().charAt(0);
}
}
return locArray;
}
private static void printArray2D(){
for(int i = 0;i<M;i++){
for(int j = 0;j<N;j++){
out.print(resultArray[i][j] + " ");
}
out.println();
}
}
private static int jawBreaker(int row,int col,int Number,char[][] array){
int numOfNum = 0;
if(row <0 || row >= M || col <0 || col >= N){
return 0;
}
if(array[row][col] == '.'){
return 0;
}
if((int)array[row][col] != Number ){
return 0;
}
if(pernah[row][col] == true){
return 0;
}
pernah[row][col] = true;
array[row][col] = '.';
numOfNum +=1;
numOfNum += jawBreaker(row + 1, col, Number, array);
numOfNum += jawBreaker(row , col + 1, Number, array);
numOfNum += jawBreaker(row - 1, col, Number, array);
numOfNum += jawBreaker(row , col - 1, Number, array);
return numOfNum;
}
private static void fillFalse(){
for(int i = 0;i<M;i++){
for(int j = 0;j<N;j++){
pernah[i][j] =false;
}
}
}
private static char[][] copyArray(char[][] array){
char[][] returned = new char[M][N];
for(int i = 0;i<M;i++){
for(int j = 0;j<N;j++){
returned[i][j] = array[i][j];
}
}
return returned;
}
private static void swap(int i1,int i2,int j,char[][] array){
char temp = array[i1][j];
array[i1][j] = array[i2][j];
array[i2][j] = temp;
}
private static char[][] dropDown(char[][] array){
for(int j = 0;j<N;j++){
for(int i = M-1;i>=0;i--){
if(i+1 < M && array[i][j] != '.' ){
int temp = i;
while( temp+1 < M && array[temp+1][j] == '.'){
swap(temp, temp+1, j,array);
temp+=1;
}
}
}
}
return array;
}
private static void jawBreakDropResq(char[][] array,int totalSum,int pick){
for(int i = 0;i<array.length;i++){
for(int j = 0;i<array[i].length;j++){
fillFalse();
char[][] copian = copyArray(array);
int sum= jawBreaker(i,j,array[i][j],copian);
if(pick ==2){
if(totalSum > ultTot){
ultTot = totalSum;
}
}
else{
jawBreakDropResq(dropDown(copian), totalSum + sum,pick+1);
}
}
}
}
}Editor is loading...