#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int n; | |
int** a; | |
int** stf; | |
int jgd(int r, int c, int x) { | |
int d[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; | |
int rst = 0; | |
for (int i = 0; i < 4; i++) { | |
int cr = r + d[i][0]; | |
int cc = c + d[i][1]; | |
if (0 > cr || cr >= n || 0 > cc || cc >= n) continue; | |
if (x == 0) { | |
if (a[cr][cc] == 0) rst++; | |
} | |
else { | |
for (int j = 0; j < 4; j++) { | |
if (a[cr][cc] == stf[x][j]) rst++; | |
} | |
} | |
} | |
return rst; | |
} | |
void sst(int x) { | |
int scr[n][n] = {}; | |
int pp[n * n][2]; | |
int max = 0, p = 0, p2 = 0; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
if (a[i][j] == 0) { | |
scr[i][j] = jgd(i, j, x); | |
if (scr[i][j] > max) max = scr[i][j]; | |
} | |
else scr[i][j] = -1; | |
} | |
} | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
if (scr[i][j] == max) { | |
pp[p][0] = i; | |
pp[p][1] = j; | |
p++; | |
} | |
} | |
} | |
if (p == 1) { | |
a[pp[0][0]][pp[0][1]] = x; | |
return; | |
} | |
max = 0; | |
for (int i = 0; i < p; i++) { | |
scr[pp[i][0]][pp[i][1]] = jgd(pp[i][0], pp[i][1], 0); | |
if (scr[pp[i][0]][pp[i][1]] > max) max = scr[pp[i][0]][pp[i][1]]; | |
} | |
for (int i = 0; i < p; i++) { | |
if (scr[pp[i][0]][pp[i][1]] == max) { | |
pp[p2][0] = pp[i][0]; | |
pp[p2][1] = pp[i][1]; | |
p2++; | |
} | |
} | |
a[pp[0][0]][pp[0][1]] = x; | |
} | |
int main() { | |
ios_base::sync_with_stdio(false); | |
cin.tie(NULL); | |
int rst = 0; | |
cin >> n; | |
a = (int**)malloc(sizeof(int*) * n); | |
stf = (int**)malloc(sizeof(int*) * (n * n + 1)); | |
for (int i = 0; i < n; i++) a[i] = (int*)malloc(sizeof(int) * n); | |
for (int i = 1; i <= n * n; i++) stf[i] = (int*)malloc(sizeof(int) * 4); | |
int stn[n * n]; | |
for (int i = 0; i < n * n; i++) { | |
cin >> stn[i]; | |
for (int j = 0; j < 4; j++) cin >> stf[stn[i]][j]; | |
} | |
for (int x : stn) sst(x); | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) rst += pow(10, jgd(i, j, a[i][j]) - 1); | |
} | |
cout << rst; | |
for (int i = 0; i < n; i++) free(a[i]); | |
for (int i = 1; i <= n * n; i++) free(stf[i]); | |
free(a); | |
free(stf); | |
return 0; | |
} |
using System; | |
class Program { | |
static int n; | |
static int[,] a; | |
static int[,] stf; | |
static int jgd(int r, int c, int x) { | |
int[,] d = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; | |
int rst = 0; | |
for (int i = 0; i < 4; i++) { | |
int cr = r + d[i, 0]; | |
int cc = c + d[i, 1]; | |
if (0 > cr || cr >= n || 0 > cc || cc >= n) continue; | |
if (x == 0) { | |
if (a[cr, cc] == 0) rst++; | |
} | |
else { | |
for (int j = 0; j < 4; j++) { | |
if (a[cr, cc] == stf[x, j]) rst++; | |
} | |
} | |
} | |
return rst; | |
} | |
static void sst(int x) { | |
int[,] scr = new int[n, n]; | |
int[,] pp = new int[n * n, 2]; | |
int max = 0, p = 0, p2 = 0; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
if (a[i, j] == 0) { | |
scr[i, j] = jgd(i, j, x); | |
if (scr[i, j] > max) max = scr[i, j]; | |
} | |
else scr[i, j] = -1; | |
} | |
} | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
if (scr[i, j] == max) { | |
pp[p, 0] = i; | |
pp[p, 1] = j; | |
p++; | |
} | |
} | |
} | |
if (p == 1) { | |
a[pp[0, 0], pp[0, 1]] = x; | |
return; | |
} | |
max = 0; | |
for (int i = 0; i < p; i++) { | |
scr[pp[i, 0], pp[i, 1]] = jgd(pp[i, 0], pp[i, 1], 0); | |
if (scr[pp[i, 0], pp[i, 1]] > max) max = scr[pp[i, 0], pp[i, 1]]; | |
} | |
for (int i = 0; i < p; i++) { | |
if (scr[pp[i, 0], pp[i, 1]] == max) { | |
pp[p2, 0] = pp[i, 0]; | |
pp[p2, 1] = pp[i, 1]; | |
p2++; | |
} | |
} | |
a[pp[0, 0], pp[0, 1]] = x; | |
} | |
public static void Main() { | |
int rst = 0; | |
n = int.Parse(Console.ReadLine()); | |
a = new int[n, n]; | |
stf = new int[n * n + 1, 4]; | |
int[] stn = new int[n * n]; | |
for (int i = 0; i < n * n; i++) { | |
string[] s = Console.ReadLine().Split(' '); | |
stn[i] = int.Parse(s[0]); | |
for (int j = 0; j < 4; j++) stf[stn[i], j] = int.Parse(s[j + 1]); | |
} | |
foreach (int x in stn) sst(x); | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) rst += (int)Math.Pow(10, (jgd(i, j, a[i, j]) - 1)); | |
} | |
Console.Write(rst); | |
} | |
} |
import java.util.Scanner; | |
import java.util.Arrays; | |
class Main { | |
static int n; | |
static int[][] a; | |
static int[][] stf; | |
static int jgd(int r, int c, int x) { | |
int[][] d = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; | |
int rst = 0; | |
for (int i = 0; i < 4; i++) { | |
int cr = r + d[i][0]; | |
int cc = c + d[i][1]; | |
if (0 > cr || cr >= n || 0 > cc || cc >= n) continue; | |
if (x == 0) { | |
if (a[cr][cc] == 0) rst++; | |
} | |
else { | |
for (int j = 0; j < 4; j++) { | |
if (a[cr][cc] == stf[x][j]) rst++; | |
} | |
} | |
} | |
return rst; | |
} | |
static void sst(int x) { | |
int[][] scr = new int[n][n]; | |
int[][] pp = new int[n * n][2]; | |
int max = 0, p = 0, p2 = 0; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
if (a[i][j] == 0) { | |
scr[i][j] = jgd(i, j, x); | |
if (scr[i][j] > max) max = scr[i][j]; | |
} | |
else scr[i][j] = -1; | |
} | |
} | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
if (scr[i][j] == max) { | |
pp[p][0] = i; | |
pp[p][1] = j; | |
p++; | |
} | |
} | |
} | |
if (p == 1) { | |
a[pp[0][0]][pp[0][1]] = x; | |
return; | |
} | |
max = 0; | |
for (int i = 0; i < p; i++) { | |
scr[pp[i][0]][pp[i][1]] = jgd(pp[i][0], pp[i][1], 0); | |
if (scr[pp[i][0]][pp[i][1]] > max) max = scr[pp[i][0]][pp[i][1]]; | |
} | |
for (int i = 0; i < p; i++) { | |
if (scr[pp[i][0]][pp[i][1]] == max) { | |
pp[p2][0] = pp[i][0]; | |
pp[p2][1] = pp[i][1]; | |
p2++; | |
} | |
} | |
a[pp[0][0]][pp[0][1]] = x; | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int rst = 0; | |
n = sc.nextInt(); | |
a = new int[n][n]; | |
stf = new int[n * n + 1][4]; | |
int[] stn = new int[n * n]; | |
for (int i = 0; i < n * n; i++) { | |
stn[i] = sc.nextInt(); | |
for (int j = 0; j < 4; j++) stf[stn[i]][j] = sc.nextInt(); | |
} | |
for (int x : stn) sst(x); | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) rst += Math.pow(10, jgd(i, j, a[i][j]) - 1); | |
} | |
System.out.print(rst); | |
} | |
} |
import sys | |
input = sys.stdin.readline | |
def jgd(r, c, x): | |
d = [[-1, 0], [0, 1], [1, 0], [0, -1]] | |
rst = 0 | |
for i in range(len(d)): | |
cr = r + d[i][0] | |
cc = c + d[i][1] | |
if 0 <= cr < n and 0 <= cc < n: | |
if x <= 0: | |
if a[cr][cc] == 0: | |
rst += 1 | |
else: | |
if a[cr][cc] in stf[x]: | |
rst += 1 | |
return rst | |
def sst(x): | |
scr = [[-1 for _ in range(n)] for _ in range(n)] | |
pp = [] | |
mmax = 0 | |
for i in range(n): | |
for j in range(n): | |
if a[i][j] == 0: | |
scr[i][j] = jgd(i, j, x) | |
if scr[i][j] > mmax: | |
mmax = scr[i][j] | |
pp = [] | |
if scr[i][j] == mmax: pp.append([i, j]) | |
if len(pp) == 1: | |
a[pp[0][0]][pp[0][1]] = x | |
return | |
mmax = t = 0 | |
for i in range(len(pp)): | |
scr[pp[i][0]][pp[i][1]] = jgd(pp[i][0], pp[i][1], 0) | |
if scr[pp[i][0]][pp[i][1]] > mmax: | |
mmax = scr[pp[i][0]][pp[i][1]] | |
t = i | |
a[pp[t][0]][pp[t][1]] = x | |
n = int(input()) | |
a = [[0 for _ in range(n)] for _ in range(n)] | |
rst = 0 | |
stn = [] | |
stf = [[] for _ in range(n * n + 1)] | |
for i in range(n * n): | |
str = list(map(int, input().split())) | |
stn.append(str[0]) | |
stf[str[0]] = str[1:] | |
for x in stn: | |
sst(x) | |
for i in range(n): | |
for j in range(n): | |
t = jgd(i, j, a[i][j]) | |
rst += (0 if t == 0 else 10 ** (t - 1)) | |
print(rst) |
import java.util.Scanner | |
import kotlin.math.* | |
var n: Int = 0 | |
var a = Array<Array<Int>>(0) { Array<Int>(0) { 0 } } | |
var stf = Array<Array<Int>>(0) { Array<Int>(0) { 0 } } | |
fun jgd(r: Int, c: Int, x: Int): Int { | |
val d = arrayOf(arrayOf(-1, 0), arrayOf(0, 1), arrayOf(1, 0), arrayOf(0, -1)) | |
var rst: Int = 0 | |
for (i in 0..3) { | |
val (cr, cc) = arrayOf(r + d[i][0], c + d[i][1]) | |
if (0 <= cr && cr < n && 0 <= cc && cc < n) { | |
if (x <= 0) { | |
if (a[cr][cc] == 0) rst++ | |
} | |
else { | |
if (a[cr][cc] in stf[x]) rst++ | |
} | |
} | |
} | |
return rst | |
} | |
fun sst(x: Int) { | |
var scr = Array<Array<Int>>(n) { Array<Int>(n) { -1 } } | |
var pp = Array<Array<Int>>(n * n) { Array<Int>(2) { 0 } } | |
var (mmax, p, t) = arrayOf(0, 0, 0) | |
for (i in 0 until n) { | |
for (j in 0 until n) { | |
if (a[i][j] == 0) { | |
scr[i][j] = jgd(i, j, x) | |
if (scr[i][j] > mmax) { | |
mmax = scr[i][j] | |
p = 0 | |
} | |
if (scr[i][j] == mmax) { | |
pp[p][0] = i | |
pp[p][1] = j | |
p++ | |
} | |
} | |
} | |
} | |
if (p == 1) { | |
a[pp[0][0]][pp[0][1]] = x | |
return | |
} | |
mmax = 0 | |
for (i in 0 until p) { | |
scr[pp[i][0]][pp[i][1]] = jgd(pp[i][0], pp[i][1], 0) | |
if (scr[pp[i][0]][pp[i][1]] > mmax) { | |
mmax = scr[pp[i][0]][pp[i][1]] | |
t = i | |
} | |
} | |
a[pp[t][0]][pp[t][1]] = x | |
} | |
fun main() { | |
val sc = Scanner(System.`in`) | |
var rst: Int = 0 | |
n = sc.nextInt() | |
a = Array<Array<Int>>(n) { Array<Int>(n) { 0 } } | |
stf = Array<Array<Int>>(n * n + 1) { Array<Int>(4) { 0 } } | |
var stn = Array<Int>(n * n) { 0 } | |
for (i in 0 until n * n) { | |
stn[i] = sc.nextInt() | |
for (j in 0..3) stf[stn[i]][j] = sc.nextInt() | |
} | |
for (x in stn) sst(x) | |
for (i in 0 until n) { | |
for (j in 0 until n) rst += 10.0.pow((jgd(i, j, a[i][j]) - 1).toDouble()).toInt() | |
} | |
print(rst) | |
} |
function jgd(r, c, x) { | |
const d = [ [ -1, 0 ], [ 0, 1 ], [ 1, 0 ], [ 0, -1 ] ] | |
let rst = 0 | |
for (let i = 0; i < 4; i++) { | |
const cr = r + d[i][0], cc = c + d[i][1] | |
if (0 <= cr && cr < n && 0 <= cc && cc < n) { | |
if (x <= 0) { | |
if (a[cr][cc] == 0) rst++ | |
} | |
else { | |
for (let j = 0; j < 4; j++) { | |
if (a[cr][cc] == stf[x][j]) rst++ | |
} | |
} | |
} | |
} | |
return rst | |
} | |
function sst(x) { | |
let scr = Array.from(Array(n), () => Array(n).fill(-1)) | |
let pp = [] | |
let mmax = 0, t = 0 | |
for (let i = 0; i < n; i++) { | |
for (let j = 0; j < n; j++) { | |
if (a[i][j] == 0) { | |
scr[i][j] = jgd(i, j, x) | |
if (scr[i][j] > mmax) { | |
mmax = scr[i][j] | |
pp = [] | |
} | |
if (scr[i][j] == mmax) pp.push([i, j]) | |
} | |
} | |
} | |
if (pp.length == 1) { | |
a[pp[0][0]][pp[0][1]] = x | |
return | |
} | |
mmax = 0 | |
for (let i = 0; i < pp.length; i++) { | |
scr[pp[i][0]][pp[i][1]] = jgd(pp[i][0], pp[i][1], -1) | |
if (scr[pp[i][0]][pp[i][1]] > mmax) { | |
mmax = scr[pp[i][0]][pp[i][1]] | |
t = i | |
} | |
} | |
a[pp[t][0]][pp[t][1]] = x | |
} | |
const ip = require('fs').readFileSync(0).toString().trim().split('\n') | |
const n = parseInt(ip[0]) | |
let ans = 0 | |
let a = Array.from(Array(n), () => Array(n).fill(0)) | |
let stn = [] | |
let stf = Array.from(Array(n * n + 1), () => Array(0).fill(0)) | |
for (let i = 1; i <= n * n; i++) { | |
const s = ip[i].split(' ') | |
stn.push(parseInt(s[0])) | |
for (let j = 0; j < 4; j++) stf[s[0]].push(parseInt(s[j + 1])) | |
} | |
for (x of stn) sst(x) | |
for (let i = 0; i < n; i++) { | |
for (let j = 0; j < n; j++) { | |
t = jgd(i, j, a[i][j]) | |
ans += (t == 0) ? 0 : 10 ** (t - 1) | |
} | |
} | |
console.log(ans) |
import 'dart:io'; | |
import 'dart:math'; | |
int n = 0; | |
List<List<int>> a = [[]]; | |
List<List<int>> stf = [[]]; | |
int jgd(int r, int c, int x) { | |
List<List<int>> d = [ [ -1, 0 ], [ 0, 1 ], [ 1, 0 ], [ 0, -1 ] ]; | |
int rst = 0; | |
for (int i = 0; i < 4; i++) { | |
int cr = r + d[i][0], cc = c + d[i][1]; | |
if ((0 <= cr && cr < n) && (0 <= cc && cc < n)) { | |
if (x <= 0) { | |
if (a[cr][cc] == 0) rst++; | |
} | |
else { | |
for (int j = 0; j < 4; j++) { | |
if (a[cr][cc] == stf[x][j]) rst++; | |
} | |
} | |
} | |
} | |
return rst; | |
} | |
void sst(int x) { | |
List<List<int>> scr = List<List<int>>.generate(n, (i) => List<int>.filled(n, -1)); | |
List<List<int>> pp = List<List<int>>.generate(n * n, (i) => List<int>.filled(2, 0)); | |
int mmax = 0, p = 0, t = 0; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
if (a[i][j] == 0) { | |
scr[i][j] = jgd(i, j, x); | |
if (scr[i][j] > mmax) { | |
mmax = scr[i][j]; | |
p = 0; | |
} | |
if (scr[i][j] == mmax) { | |
pp[p][0] = i; | |
pp[p][1] = j; | |
p++; | |
} | |
} | |
} | |
} | |
if (p == 1) { | |
a[pp[0][0]][pp[0][1]] = x; | |
return; | |
} | |
mmax = 0; | |
for (int i = 0; i < p; i++) { | |
scr[pp[i][0]][pp[i][1]] = jgd(pp[i][0], pp[i][1], -1); | |
if (scr[pp[i][0]][pp[i][1]] > mmax) { | |
mmax = scr[pp[i][0]][pp[i][1]]; | |
t = i; | |
} | |
} | |
a[pp[t][0]][pp[t][1]] = x; | |
} | |
void main() { | |
int rst = 0; | |
n = int.parse(stdin.readLineSync()!); | |
a = List<List<int>>.generate(n, (i) => List<int>.filled(n, 0)); | |
stf = List<List<int>>.generate(n * n + 1, (i) => []); | |
List<int> stn = []; | |
for (int i = 0; i < n * n; i++) { | |
List<String> s = stdin.readLineSync()!.split(' '); | |
stn.add(int.parse(s[0])); | |
for (int j = 0; j < 4; j++) stf[stn[i]].add(int.parse(s[j + 1])); | |
} | |
for (int x in stn) sst(x); | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
int t = jgd(i, j, a[i][j]); | |
rst += (t == 0) ? 0 : pow(10, t - 1).toInt(); | |
} | |
} | |
print(rst); | |
} |
문제를 이해하는 것이 중요하고 이해한 것에 맞춰 구현해야 한다.
나는 규칙을 구현하는 함수와 배치된 자리의 만족도를 계산하는 함수를 만들어 문제를 해결했다.
문제 규칙을 정확하게 준수하는 함수를 만드는데 집중했다.