๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 2 BOJ #16927

by seolhee2750 2022. 8. 10.
๋ฌธ์ œ

https://www.acmicpc.net/problem/16927

 

16927๋ฒˆ: ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 2

ํฌ๊ธฐ๊ฐ€ N×M์ธ ๋ฐฐ์—ด์ด ์žˆ์„ ๋•Œ, ๋ฐฐ์—ด์„ ๋Œ๋ ค๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ๋ฐฐ์—ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ ๋Œ๋ ค์•ผ ํ•œ๋‹ค. A[1][1] ← A[1][2] ← A[1][3] ← A[1][4] ← A[1][5] ↓ ↑ A[2][1] A[2][2] ← A[2][3] ← A[2][4] A[2][5]

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	
	static int[][] nums;
	static int n, m, r;
	static int[] dx = {0, 1, 0, -1};
	static int[] dy = {1, 0, -1, 0};
	static int nNext, mNext; // ๋ ๋งˆ๋‹ค ์ค„์–ด๋“œ๋Š” ๋ณ€์˜ ๊ธธ์ด ์ €์žฅ์„ ์œ„ํ•จ
	
	public static void turn(int start, int nowTurnNum) {
		for(int i = 0; i < nowTurnNum ; i++) { // ์œ ํšจ ํšŒ์ „ ์ˆ˜ ๋งŒํผ๋งŒ ํšŒ์ „
			int x = start;
			int y = start;
			int tmp = nums[start][start];
			int dir = 0; // ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚  ๋•Œ๋งˆ๋‹ค dir์„ ๋ฐ”๊ฟ”์ฃผ์–ด ๋ฐฉํ–ฅ ์ „ํ™˜
			
			while(dir < 4) {
				int nx = x + dx[dir];
				int ny = y + dy[dir];
				
				if(nx < start || ny < start || nx >= n-start || ny >= m-start) { // ๋ฒ”์œ„ ๋ฒ—์–ด๋‚  ์‹œ ๋ฐฉํ–ฅ ์ „ํ™˜
					dir++; 
					continue;
				}
				
				nums[x][y] = nums[nx][ny];
				x = nx;
				y = ny;
			}
			
			nums[start+1][start] = tmp;
		}
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuffer out = new StringBuffer();
		
		st = new StringTokenizer(in.readLine());
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		nNext = n;
		mNext = m;
		r = Integer.parseInt(st.nextToken());
		int turnNum = Math.min(n, m) / 2; // ํšŒ์ „์‹œ์ผœ์•ผ ํ•˜๋Š” ๋ ์˜ ๊ฐœ์ˆ˜
		nums = new int[n][m];
		
		for(int i = 0; i < n; i++) {
			st = new StringTokenizer(in.readLine());
			for(int j = 0; j < m; j++) nums[i][j] = Integer.parseInt(st.nextToken());
		}
		
		for(int i = 0; i < turnNum; i++) { // ๋‚ด๋ถ€๋กœ ๋“ค์–ด๊ฐ€๋ฉด์„œ ๋ชจ๋“  ๋  ์ˆœํšŒ
			turn(i, r % ((nNext + mNext - 2) * 2));
			nNext -= 2;
			mNext -= 2;
		}
		
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				out.append(nums[i][j]);
				if(j < m-1) out.append(" ");
			}
			out.append("\n");
		}
		System.out.println(out);
	}

}

๐Ÿ‘‰ ๊ตฌํ˜„ ๋ฌธ์ œ

 

[ ๋กœ์ง ์„ค๋ช… ]

โœ”๏ธ ํšŒ์ „ํ•˜๋Š” ๋ฐฐ์—ด์€ ๊ฐ€์žฅ ๋ฐ”๊นฅ์—์„œ๋ถ€ํ„ฐ ์•ˆ์ชฝ์œผ๋กœ ํ•œ ์นธ์”ฉ์˜ ๋ ๋ฅผ ํ˜•์„ฑํ•˜๊ณ ,
๊ฐ™์€ ๋ ๋“ค์ด ํ•จ๊ป˜ ๊ทธ๋ฃน์„ ํ˜•์„ฑํ•˜์—ฌ ํšŒ์ „ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

 

โœ”๏ธ ๋‚˜๋Š” ํ•˜๋‚˜์˜ ๋ ๋งˆ๋‹ค ๊ฐ€์žฅ ์ขŒ์ธก ์ƒ๋‹จ์„ ๊ผญ์ง“์ ์œผ๋กœ ๋ณด๊ณ , ๊ผญ์ง“์ ์— ์ €์žฅ๋œ ๊ฐ’์„ tmp์— ๋„ฃ์–ด๋†“์€ ๋’ค
์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ์ž๋ฆฌ๋ฅผ ์˜ฎ๊ฒจ์ฃผ๋Š” ์‹์œผ๋กœ ๋ฐฐ์—ด์„ ํšŒ์ „์‹œ์ผฐ๋‹ค.

 

โœ”๏ธ ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€, ์ด์™€ ๋น„์Šทํ•œ #16926๋ฒˆ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ์ง„์งœ ํšŒ์ „๋งŒ ์‹œ์ผœ๋„ ๊ดœ์ฐฎ๋‹ค.
ํ•˜์ง€๋งŒ ์ด ๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ ํšŒ์ „์— ๋Œ€ํ•œ ์ž…๋ ฅ ๋ฒ”์œ„๊ฐ€ ์•„์ฃผ ์ปค์กŒ๊ณ , ํšจ์œจ์ด ๊ด€๊ฑด์ด๋‹ค.

 

โœ”๏ธ ๋”ฐ๋ผ์„œ ๋‚˜๋Š” ์•„์ฃผ ๋งŽ์ด ํšŒ์ „ํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ์ค‘๋ณต๋œ ํšŒ์ „์„ ํ”ผํ•˜๊ณ ์ž ํ–ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ๊ทธ๋ฆผ ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋ฐ”๊นฅ ์ชฝ ๋ ๋Š” 12๋ฒˆ ํšŒ์ „ ์‹œ ์›์ƒ๋ณต๊ตฌ๋˜์–ด ํšŒ์ „์˜ ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.
=> 14๋ฒˆ ํšŒ์ „ํ•˜๋ผ๊ณ  ์ฃผ์–ด์ง„๋‹ค๋ฉด ๊ทธ ์ค‘ 12๋ฒˆ์„ ์ œ์™ธํ•œ 2๋ฒˆ๋งŒ ํšŒ์ „ํ•˜์—ฌ๋„ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

โœ”๏ธ ์ด์— ๋‚˜๋Š” ๋ ๋งˆ๋‹ค ์œ ํšจํ•œ ํšŒ์ „ ์ˆ˜์— ๋Œ€ํ•œ ์ ํ™”์‹์„ ๊ตฌํ•˜์˜€๋‹ค.
๋ ๋งˆ๋‹ค ์›์ƒ๋ณต๊ท€๋˜๋Š” ํšŒ์ „ ์ˆ˜ : (์„ธ๋กœ ๊ธธ์ด + ๊ฐ€๋กœ ๊ธธ์ด - 2) * 2
=> ๋ ๋งˆ๋‹ค ์œ ํšจํ•œ ํšŒ์ „ ์ˆ˜ : R % ((์„ธ๋กœ ๊ธธ์ด + ๊ฐ€๋กœ ๊ธธ์ด - 2) * 2

 

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๋„ˆ๋ฌด๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ ์‹œ๋ฆฌ์ฆˆ๋ฅผ ํ’€์–ด๋ณด๋Š” ์ค‘์ธ๋ฐ,
    ๊ฝค ํ’€๊ธฐ ์–ด์ง€๋Ÿฌ์šด ๋ฌธ์ œ๋“ค์ด์ง€๋งŒ,, ํ™•์‹คํžˆ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™๊ธดํ•˜๋‹ค.
    ๋ฌธ์ œ์ง‘: ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ (์‹œ๋ฆฌ์ฆˆ) (acmicpc.net)

๋Œ“๊ธ€