Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

哪位大哥帮下忙看看?多谢!!!!!!!!!

Posted by onon at 2006-08-05 01:41:43 on Problem 1979
#include<iostream.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>

const int MaxSize=1000;

typedef struct{
   int x;
   int y;
}PosType;

typedef struct{
   int di;
   PosType pos;
}SElemType;

typedef struct{
    SElemType *base, *top;
	int CurSize;
	int StackSize;
}Stack, SType;

void InitStack(SType*s)
{
   s->base=(SElemType*)malloc(sizeof(SElemType)*MaxSize);
   if(s->base==NULL) cout<<"memery allocation failure"<<endl;
   s->StackSize=MaxSize;
   s->CurSize=0;
   s->top=s->base;
 
}

void Push(SElemType e, SType *s)
{
    if(s->CurSize==MaxSize)
	{
	   cout<<"the stack is full"<<endl;
	   return;
	}
	else{
	   *s->top++=e;
	   s->CurSize++;
	   
	}
}

void Pop(SElemType&e, SType *s)
{
   if(s->base==s->top)
   {
     cout<<"the stack is empty"<<endl;
	 return;
   }
   else e=*--s->top;
}

int StackEmpty(SType s)
{
	 if(s.base==s.top) return 1;
	 else return 0;
}

PosType  NextPos(PosType pt, int d)
{
	switch(d){
	  case 1: pt.x+=1; break;
      case 2: pt.y+=1; break;
	  case 3: pt.x-=1; break;
	  case 4: pt.y-=1; break;
	  default: cout<<"wrong direction"<<endl;
    }
	return pt;
}

void print(char m[][25], int r, int c)
{
	 for(int i=0; i<r; i++){
		 for(int j=0; j<c; j++)
			 cout<<m[i][j]<<' ';
	     cout<<endl;
	}
}


    
int main()
{
   int step, i, j, w, h;
   char maze[25][25], c;
   PosType start, CurPos;
   SType s;
   SElemType e;

  
   

   while(1)
   {
     cin>>w>>h;
	 if(w==0&&h==0) break;
	 InitStack(&s);
     
	 for(i=1; i<=h; i++)
		 for(j=1; j<=w; j++){
			 cin>>maze[i][j];
			 if(maze[i][j]=='@'){
				 start.x=i; start.y=j;
			 }
		 }
	 for(i=0; i<=w+1; i++){ maze[0][i]='#';maze[h+1][i]='#';}
	 for(i=0; i<=h+1; i++){maze[i][0]='#';maze[i][w+1]='#';}
	 step=0;
     CurPos=start;
	 
	 do
	 {
		 if(maze[CurPos.x][CurPos.y]=='.' || maze[CurPos.x][CurPos.y]=='@')
		 {
			 
             e.di=1; e.pos=CurPos; Push(e, &s); 
			 step++;
			 maze[CurPos.x][CurPos.y]='w';
		     CurPos=NextPos(CurPos, 1);
		     
		 }
		 else{
			 
			 if(!StackEmpty(s)){
			    Pop(e, &s);
			    if(e.di==4) maze[e.pos.x][e.pos.y]='w';
			    while(!StackEmpty(s)&&e.di==4){
			       Pop(e,&s);
				   maze[e.pos.x][e.pos.y]='w';
				}//while
			    if(e.di<4){
			       e.di++; Push(e, &s);
				   CurPos=NextPos(e.pos, e.di);
				}//if
			 }//if
		 }//else
	 }while(!StackEmpty(s));
	 cout<<step<<endl;
	 //print(maze, h+2, w+2);
	 free(s.base);
   }//
   return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator