back_recharge
wangyuan 2019-06-29 11:06:33 +08:00
parent 4dd64f4237
commit 51257cda85
1 changed files with 19 additions and 72 deletions

View File

@ -24,24 +24,11 @@ public class CellUtil {
public static void main(String[] args) { public static void main(String[] args) {
int xx = 9; int xx = 2;
int yy = 19; int yy = 2;
xy2Pos(xx,yy); xy2Pos(xx,yy);
System.out.println(getSurroundPos(10,20,xy2Pos(xx,yy))); System.out.println(getSurroundPos(3,3,xy2Pos(xx,yy)));
// int pos = xy2Pos(xx,yy);
// System.out.println("======> pos : "+pos);
// int[] xy = pos2XY(pos);
// System.out.println("=======> nX : "+xy[0]+" , nY : "+xy[1]);
// System.out.println("=====================================");
// Set<Integer> cells = getOneOpenCell(21,18, pos);
// for(Integer i : cells){
// int[] new_xy = pos2XY(i);
// System.out.println("=======> nX : "+new_xy[0]+" , nY : "+new_xy[1]);
// }
//
// System.out.println(pos2XY(520)[0]);
// System.out.println(pos2XY(520)[1]);
} }
@ -86,71 +73,31 @@ public class CellUtil {
System.out.println("====================================="); System.out.println("=====================================");
return openCells; return openCells;
} }
// int width,int height ,
public static Set<Integer> getSurroundPos(int width,int height ,int pos){ public static Set<Integer> getSurroundPos(int width,int height ,int pos){
Set<Integer> poses = new HashSet<>(); Set<Integer> poses = new HashSet<>();
// int width = 10;
// int height = 10;
int [] xy = pos2XY(pos); int [] xy = pos2XY(pos);
int x = xy[0]; int x = xy[0];
int y = xy[1]; int y = xy[1];
if(x==1){ int x1=x-1;
if(y==1){ int y1=y-1;
poses.add(xy2Pos(x,y+1)); for(;x1<x+2;x1++){
if(x1<1 || x1 > width){
continue;
} }
else if(y==height){ for(;y1<x+2;y1++){
poses.add(xy2Pos(x,y-1)); if(y1<1 || y1 > height){
}else{ continue;
poses.add(xy2Pos(x,y+1)); }
poses.add(xy2Pos(x,y-1)); if(x1 == x && y1 == y){
continue;
}
System.out.println("x ->" + x1 + " y->" + y1);
poses.add(xy2Pos(x1,y1));
} }
poses.add(xy2Pos(x+1,y)); y1=y-1;
return poses;
}else if(x==width){
if(y==1){
poses.add(xy2Pos(x,y+1));
}else if(y==height){
poses.add(xy2Pos(x,y-1));
}else{
poses.add(xy2Pos(x,y+1));
poses.add(xy2Pos(x,y-1));
}
poses.add(xy2Pos(x-1,y));
return poses;
} }
if(y==1){
if(x==1){
poses.add(xy2Pos(x+1,y));
}
else if(x==width){
poses.add(xy2Pos(x-1,y));
}else{
poses.add(xy2Pos(x+1,y));
poses.add(xy2Pos(x-1,y));
}
poses.add(xy2Pos(x,y+1));
return poses;
}else if(y==height){
if(x==1){
poses.add(xy2Pos(x+1,y));
}else if(x==width){
poses.add(xy2Pos(x-1,y));
}else{
poses.add(xy2Pos(x+1,y));
poses.add(xy2Pos(x-1,y));
}
poses.add(xy2Pos(x,y-1));
return poses;
}
poses.add(xy2Pos(x+1,y));
poses.add(xy2Pos(x-1,y));
poses.add(xy2Pos(x,y+1));
poses.add(xy2Pos(x,y-1));
return poses; return poses;
} }