import java.util.ArrayList;
public class Algorithm {
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;
public static void main(String[] args) {
matrix = new int[][] {
{ 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 1 },
{ 0, 1, 0, 1, 1 },
{ 0, 1, 0, 0, 1 },
{ 1, 1, 0, 0, 0 } };
visited = new int[5][5];
count= new int[5];
countIndex = -1;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; 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 < 5;
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 < 5;
if (r) {
return new Pair(current.i, current.j + 1);
}
return null;
}
}
'etc' 카테고리의 다른 글
영어 선생 블로그 (0) | 2014.02.02 |
---|---|
영어 - 1 (0) | 2014.02.01 |
민감한 체질에 맞는 건강관리 (0) | 2014.01.30 |
blog (0) | 2014.01.12 |
20140112005200 (0) | 2014.01.12 |