HumansWay.h
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:6k
- ////////////////////////////////////
- // this one is only for perfect maze
- // and it is very important that we should ensure that every available way's authority should be a
- //positive
- // Copyright: 胡小民,丁展 2002.5
- #include <iostream.h>
- #include <stdlib.h>
- #include "maze.h"
- const int fromLeft=0;
- const int fromUp=1;
- const int fromRight=2;
- const int fromDown=3;
- #define END 0
- int refreshSig(int sig[CONST_MAZEX][CONST_MAZEY]);
- int refreshSig(int sig[CONST_MAZEX][CONST_MAZEY],int x,int y,int dir)
- {
- //确保权值在表示墙时不会相加
- if(y>=CONST_MAZEY||x>=CONST_MAZEX||x<0||y<0)
- return 0; // dingzhan added
-
- if(sig[y][x]<UNACCESSABLE)
- sig[y][x]=UNACCESSABLE;
- int temp1,temp2,temp3;
- // 不使用sig[y][x]==UNACCESSABLE是为了确保有些情况那儿的确是墙了,却有sig[y][x]约等于UNACCESSABLE
- if(sig[y][x]==END || sig[y][x]<UNACCESSABLE/2)
- return 0;
- if(x==1)
- {
- if(y==1)
- {
- if(dir==fromRight)
- {
- temp1=refreshSig(sig,x,y+1,fromUp);
- if(temp1!=END && temp1>UNACCESSABLE/2)
- sig[y][x]=temp1;
- else return sig[y][x];
- return temp1;
- }
- else if(dir==fromDown)
- {
- temp1=refreshSig(sig,x+1,y,fromLeft);
- if(temp1!=END && temp1>UNACCESSABLE/2)
- sig[y][x]=temp1;
- else return sig[y][x];
- return temp1;
- }
- else if(dir==fromLeft) // from the entrance
- {
- temp1=refreshSig(sig,x+1,y,fromLeft);
- temp2=refreshSig(sig,x,y+1,fromUp);
- if((temp1+temp2)>UNACCESSABLE/2 && (temp1+temp2)!=END)
- sig[y][x]=temp1+temp2;
- else return sig[y][x];
- return temp1+temp2;
- }
- }
- else if(y==SIZEX_MAZE)
- {
- if(dir==fromRight)
- {
- temp1=refreshSig(sig,x,y-1,fromDown);
- if(temp1!=END && temp1>UNACCESSABLE/2)
- sig[y][x]=temp1;
- else return sig[y][x];
- return temp1;
- }
- else if(dir==fromUp)
- {
- temp1=refreshSig(sig,x+1,y,fromLeft);
- if(temp1!=END && temp1>UNACCESSABLE/2)
- sig[y][x]=temp1;
- else return sig[y][x];
- return temp1;
- }
- }
- else
- {
- if(dir==fromRight)
- {
- temp1=refreshSig(sig,x,y-1,fromDown);
- temp2=refreshSig(sig,x,y+1,fromUp);
- if((temp1+temp2)>UNACCESSABLE/2 && (temp1+temp2)!=END)
- sig[y][x]=temp1+temp2;
- else return sig[y][x];
- return temp1+temp2;
- }
- else if(dir==fromDown)
- {
- temp1=refreshSig(sig,x+1,y,fromLeft);
- temp2=refreshSig(sig,x,y-1,fromDown);
- if((temp1+temp2)>UNACCESSABLE/2 && (temp1+temp2)!=END)
- sig[y][x]=temp1+temp2;
- else return sig[y][x];
- return temp1+temp2;
- }
- else if(dir==fromUp)
- {
- temp1=refreshSig(sig,x+1,y,fromLeft);
- temp2=refreshSig(sig,x,y+1,fromUp);
- if((temp1+temp2)>UNACCESSABLE/2 && (temp1+temp2)!=END)
- sig[y][x]=temp1+temp2;
- else return sig[y][x];
- return temp1+temp2;
- }
- }
- }
- else if(x==SIZEY_MAZE)
- {
- if(y==1)
- {
- if(dir==fromLeft)
- {
- temp1=refreshSig(sig,x,y+1,fromUp);
- if(temp1!=END && temp1>UNACCESSABLE/2)
- sig[y][x]=temp1;
- else return sig[y][x];
- return temp1;
- }
- else if(dir==fromDown)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- if(temp1!=END && temp1>UNACCESSABLE/2)
- sig[y][x]=temp1;
- else return sig[y][x];
- return temp1;
- }
- }
- else if(y==SIZEX_MAZE)
- {
- if(dir==fromLeft)
- {
- temp1=refreshSig(sig,x,y-1,fromDown);
- if(temp1!=END && temp1>UNACCESSABLE/2)
- sig[y][x]=temp1;
- else return sig[y][x];
- return temp1;
- }
- else if(dir==fromUp)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- if(temp1!=END && temp1>UNACCESSABLE/2)
- sig[y][x]=temp1;
- else return sig[y][x];
- return temp1;
- }
- }
- else
- {
- if(dir==fromLeft)
- {
- temp1=refreshSig(sig,x,y-1,fromDown);
- temp2=refreshSig(sig,x,y+1,fromUp);
- }
- else if(dir==fromDown)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- temp2=refreshSig(sig,x,y-1,fromDown);
- }
- else if(dir==fromUp)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- temp2=refreshSig(sig,x,y+1,fromUp);
- }
- else
- {
- cout<<"error in refreshSig"<<endl;
- exit(0);
- }
- if((temp1+temp2)>UNACCESSABLE/2 && (temp1+temp2)!=END)
- sig[y][x]=temp1+temp2;
- else return sig[y][x];
- return temp1+temp2;
- }
- }
- if(y==1)
- {
- if(dir==fromDown)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- temp2=refreshSig(sig,x+1,y,fromLeft);
- }
- else if(dir==fromLeft)
- {
- temp1=refreshSig(sig,x+1,y,fromLeft);
- temp2=refreshSig(sig,x,y+1,fromUp);
- }
- else if(dir==fromRight)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- temp2=refreshSig(sig,x,y+1,fromUp);
- }
- else
- {
- cout<<"error in refreshSig"<<endl;
- exit(0);
- }
- if((temp1+temp2)>UNACCESSABLE/2 && (temp1+temp2)!=END)
- sig[y][x]=temp1+temp2;
- else
- return sig[y][x];
- return temp1+temp2;
- }
- else if(y==SIZEX_MAZE)
- {
- if(dir==fromUp)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- temp2=refreshSig(sig,x+1,y,fromLeft);
- }
- else if(dir==fromLeft)
- {
- temp1=refreshSig(sig,x+1,y,fromLeft);
- temp2=refreshSig(sig,x,y-1,fromDown);
- }
- else if(dir==fromRight)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- temp2=refreshSig(sig,x,y-1,fromDown);
- }
- else
- {
- cout<<"error in refreshSig"<<endl;
- exit(0);
- }
- if((temp1+temp2)>UNACCESSABLE/2 && (temp1+temp2)!=END)
- sig[y][x]=temp1+temp2;
- else return sig[y][x];
- return temp1+temp2;
- }
- if(dir==fromUp)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- temp2=refreshSig(sig,x+1,y,fromLeft);
- temp3=refreshSig(sig,x,y+1,fromUp);
- }
- else if(dir==fromDown)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- temp2=refreshSig(sig,x+1,y,fromLeft);
- temp3=refreshSig(sig,x,y-1,fromDown);
- }
- else if(dir==fromRight)
- {
- temp1=refreshSig(sig,x-1,y,fromRight);
- temp2=refreshSig(sig,x,y-1,fromDown);
- temp3=refreshSig(sig,x,y+1,fromUp);
- }
- else
- {
- temp1=refreshSig(sig,x+1,y,fromLeft);
- temp2=refreshSig(sig,x,y-1,fromDown);
- temp3=refreshSig(sig,x,y+1,fromUp);
- }
- if((temp1+temp2+temp3)>UNACCESSABLE/2 && (temp1+temp2+temp3)!=END)
- sig[y][x]=temp1+temp2+temp3;
- else return sig[y][x];
- return temp1+temp2+temp3;
- }