標題: | 迷宮問題 |
時 限: | 100000 ms |
內存限制: | 100000 K |
總時限: | 3000 ms |
描述: |
迷宮問題
迷宮是一個二維矩陣,其中1爲牆,0爲路,3爲入口,4爲出口.要求從入口開始,從出口結束,按照 下,左,上,右 的順序來搜索路徑.
|
輸入: |
迷宮寬度w 迷宮高度h
迷宮第一行
迷宮第二行
...
迷宮第h 行
|
輸出: |
入口橫座標1 入口縱座標1
橫座標2 縱座標2
橫座標3 縱座標3
橫座標4 縱座標4
...
橫座標n-1 縱座標n-1
出口橫座標n 出口縱座標n
|
輸入樣例: |
8 10
1 1 1 1 1 1 1 1
1 0 1 1 0 1 0 1
1 0 1 0 0 1 0 1
1 1 0 3 1 0 1 1
1 0 0 1 0 0 4 1
1 0 0 0 0 1 1 1
1 0 1 0 0 1 0 1
1 0 1 0 0 0 1 1
1 1 1 1 0 0 0 1
1 1 1 1 1 1 1 1 |
輸出樣例: |
3 3
2 3
2 4
2 5
3 5
3 6
3 7
4 7
4 6
4 5
4 4
5 4
6 4
|
//編譯器G++
#include <stdio.h>
#include <stdlib.h>
#define InitStackSize 1000
#define SizeIncrease 100
typedef struct
{
int x;
int y;
}PosType;
typedef struct
{
int ord;
PosType seat;
int di;
}SElemType;
typedef struct sqStack
{
SElemType *top;
SElemType *base;
int stacksize;
}sqStack;
//initial stack
int InitStack(sqStack *s)
{
s->base=(SElemType*)malloc(InitStackSize*sizeof(SElemType));
s->top=s->base;
s->stacksize=InitStackSize;
return 0;
}
//push
int Push(sqStack *s,SElemType e)
{
if(s->top-s->base>=s->stacksize)//stack is full,realloc
{
//printf("realloc\n");
s->base=(SElemType*)realloc(s->base,(s->stacksize+SizeIncrease)*sizeof(SElemType));
s->top=s->base+s->stacksize;
s->stacksize=s->stacksize+SizeIncrease;
}
*(s->top)=e;
s->top++;
return 0;
}
//pop
SElemType pop(sqStack *s)
{
SElemType temp;
s->top--;
temp=*(s->top);
return temp;
}
//gettop
SElemType gettop(sqStack *s)
{
return *(s->top-1);
}
//isempty
int isempty(sqStack *s)
{
if(s->top==s->base)
return 1;
else
return 0;
}
//Traverse
//int StackTraverse(sqStack *s)
//{
//SElemType *p=s->top;
////printf("該棧自頂向下的元素爲:\n");
//while(p!=s->base)
//{
//p--;
////cout<<*p<<endl;
//printf("(%d %d)",p->seat.y,p->seat.x);
//}
//printf("\n");
//return 0;
//}
void printffrombase(sqStack *s)
{
SElemType *p=s->base;
while(p!=s->top)
{
printf("%d %d\n",p->seat.x,p->seat.y);
p++;
}
}
int Pass(int **Maze,PosType pos)
{
if(Maze[pos.y][pos.x]!=1)
return 1;
return 0;
}
void Footprint(int **Maze,PosType pos)
{
//pos->walked=1;
Maze[pos.y][pos.x]=1;
}
void Markprint(int **Maze,PosType pos)
{
Maze[pos.y][pos.x]=1;
}
PosType NextPos(PosType curpos,int di)
{
if(di==1)
{
curpos.y=curpos.y+1;
}
if(di==2)
{
curpos.x=curpos.x-1;
}
if(di==3)
{
curpos.y=curpos.y-1;
}
if(di==4)
{
curpos.x=curpos.x+1;
}
return curpos;
}
int main()
{
int width,height;
scanf("%d%d",&width,&height);
//動態分配迷宮的寬和高
int **Maze;
Maze=(int **)malloc(sizeof(int*)*height);
Maze[0]=(int *)malloc(sizeof(int)*height*width);
int i;
for(i=1;i<height;i++)
{
Maze[i]=Maze[i-1]+width;
}
//獲取迷宮各位置的值
int temp;
for (i=0;i<height;i++)
{
int j;
for(j=0;j<width;j++)
{
scanf("%d",&temp);
//setPos(&pos[i][j],j,i,temp);
Maze[i][j]=temp;
}
}
sqStack *stack=(sqStack*)malloc(sizeof(sqStack));
InitStack(stack);
PosType curpos,start,end;
int j;
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
if(Maze[i][j]==3)
{
start.x=j;
start.y=i;
}
}
}
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
if(Maze[i][j]==4)
{
end.x=j;
end.y=i;
}
}
}
int curstep=1;
curpos=start;
do
{
if(Pass(Maze,curpos))
{
Footprint(Maze,curpos);
SElemType e;
e.ord=curstep;
e.seat=curpos;
e.di=1;
Push(stack,e);
if(curpos.x==end.x&&curpos.y==end.y)
{
//printf("12\n");
printffrombase(stack);
return 0;
}
curpos=NextPos(curpos,1);
curstep++;
}//if pass
else
{
//printf("nopass\n");
if(!isempty(stack))
{
SElemType e=pop(stack);
while(e.di==4 && !isempty(stack))
{
Markprint(Maze,e.seat);
e=pop(stack);
}//while
if(e.di<4)
{
e.di++;
Push(stack,e);
curpos=NextPos(e.seat,e.di);
}//if
}//if
}//else
}while(!isempty(stack));
return 1;
}