와 두번다시는 풀고 싶지는 않는문제네요 * 이후 얼음이 있는 칸 3개 또는 그 이상과 인접해있지 않은 칸은 얼음의 양이 1 줄어든다. ->3개 미만은 얼음이 줄어든다 (무슨 국어 문제인가.. ㅎ) * 남아있는 얼음 중 가장 큰 덩어리가 차지하는 칸의 개수 얼음이 있는 칸이 얼음이 있는 칸과 인접해 있으면, 두 칸을 연결되어 있다고 한다. 덩어리는 연결된 칸의 집합 ->연결된 얼음 덩어리들의 칸의 갯수의 합,즉 얼음은 bfs,dfs를 통해서 연결해서 얼음 덩어리들을 계산하라는 의미
import java.util.*;
import java.io.*;
import java.lang.*;
import java.io.*;
public class Main {
public static int n;
public static int q;
public static int maps[][];
public static int copyMaps[][];
public static boolean bCheck[][];
public static boolean initbCheck[][];
public static boolean[][] visited;
public static int L;
public static void prints(int tmpMap[][]){
System.out.println();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(tmpMap[i][j]+",");
}
System.out.println();
}
}
public static class Ice {
public int i;
public int j;
public Ice(int i,int j) {
this.i=i;
this.j=j;
}
}
public static void Rotation(int starti, int startj){
int cnt=1;
int rStarti=starti*L;
int rEndi=starti*L+L;
int rStartj=startj*L;
int rEndj=startj*L+L;
int rLengthI=(rStarti+rEndi)/2;
int rLengthJ=(rStartj+rEndj)/2;
int rCountI=(rEndi-rStarti)/2;
int rCountJ=(rEndi-rStarti)/2;
//1 ->2
for(int i=rStarti;i<rLengthI;i++){
for(int j=rStartj;j<rLengthJ;j++){
copyMaps[i][j+ rCountJ]=maps[i][j];
}
}
//2->3
for(int i=rStarti;i<rLengthI;i++){
for(int j=rLengthJ;j<rEndj;j++){
copyMaps[i+rCountI][j]=maps[i][j];
}
}
//3->4
for(int i=rLengthI;i<rEndi;i++){
for(int j=rLengthJ ;j<rEndj;j++){
copyMaps[i][j-rCountJ]=maps[i][j];
}
}
//4->1
for(int i=rLengthI;i<rEndi;i++){
for(int j=rStartj;j<rLengthJ;j++){
copyMaps[i-rCountI][j]=maps[i][j];
}
}
//prints(copyMaps);
}
public static void Rotations(int starti, int startj) {
int rStarti = starti * L;
int rStartj = startj * L;
int[][] temp = new int[L][L];
for (int i = 0; i < L; i++) {
for (int j = 0; j < L; j++) {
temp[j][L - 1 - i] = maps[rStarti + i][rStartj + j];
}
}
for (int i = 0; i < L; i++) {
for (int j = 0; j < L; j++) {
maps[rStarti + i][rStartj + j] = temp[i][j];
}
}
}
public static int bfs(int x, int y) {
if(maps[x][y] <= 0) return 0;
int count = 1;
Queue<Ice> queue = new LinkedList<>();
queue.add(new Ice(x, y));
visited[x][y] = true;
while (!queue.isEmpty()) {
Ice current = queue.poll();
int i = current.i;
int j = current.j;
int[] di = {-1, 1, 0, 0};
int[] dj = {0, 0, -1, 1};
for (int k = 0; k < 4; k++) {
int ni = i + di[k];
int nj = j + dj[k];
if (ni >= 0 && ni < n && nj >= 0 && nj < n && !visited[ni][nj] && maps[ni][nj] > 0) {
queue.add(new Ice(ni, nj));
visited[ni][nj] = true;
count++;
}
}
}
return count;
}
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
n=scan.nextInt();
q=scan.nextInt();
double tmpn=Math.pow(2,n);
n=(int)tmpn;
maps=new int[n][n];
copyMaps=new int[n][n];
bCheck=new boolean[n][n];
initbCheck=new boolean[n][n];
visited = new boolean[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
maps[i][j]=scan.nextInt();
copyMaps[i][j]=maps[i][j];
}
}
for(int t=0;t<q;t++){
L=scan.nextInt();
double tmpL=Math.pow(2,L);
L=(int)tmpL;
//2x2
//4x4
//8x8
int divide=n/L;
for(int i=0;i<divide;i++){
for(int j=0;j<divide;j++){
Rotations(i,j);
}
}
int diri[]= {-1,1,0,0};
int dirj[]= {0,0,-1,1};
ArrayList<Ice> saveIce=new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int iceCnt = 0;
for (int k = 0; k < 4; k++) {
int ni = i + diri[k];
int nj = j + dirj[k];
if (ni < 0 || nj < 0 || ni >= n || nj >= n) continue;
if (maps[ni][nj] > 0) iceCnt++;
}
if (iceCnt < 3) {
saveIce.add(new Ice(i, j));
}
}
}
// 저장된 모든 칸의 얼음을 1 줄입니다.
for (Ice ice : saveIce) {
maps[ice.i][ice.j] = Math.max(0, maps[ice.i][ice.j] - 1);
}
}
int sum=0;
int maxs=0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(maps[i][j]>=0) {
sum+=maps[i][j];
}
}
}
System.out.println(sum);
int maxSizeOfIce = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!visited[i][j] && maps[i][j] > 0) {
maxSizeOfIce = Math.max(maxSizeOfIce, bfs(i, j));
}
}
}
System.out.println(maxSizeOfIce);
//Rotation(0,1);
}
}
명령횟수가 10^9이어서 시간초과가 발생합니다. 그래서 회전의 경우 원복되는 상황은 그대로인걸 감안해서 %를 이용해서 횟수를 줄이면됩니다.
키포인트:
int parms = (2 * (n - 2 * i) + 2 * (m - 2 * i) - 4);
int rotations = k % parms;
package test01;
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static int[][] maps;
public static int[][] copymaps;
public static int n, m, k;
public static void prtints(int[][] maps, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(maps[i][j] + " ");
}
System.out.println();
}
}
public static void rotation(int startr, int endr, int startc, int endc) {
// top
for (int i = startc + 1; i <= endc; i++) {
copymaps[startr][i - 1] = maps[startr][i];
}
// left
for (int i = startr; i < endr; i++) {
copymaps[i + 1][startc] = maps[i][startc];
}
// right
for (int i = startr + 1; i <= endr; i++) {
copymaps[i - 1][endc] = maps[i][endc];
}
// bottom
for (int i = startc; i < endc; i++) {
copymaps[endr][i + 1] = maps[endr][i];
}
for (int i = startr; i <= endr; i++) {
for (int j = startc; j <= endc; j++) {
maps[i][j] = copymaps[i][j];
}
}
}
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
m = scan.nextInt();
k = scan.nextInt();
maps = new int[n][m];
copymaps = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int tmp = scan.nextInt();
maps[i][j] = tmp;
copymaps[i][j] = tmp;
}
}
int ci = Math.min(n, m) / 2;
for (int i = 0; i < ci; i++) {
int parms = (2 * (n - 2 * i) + 2 * (m - 2 * i) - 4);
int rotations = k % parms;
for (int t = 0; t < rotations; t++) {
rotation(i, n - 1 - i, i, m - 1 - i);
}
}
prtints(maps, n, m);
}
}
import java.util.*;
import java.io.*;
public class Main {
public static int A;
public static int B;
public static boolean bOkay=true;
public static ArrayList<String> anws=new ArrayList<>();
public static void Prints(){
System.out.println();
for(int i=0;i<B;i++){
for(int j=0;j<A;j++){
if(maps[i][j].bCheck==true){
System.out.print(1+",");
}else{
System.out.print(0+",");
}
}
System.out.println();
}
System.out.println();
}
public static class Robot{
public int x;
public int y;
public int dir;
public Robot(int x,int y, int dir){
this.x=x;
this.y=y;
this.dir=dir;
}
}
public static class RMaps{
public int rnum;
public boolean bCheck=false;
public RMaps(int rnum, boolean bCheck){
this.rnum=rnum;
this.bCheck=bCheck;
}
}
public static class TestCommand{
public int r;
public char command;
public int loop;
public TestCommand(int r,char command, int loop){
this.r=r;
this.command=command;
this.loop=loop;
}
}
public static void forword(int x, int y,int dir,int rnum){
switch (dir){
case 0:
//n
int checkY=y-1;
if(checkY<0){
//wall
int tmp=rnum+1;
String tmpAnw="Robot "+tmp+" crashes into the wall";
anws.add(tmpAnw);
bOkay=false;
break;
}
if(maps[checkY][x].bCheck==true){
//robot collision
int tmp=rnum+1;
int tmp2=maps[checkY][x].rnum+1;
String tmpAnw="Robot "+tmp+" crashes into robot "+tmp2;
anws.add(tmpAnw);
bOkay=false;
break;
}
robots.get(rnum).y=checkY;
break;
case 1:
//w
int checkX=x-1;
if(checkX<0){
int tmp=rnum+1;
String tmpAnw="Robot "+tmp+" crashes into the wall";
anws.add(tmpAnw);
bOkay=false;
break;
}
if(maps[y][checkX].bCheck==true){
//robot collision
int tmp=rnum+1;
int tmp2=maps[y][checkX].rnum+1;
String tmpAnw="Robot "+tmp+" crashes into robot "+tmp2;
anws.add(tmpAnw);
bOkay=false;
break;
}
robots.get(rnum).x=checkX;
break;
case 2:
//s
int checkY2=y+1;
if(checkY2>=B){
//wall
int tmp=rnum+1;
String tmpAnw="Robot "+tmp+" crashes into the wall";
anws.add(tmpAnw);
bOkay=false;
break;
}
if(maps[checkY2][x].bCheck==true){
//robot collision
int tmp=rnum+1;
int tmp2=maps[checkY2][x].rnum+1;
String tmpAnw="Robot "+tmp+" crashes into robot "+tmp2;
anws.add(tmpAnw);
bOkay=false;
break;
}
robots.get(rnum).y=checkY2;
break;
case 3:
int checkX2=x+1;
if(checkX2>=A){
//wall
int tmp=rnum+1;
String tmpAnw="Robot "+tmp+" crashes into the wall";
anws.add(tmpAnw);
bOkay=false;
break;
}
if(maps[y][checkX2].bCheck==true){
//robot collision
int tmp=rnum+1;
int tmp2=maps[y][checkX2].rnum+1;
String tmpAnw="Robot "+tmp+" crashes into robot "+tmp2;
anws.add(tmpAnw);
bOkay=false;
break;
}
robots.get(rnum).x=checkX2;
break;
default:
break;
}
}
public static RMaps maps[][];
public static ArrayList<Robot> robots=new ArrayList<>();
public static ArrayList<TestCommand> testCommands=new ArrayList<>();
public static void main(String[] args) throws Exception {
Scanner scan= new Scanner(System.in);
A=scan.nextInt(); //5
B=scan.nextInt(); //4
maps=new RMaps[B][A];//4 x 5
int n=scan.nextInt();
int m=scan.nextInt();
for(int i=0;i<B;i++){//y 4
for(int j=0;j<A;j++){//x 5
maps[i][j]=new RMaps(-1,false);
}
}
for(int i=0;i<n;i++){
int x=scan.nextInt()-1; // 5
int y=B-scan.nextInt(); // 4
String dir=scan.next();
int d=0;
if(dir.charAt(0)=='N'){
d=0;
}else if(dir.charAt(0)=='W'){
d=1;
}
else if(dir.charAt(0)=='S'){
d=2;
}
else if(dir.charAt(0)=='E'){
d=3;
}
robots.add(new Robot(x,y,d));
maps[y][x].bCheck=true;
maps[y][x].rnum=i;
}
//Prints();
for(int i=0;i<m;i++){
int r=scan.nextInt();
String command=scan.next();
int loop=scan.nextInt();
testCommands.add(new TestCommand(r,command.charAt(0),loop));
}
for(int t=0;t<testCommands.size();t++){
int rnum=testCommands.get(t).r-1;
char commad=testCommands.get(t).command;
int loop =testCommands.get(t).loop;
int initx=robots.get(rnum).x;
int inity=robots.get(rnum).y;
bOkay=true;
for(int i=0;i<loop;i++){
int x=robots.get(rnum).x;
int y=robots.get(rnum).y;
int dir=robots.get(rnum).dir;
switch(commad){
case 'L':
dir=dir+1;
robots.get(rnum).dir=dir%4;
break;
case 'R':
dir=dir-1;
if(dir<0){
dir=3;
}
robots.get(rnum).dir=dir%4;
break;
case 'F':
forword(x,y,dir,rnum);
if(bOkay==false){
i=loop+1;
continue;
}
break;
default:
break;
}
}
if(bOkay==true){
maps[inity][initx].bCheck=false;
maps[inity][initx].rnum=-1;
maps[robots.get(rnum).y][robots.get(rnum).x].rnum=rnum;
maps[robots.get(rnum).y][robots.get(rnum).x].bCheck=true;
//Prints();
//System.out.println(robots.get(rnum).x+","+robots.get(rnum).y+","+robots.get(rnum).dir);
}
if(anws.size()>0){
break;
}
}
if(anws.size()>0){
System.out.print(anws.get(0));
}else{
System.out.print("OK");
}
}
}
import java.util.*;
import java.io.*;
public class Main {
public static int A;
public static int B;
public static boolean bOkay=true;
public static ArrayList<String> anws=new ArrayList<>();
public static void Prints(){
System.out.println();
for(int i=0;i<B;i++){
for(int j=0;j<A;j++){
if(maps[i][j].bCheck==true){
System.out.print(1+",");
}else{
System.out.print(0+",");
}
}
System.out.println();
}
System.out.println();
}
public static class Robot{
public int x;
public int y;
public int dir;
public Robot(int x,int y, int dir){
this.x=x;
this.y=y;
this.dir=dir;
}
}
public static class RMaps{
public int rnum;
public boolean bCheck=false;
public RMaps(int rnum, boolean bCheck){
this.rnum=rnum;
this.bCheck=bCheck;
}
}
public static class TestCommand{
public int r;
public char command;
public int loop;
public TestCommand(int r,char command, int loop){
this.r=r;
this.command=command;
this.loop=loop;
}
}
public static void forword(int x, int y,int dir,int rnum){
switch (dir){
case 0:
//n
int checkY=y-1;
if(checkY<0){
//wall
int tmp=rnum+1;
String tmpAnw="Robot "+tmp+" crashes into the wall";
anws.add(tmpAnw);
bOkay=false;
break;
}
if(maps[checkY][x].bCheck==true){
//robot collision
int tmp=rnum+1;
int tmp2=maps[checkY][x].rnum+1;
String tmpAnw="Robot "+tmp+" crashes into robot "+tmp2;
anws.add(tmpAnw);
bOkay=false;
break;
}
robots.get(rnum).y=checkY;
break;
case 1:
//w
int checkX=x-1;
if(checkX<0){
int tmp=rnum+1;
String tmpAnw="Robot "+tmp+" crashes into the wall";
anws.add(tmpAnw);
bOkay=false;
break;
}
if(maps[y][checkX].bCheck==true){
//robot collision
int tmp=rnum+1;
int tmp2=maps[y][checkX].rnum+1;
String tmpAnw="Robot "+tmp+" crashes into robot "+tmp2;
anws.add(tmpAnw);
bOkay=false;
break;
}
robots.get(rnum).x=checkX;
break;
case 2:
//s
int checkY2=y+1;
if(checkY2>=B){
//wall
int tmp=rnum+1;
String tmpAnw="Robot "+tmp+" crashes into the wall";
anws.add(tmpAnw);
bOkay=false;
break;
}
if(maps[checkY2][x].bCheck==true){
//robot collision
int tmp=rnum+1;
int tmp2=maps[checkY2][x].rnum+1;
String tmpAnw="Robot "+tmp+" crashes into robot "+tmp2;
anws.add(tmpAnw);
bOkay=false;
break;
}
robots.get(rnum).y=checkY2;
break;
case 3:
int checkX2=x+1;
if(checkX2>=A){
//wall
int tmp=rnum+1;
String tmpAnw="Robot "+tmp+" crashes into the wall";
anws.add(tmpAnw);
bOkay=false;
break;
}
if(maps[y][checkX2].bCheck==true){
//robot collision
int tmp=rnum+1;
int tmp2=maps[y][checkX2].rnum+1;
String tmpAnw="Robot "+tmp+" crashes into robot "+tmp2;
anws.add(tmpAnw);
bOkay=false;
break;
}
robots.get(rnum).x=checkX2;
break;
default:
break;
}
}
public static RMaps maps[][];
public static ArrayList<Robot> robots=new ArrayList<>();
public static ArrayList<TestCommand> testCommands=new ArrayList<>();
public static void main(String[] args) throws Exception {
Scanner scan= new Scanner(System.in);
A=scan.nextInt(); //5
B=scan.nextInt(); //4
maps=new RMaps[B][A];//4 x 5
int n=scan.nextInt();
int m=scan.nextInt();
for(int i=0;i<B;i++){//y 4
for(int j=0;j<A;j++){//x 5
maps[i][j]=new RMaps(-1,false);
}
}
for(int i=0;i<n;i++){
int x=scan.nextInt()-1; // 5
int y=B-scan.nextInt(); // 4
String dir=scan.next();
int d=0;
if(dir.charAt(0)=='N'){
d=0;
}else if(dir.charAt(0)=='W'){
d=1;
}
else if(dir.charAt(0)=='S'){
d=2;
}
else if(dir.charAt(0)=='E'){
d=3;
}
robots.add(new Robot(x,y,d));
maps[y][x].bCheck=true;
maps[y][x].rnum=i;
}
//Prints();
for(int i=0;i<m;i++){
int r=scan.nextInt();
String command=scan.next();
int loop=scan.nextInt();
testCommands.add(new TestCommand(r,command.charAt(0),loop));
}
for(int t=0;t<testCommands.size();t++){
int rnum=testCommands.get(t).r-1;
char commad=testCommands.get(t).command;
int loop =testCommands.get(t).loop;
int initx=robots.get(rnum).x;
int inity=robots.get(rnum).y;
bOkay=true;
for(int i=0;i<loop;i++){
int x=robots.get(rnum).x;
int y=robots.get(rnum).y;
int dir=robots.get(rnum).dir;
switch(commad){
case 'L':
dir=dir+1;
robots.get(rnum).dir=dir%4;
break;
case 'R':
dir=dir-1;
if(dir<0){
dir=3;
}
robots.get(rnum).dir=dir%4;
break;
case 'F':
forword(x,y,dir,rnum);
if(bOkay==false){
i=loop+1;
continue;
}
break;
default:
break;
}
}
if(bOkay==true){
maps[inity][initx].bCheck=false;
maps[inity][initx].rnum=-1;
maps[robots.get(rnum).y][robots.get(rnum).x].rnum=rnum;
maps[robots.get(rnum).y][robots.get(rnum).x].bCheck=true;
//Prints();
//System.out.println(robots.get(rnum).x+","+robots.get(rnum).y+","+robots.get(rnum).dir);
}
if(anws.size()>0){
break;
}
}
if(anws.size()>0){
System.out.print(anws.get(0));
}else{
System.out.print("OK");
}
}
}
먼저 hibernate 란 뭘까? Hibernate는 자바 기반의 객체 관계 매핑(ORM) 라이브러리입니다. ORM은 객체 지향 프로그래밍 언어를 사용하여 데이터베이스와의 상호작용을 추상화하는 프로그래밍 기법 중 하나로, 개발자가 객체 지향적인 방식으로 데이터베이스를 다룰 수 있게 해줍니다. Hibernate는 이 ORM을 구현한 대표적인 프레임워크 중 하나입니다. Hibernate의 주요 목적은 Java 어플리케이션에서 사용하는 데이터베이스 작업을 단순화하고, 데이터베이스와 객체 사이의 불일치를 해결하는 것입니다. 이를 통해 개발자는 복잡한 JDBC(Java Database Connectivity) 코드를 작성하는 대신, 더 간결하고 객체 지향적인 코드로 데이터베이스 작업을 수행할 수 있습니다. -chat gpt- chat gpt 선생님께서는 이렇게 설명하신다.역시나 정확하시다.