12100번 - 2048 (Easy)
package test;
import java.util.Scanner;
public class test1 { static Scanner s = new Scanner(System.in); static int n = -1; static Node start = null; static int maxNumber = -1; public static void main(String[] args) { init(); solve();// Node dummy = goLeft(start);// printMap(dummy);// System.out.println();// dummy= goRight(start);// printMap(dummy);// System.out.println();// dummy= goTop(start);// printMap(dummy);// System.out.println();// dummy= goBot(start);// printMap(dummy);// System.out.println();// // } static void solve(){ dfs(start,0); System.out.println(maxNumber); } static void dfs(Node prv,int deapth){ if(deapth == 6) return; dfs(goLeft(prv),deapth+1); dfs(goTop(prv),deapth+1); dfs(goRight(prv),deapth+1); dfs(goBot(prv),deapth+1); } static Node goLeft(Node prv){ Node nextNode = new Node(n); for(int i=0;i<n;i++){ int newNodeIndex = 0; int prvValue=-1; int j=0; while (j<n){ if(prv.map[i][j]!=0){ if(prvValue ==prv.map[i][j]){ //값이 같아서 합체! nextNode.map[i][newNodeIndex++] = prv.map[i][j]*2; prvValue = -1; if(prv.map[i][j]*2>maxNumber) maxNumber = prv.map[i][j]*2; }else if(prvValue != prv.map[i][j]){//값이 달라.. if(prvValue == -1){ prvValue = prv.map[i][j]; }else{ nextNode.map[i][newNodeIndex++]= prvValue; prvValue = prv.map[i][j]; } } } if(j==n-1 && prvValue != -1){ nextNode.map[i][newNodeIndex] = prvValue; } j++; } } return nextNode; } static Node goTop(Node prv){ Node nextNode = new Node(n); for(int i=0;i<n;i++){ int newNodeIndex = 0; int prvValue=-1; int j=0; while (j<n){ if(prv.map[j][i]!=0){ if(prvValue ==prv.map[j][i]){ //값이 같아서 합체! nextNode.map[newNodeIndex++][i] = prv.map[j][i]*2; prvValue = -1; if(prv.map[j][i]*2>maxNumber) maxNumber = prv.map[j][i]*2; }else if(prvValue != prv.map[j][i]){//값이 달라.. if(prvValue == -1){ prvValue = prv.map[j][i]; }else{ nextNode.map[newNodeIndex++][i]= prvValue; prvValue = prv.map[j][i]; } } } if(j==n-1 && prvValue != -1){ nextNode.map[newNodeIndex][i] = prvValue; } j++; } } return nextNode; } static Node goRight(Node prv){ Node nextNode = new Node(n); for(int i=0;i<n;i++){ int newNodeIndex = n-1; int prvValue=-1; int j=n-1; while (j>=0){ if(prv.map[i][j]!=0){ if(prvValue ==prv.map[i][j]){ //값이 같아서 합체! nextNode.map[i][newNodeIndex--] = prv.map[i][j]*2; prvValue = -1; if(prv.map[i][j]*2>maxNumber){ maxNumber = prv.map[i][j]*2; } }else if(prvValue != prv.map[i][j]){//값이 달라.. if(prvValue == -1){ prvValue = prv.map[i][j]; }else{ nextNode.map[i][newNodeIndex--]= prvValue; prvValue = prv.map[i][j]; } } } if(j==0 && prvValue!= -1){ nextNode.map[i][newNodeIndex] = prvValue; } j-=1; } } return nextNode; } static Node goBot(Node prv){ Node nextNode = new Node(n); for(int i=0;i<n;i++){ int newNodeIndex = n-1; int prvValue=-1; int j=n-1; while (j>=0){ if(prv.map[j][i]!=0){ if(prvValue ==prv.map[j][i]){ //값이 같아서 합체! nextNode.map[newNodeIndex--][i] = prv.map[j][i]*2; prvValue = -1; if(prv.map[j][i]*2>maxNumber) maxNumber = prv.map[j][i]*2; }else if(prvValue != prv.map[j][i]){//값이 달라.. if(prvValue == -1){ prvValue = prv.map[j][i]; }else{ nextNode.map[newNodeIndex--][i]= prvValue; prvValue = prv.map[j][i]; } } } if(j==0 && prvValue != -1){ nextNode.map[newNodeIndex][i] = prvValue; } j--; } } return nextNode; } static void init(){ n = s.nextInt(); start = new Node(n); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ int dummy = s.nextInt(); if(dummy>maxNumber) maxNumber = dummy; start.map[i][j] = dummy; } } } static void printMap(Node node){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ System.out.print(node.map[i][j]+" "); } System.out.println(); } } static class Node{ int n= -1; int[][] map = null; public Node(int n){ this.n = n; map = new int[n][n]; } }}
어 왜 들여쓰기가 ...
댓글을 작성하려면 로그인해야 합니다.
jhsong2580 6년 전
package test;
import java.util.Scanner;
public class test1 {
static Scanner s = new Scanner(System.in);
static int n = -1;
static Node start = null;
static int maxNumber = -1;
public static void main(String[] args) {
init();
solve();
// Node dummy = goLeft(start);
// printMap(dummy);
// System.out.println();
// dummy= goRight(start);
// printMap(dummy);
// System.out.println();
// dummy= goTop(start);
// printMap(dummy);
// System.out.println();
// dummy= goBot(start);
// printMap(dummy);
// System.out.println();
//
//
}
static void solve(){
dfs(start,0);
System.out.println(maxNumber);
}
static void dfs(Node prv,int deapth){
if(deapth == 6)
return;
dfs(goLeft(prv),deapth+1);
dfs(goTop(prv),deapth+1);
dfs(goRight(prv),deapth+1);
dfs(goBot(prv),deapth+1);
}
static Node goLeft(Node prv){
Node nextNode = new Node(n);
for(int i=0;i<n;i++){
int newNodeIndex = 0;
int prvValue=-1;
int j=0;
while (j<n){
if(prv.map[i][j]!=0){
if(prvValue ==prv.map[i][j]){ //값이 같아서 합체!
nextNode.map[i][newNodeIndex++] = prv.map[i][j]*2;
prvValue = -1;
if(prv.map[i][j]*2>maxNumber)
maxNumber = prv.map[i][j]*2;
}else if(prvValue != prv.map[i][j]){//값이 달라..
if(prvValue == -1){
prvValue = prv.map[i][j];
}else{
nextNode.map[i][newNodeIndex++]= prvValue;
prvValue = prv.map[i][j];
}
}
}
if(j==n-1 && prvValue != -1){
nextNode.map[i][newNodeIndex] = prvValue;
}
j++;
}
}
return nextNode;
}
static Node goTop(Node prv){
Node nextNode = new Node(n);
for(int i=0;i<n;i++){
int newNodeIndex = 0;
int prvValue=-1;
int j=0;
while (j<n){
if(prv.map[j][i]!=0){
if(prvValue ==prv.map[j][i]){ //값이 같아서 합체!
nextNode.map[newNodeIndex+
+][i] = prv.map[j][i]*2;
prvValue = -1;
if(prv.map[j][i]*2>maxNumber)
maxNumber = prv.map[j][i]*2;
}else if(prvValue != prv.map[j][i]){//값이 달라..
if(prvValue == -1){
prvValue = prv.map[j][i];
}else{
nextNode.map[newNodeIndex++][i]= prvValue;
prvValue = prv.map[j][i];
}
}
}
if(j==n-1 && prvValue != -1){
nextNode.map[newNodeIndex][i] = prvValue;
}
j++;
}
}
return nextNode;
}
static Node goRight(Node prv){
Node nextNode = new Node(n);
for(int i=0;i<n;i++){
int newNodeIndex = n-1;
int prvValue=-1;
int j=n-1;
while (j>=0){
if(prv.map[i][j]!=0){
if(prvValue ==prv.map[i][j]){ //값이 같아서 합체!
nextNode.map[i][newNodeIndex--] = prv.map[i][j]*2;
prvValue = -1;
if(prv.map[i][j]*2>maxNumber){
maxNumber = prv.map[i][j]*2;
}
}else if(prvValue != prv.map[i][j]){//값이 달라..
if(prvValue == -1){
prvValue = prv.map[i][j];
}else{
nextNode.map[i][newNodeIndex--]= prvValue;
prvValue = prv.map[i][j];
}
}
}
if(j==0 && prvValue!= -1){
nextNode.map[i][newNodeIndex] = prvValue;
}
j-=1;
}
}
return nextNode;
}
static Node goBot(Node prv){
Node nextNode = new Node(n);
for(int i=0;i<n;i++){
int newNodeIndex = n-1;
int prvValue=-1;
int j=n-1;
while (j>=0){
if(prv.map[j][i]!=0){
if(prvValue ==prv.map[j][i]){ //값이 같아서 합체!
nextNode.map[newNodeIndex--][i] = prv.map[j][i]*2;
prvValue = -1;
if(prv.map[j][i]*2>maxNumber)
maxNumber = prv.map[j][i]*2;
}else if(prvValue != prv.map[j][i]){//값이 달라..
if(prvValue == -1){
prvValue = prv.map[j][i];
}else{
nextNode.map[newNodeIndex--][i]= prvValue;
prvValue = prv.map[j][i];
}
}
}
if(j==0 && prvValue != -1){
nextNode.map[newNodeIndex][i] = prvValue;
}
j--;
}
}
return nextNode;
}
static void init(){
n = s.nextInt();
start = new Node(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int dummy = s.nextInt();
if(dummy>maxNumber)
maxNumber = dummy;
start.map[i][j] = dummy;
}
}
}
static void printMap(Node node){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(node.map[i][j]+" ");
}
System.out.println();
}
}
static class Node{
int n= -1;
int[][] map = null;
public Node(int n){
this.n = n;
map = new int[n][n];
}
}
}