편집 : 여기
는 하스켈 솔루션 (I 함수 여기 행렬을 교체하고 있습니다 :
class Pair {
public Pair(int x, int y) { this.x = x; this.y = y; }
public int x;
public int y;
};
class Main {
static Vector<Pair> singleton(Pair p) {
Vector<Pair> v = new Vector<Pair>();
v.insert(p);
return v;
}
static void f(String str, int [][]matrix, int width, int height) {
Vector<Vector<Pair>> v = new Vector<Vector<Pair>>();
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
try(i, j, str, matrix, width, height, v);
}
static void try(int i, int j, String str, int [][]matrix, int width, int height, Vector<Vector<Pair>> v) {
int old_v_size = v.length;
if (i < 0 || i >= height || j < 0 || j >= width) return;
if (str.length == 1) v.insert(singleton(new Pair(i,j));
try(i+1,j,str.substr(1),matrix,width,height,v);
try(i-1,j,str.substr(1),matrix,width,height,v);
try(i,j+1,str.substr(1),matrix,width,height,v);
try(i,j-1,str.substr(1),matrix,width,height,v);
for (int k = old_v_size; k < v.length; ++k) v[k].insert(new Pair(i,j));
}
}
올드 아래 코드는 다음과 느린 반 자바 반 의사 코드 버전을 추가 두 개의 정수를 가져 와서 행렬의 값을 반환합니다). 함수 f는 문자열, 행렬 함수, 너비 및 높이를 받아들이고 각 가능한 솔루션에 대한 튜플 목록 목록을 반환합니다.
f str matrix width height =
concat [try i j str matrix width height | i <- [0..width-1], j <- [0..height-1] ]
try i j (c:cs) matrix width height | i < 0 ||
i >= height ||
j < 0 ||
j >= width ||
matrix i j /= c = []
try i j [c] matrix width height = [(i,j)]
try i j (c:cs) matrix width height =
concat [map ((i,j):) $ try (i+1) j cs matrix width height,
map ((i,j):) $ try (i-1) j cs matrix width height,
map (c:) $ try i (j+1) cs matrix width height,
map (c:) $ try i (j-1) cs matrix width height]
당신은 다른 언어를 지정하세요 원한다면 나는 다른 해결책 자바에 대한
어떻게 보낼 것인가? –
자바를 잘 기억하지 못한다. 그러나 자바에서 다시 쓰기를 시도했다. – tohava