该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
int getStat(int x, int y) { // 获取坐标的状态
int i;
for (i = 0; i < LIST; i++) {
if (lists[i][0] != EMPTY && lists[i][1] == x && lists[i][2] == y) {
return lists[i][0];
}
}
return 0;
}
int getDIRECTION(int x, int y) { // 获取坐标的方向
int i;
for (i = 0; i < LIST; i++) {
if (lists[i][0] != EMPTY && lists[i][1] == x && lists[i][2] == y) {
return lists[i][6];
}
}
return 0;
}
void changeStat(int stat, int x, int y) { // 修改坐标的状态
int i;
for (i = 0; i < LIST; i++) {
if (lists[i][0] != EMPTY && lists[i][1] == x && lists[i][2] == y) {
lists[i][0] = stat;
return;
}
}
}
void gotoSmallest() { // 选择 F 最小的坐标继续搜索
int i, f = (X + Y + curr_g), s_x = 0, s_y = 0, s_g = 0, a = 0;
changeStat(CLOSED, curr_x, curr_y);
for (i = LIST; i >= 0; i--) {
if (lists[i][0] == OPENED) {
if (f > lists[i][5]) {
f = lists[i][5];
s_x = lists[i][1];
s_y = lists[i][2];
s_g = lists[i][3];
}
a = 1;
}
}
if (a == 0) { // 找不到任何 opened 的坐标了
AS=1;
}
curr_x = s_x;
curr_y = s_y;
curr_g = s_g;
}
void addWay(int wayto) { // 将最终路径添加到结果数组
int i;
for (i = 0; i < LIST; i++) {
if (way[i] == NONE) {
way[i] = wayto;
break;
}
}
}
void find_g(int a) {
int i,j;
for(i=0;i
for(j=0;j<7;j++){
lists[i][j]=0;
}
way[i] = 0;
}
tar_x = Gx;
tar_y = Gy;
if(a==1){
cat_x = Q1->x;
cat_y = Q1->y;
}
if(a==2){
cat_x = Q2->x;
cat_y = Q2->y;
}
curr_x = cat_x;
curr_y = cat_y;
curr_g = 0;
addStat(CLOSED, curr_x, curr_y, 0); // 猫咪起始位置,close
for (;;) {
if (getStat(curr_x - 1, curr_y) == EMPTY && (m[curr_x - 1][curr_y] == GND || m[curr_x - 1][curr_y] == FISH)) { // 向北搜索
addStat(OPENED, curr_x - 1, curr_y, UP);
}
if (getStat(curr_x + 1, curr_y) == EMPTY && (m[curr_x + 1][curr_y] == GND || m[curr_x + 1][curr_y] == FISH)) { // 向南搜索
addStat(OPENED, curr_x + 1, curr_y, DOWN);
}
if (getStat(curr_x, curr_y - 1) == EMPTY && (m[curr_x][curr_y - 1] == GND || m[curr_x][curr_y - 1] == FISH)) { // 向西搜索
addStat(OPENED, curr_x, curr_y - 1, LEFT);
}
if (getStat(curr_x, curr_y + 1) == EMPTY && (m[curr_x][curr_y + 1] == GND || m[curr_x][curr_y + 1] == FISH)) { // 向东搜索
addStat(OPENED, curr_x, curr_y + 1, RIGHT);
}
gotoSmallest(); // 移动
if (getStat(tar_x, tar_y) == OPENED||AS==1) { // 鱼在 opened 区域
break;
}
}
//开始回推
int back_x = tar_x, back_y = tar_y;
int direct = 0;
for (;;) {
direct = getDIRECTION(back_x, back_y); // 获取方向
addWay(direct); // 回推过程写入结果数组
if (direct == UP) back_x++;
else if (direct == DOWN) back_x--;
else if (direct == LEFT) back_y++;
else if (direct == RIGHT) back_y--;
else if (direct == NONE) break;
}
//Draw
int draw_x = cat_x, draw_y = cat_y;
for (i = LIST; i >= 0; i--) { // 结果数组倒序输出
if (way[i] != NONE) {
if (draw_x != cat_x || draw_y != cat_y) {
m[draw_x][draw_y] = way[i] + GO_OFFSET; // 向地图添加箭头信息
}
if (way[i] == UP) {
draw_x--;
} else if (way[i] == DOWN) {
draw_x++;
} else if (way[i] == LEFT) {
draw_y--;
} else if (way[i] == RIGHT) {
draw_y++;
}
}
}
if(a==1){
if((m[Q1->x][Q1->y-1]>=11&&m[Q1->x][Q1->y-1]<=14)||m[Q1->x][Q1->y-1]==A4){fx1=1;}
if((m[Q1->x][Q1->y+1]>=11&&m[Q1->x][Q1->y+1]<=14)||m[Q1->x][Q1->y+1]==A4){fx1=2;}
if((m[Q1->x-1][Q1->y]>=11&&m[Q1->x-1][Q1->y]<=14)||m[Q1->x-1][Q1->y]==A4){fx1=3;}
if((m[Q1->x+1][Q1->y]>=11&&m[Q1->x+1][Q1->y]<=14)||m[Q1->x+1][Q1->y]==A4){fx1=4;}
}
if(a==2){
if((m[Q2->x][Q2->y-1]>=11&&m[Q2->x][Q2->y-1]<=14)||m[Q2->x][Q2->y-1]==A4){fx2=1;}
if((m[Q2->x][Q2->y+1]>=11&&m[Q2->x][Q2->y+1]<=14)||m[Q2->x][Q2->y+1]==A4){fx2=2;}
if((m[Q2->x-1][Q2->y]>=11&&m[Q2->x-1][Q2->y]<=14)||m[Q2->x-1][Q2->y]==A4){fx2=3;}
if((m[Q2->x+1][Q2->y]>=11&&m[Q2->x+1][Q2->y]<=14)||m[Q2->x+1][Q2->y]==A4){fx2=4;}
}
for(i=0;i
for(j=0;j
if(m[i][j]>=11&&m[i][j]<=14){
m[i][j]=0;
}
}
}
}