본문 바로가기

etc

dsdfsdfsdf

반응형

import java.util.ArrayList;


public class Algo2 {

static int[][] matrix;

static int[][] visited;

static boolean r;

static boolean l;

static boolean u;

static boolean d;

static boolean ru;

static boolean rd;

static boolean lu;

static boolean ld;

static int[] count;

static int countIndex;

static int n;

public static void main(String[] args) {

n = 10;

matrix = new int[][] { 

{ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 1, 0, 0, 1, 0, 0, 1, 1, 1 },

{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 },

{ 0, 0, 0, 0, 1, 1, 1, 0, 0, 1 },

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 1, 1, 0, 0, 1, 0, 1, 0, 0 },

{ 1, 0, 1, 0, 0, 1, 0, 1, 0, 0 },

{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }};


visited = new int[n][n];

count = new int[n];

countIndex = -1;


for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

visitThis(new Pair(i, j), true);

}

}

for (int i = 0; i < count.length; i++) {

System.out.println("Count " + i + ":" + count[i]);

}

}


private static void visitThis(Pair pair, boolean isHead) {

int i = pair.i;

int j = pair.j;


if ((matrix[i][j] == 1) && (visited[i][j] != 1)) {

if (isHead) {

System.out.println("Is HEAD:" + i + "," + j);

countIndex++;

System.out.println("Count index :" + countIndex);

count[countIndex] = 1;

} else {

count[countIndex]++;

}

visited[i][j] = 1;

ArrayList<Pair> alist = getNeighbor(pair);

System.out.println(i + "," + j);

if (alist.size() > 0) {

for (int k = 0; k < alist.size(); k++) {

Pair newPair = alist.get(k);

visitThis(newPair, false);

}

}

}

}


private static ArrayList<Pair> getNeighbor(Pair current) {

ArrayList<Pair> resultList = new ArrayList<Pair>();

Pair anew;


if ((anew = getLeft(current)) != null) {

resultList.add(anew);

}

if ((anew = getRight(current)) != null) {

resultList.add(anew);

}


if ((anew = getUp(current)) != null) {

resultList.add(anew);

}


if ((anew = getDown(current)) != null) {

resultList.add(anew);

}


if ((anew = getLeftUp(current)) != null) {

resultList.add(anew);

}


if ((anew = getLeftDown(current)) != null) {

resultList.add(anew);

}


if ((anew = getRightUp(current)) != null) {

resultList.add(anew);

}


if ((anew = getRightDown(current)) != null) {

resultList.add(anew);

}


return resultList;


}


private static Pair getRightDown(Pair current) {

if (r && d) {

return new Pair(current.i + 1, current.j + 1);

}

return null;

}


private static Pair getRightUp(Pair current) {

if (r && u) {

return new Pair(current.i - 1, current.j + 1);

}

return null;

}


private static Pair getLeftDown(Pair current) {

if (l && d) {

return new Pair(current.i + 1, current.j - 1);

}

return null;

}


private static Pair getLeftUp(Pair current) {

if (l && u) {

return new Pair(current.i - 1, current.j - 1);

}

return null;

}


private static Pair getDown(Pair current) {

d = current.i + 1 < n;

if (d) {

return new Pair(current.i + 1, current.j);

}

return null;

}


private static Pair getUp(Pair current) {

u = current.i - 1 >= 0;

if (u) {

return new Pair(current.i - 1, current.j);

}

return null;

}


private static Pair getLeft(Pair current) {

l = current.j - 1 >= 0;

if (l) {

return new Pair(current.i, current.j - 1);

}

return null;

}


private static Pair getRight(Pair current) {

r = current.j + 1 < n;

if (r) {

return new Pair(current.i, current.j + 1);

}

return null;

}


}



반응형

'etc' 카테고리의 다른 글

book  (0) 2014.02.27
[TED]로리 서덜랜드: 인식이 모든 것이다  (0) 2014.02.22
영어 선생 블로그  (0) 2014.02.02
영어 - 1  (0) 2014.02.01
인접행렬 최대크기  (0) 2014.02.01