NND 写了程序没人理啊 又加入了时间 可以计算多长时间做完的 大家玩下吧
#include <stdio.h>
#include <time.h>
#define DEBUG 0
#define TRUE 1
#define FALSE 0
typedef struct _Frog
{
int spaceflag;//1 = have space, -1=no space
int direct;//1 = right, 0 = no direct, -1 = left
char* id;//ÇàÍܵÄID
int address;//Êä³öµÄµØÖ·£¬´ú±íµÄÊǵڼ¸¸ö
}Frog;
Frog frog[7];
static void Printf(Frog* frog);
static int Act(Frog* frog);
static int Review(Frog* frog);
int main()
{
int a,i,x;
double b;
unsigned long start, finish;
x=0;
//Ïȳõʼ»¯£¬²»ÖªµÀÄÄλÐÖµÜÄܼò»¯Ò»ÏÂ
frog[0].spaceflag=-1;
frog[0].direct=1;
frog[0].id="A1";
frog[0].address=0;
frog[1].spaceflag=1;
frog[1].direct=1;
frog[1].id="A2";
frog[1].address=1;
frog[2].spaceflag=1;
frog[2].direct=1;
frog[2].id="A3";
frog[2].address=2;
frog[3].spaceflag=-1;
frog[3].direct=0;
frog[3].id="space";
frog[3].address=3;
frog[4].spaceflag=1;
frog[4].direct=-1;
frog[4].id="B4";
frog[4].address=4;
frog[5].spaceflag=1;
frog[5].direct=-1;
frog[5].id="B5";
frog[5].address=5;
frog[6].spaceflag=-1;
frog[6].direct=-1;
frog[6].id="B6";
frog[6].address=6;
start = clock()/CLOCKS_PER_SEC;
//×öÑ»·.....
while(1)
{
Printf(frog);
a=Act(frog);
if(DEBUG)
{
printf("Elapsed time:%u secs.\n",clock()/CLOCKS_PER_SEC);
}
//ÿ´ÎÒª»Ö¸´Îª0£¬²»È»×ÜÊÇ»áÍùÉϼӣ¬×îºó¼´Ê¹È«²¿Ìø¹ýÒ²µÃ²»µ½½á¹û
x=0;
for(i=0;i<7;i++)
{
//È«²¿Ìø¹ýºóÅжÏÊÇ·ñ¿Õ¼äָʾ¶¼Îª-1£¬È«-1µÄ»°´ú±í²»ÄÜÌøÁË
if((frog+i)->spaceflag == -1)
{
x=x+1;
}
}
if((x==7)&&(((frog+4)->address+(frog+5)->address+(frog+6)->address)==3))//È«-1»¹ÓÐBµÄÇàÍܶ¼Òªµ½´ïÖ¸¶¨µØµã
break;
else
if((x==7)&&(((frog+4)->address+(frog+5)->address+(frog+6)->address)!=3))
{
printf("you lose, hiahia\n");
exit (1);
}
};
finish = clock()/CLOCKS_PER_SEC;
printf("congratulations, you used %u seconds\n", (finish - start));
}
int Act(Frog* frog)
{
int y,i;
printf("please input frog id:");
scanf("%d", &y);
//y<4µÄʱºòÒª¼õ1£¬ÒòΪÊÇ0¿ªÍ·µÄ¶ø°´¼üÊÇ1£¬4-6¾ÍÓÃÔ¼ü룬ÒòΪÖмäÓиöspace
if(y<4)
y=y-1;
//¿ÕµØ²»×¼ÊäÈë ´óÓÚ6Ò²²»×¼
if(((frog+3)->address==y)||(y>6))
{
printf("Id error,don't move\n");
}
else
{
if((frog+y)->spaceflag == 1)//ÊäÈëºóÅжÏÊÇ·ñÄÜÌø
{
if(((frog+y)->direct == 1)&&((frog+y)->address<(frog+3)->address))//¿´ÊÇ·ñA±ßµÄÇàÍÜ»¹ÓÐÊÇ·ñÔÚspace×ó±ß£¨Æä //ʵ¿ÉÒÔ²»ÅÐ¶ÏµÄ ²»¹ýµ±Ê±Ð´ÁË ÏÖÔÚÀÁµÃ¸Ä£©
{
if(((frog+3)->address)-((frog+y)->address) == 1)//¿´ÊDz»ÊÇÖ±½ÓÌø
{
(frog+y)->address = (frog+y)->address+1;//ÓëspaceµØÖ·»¥»»
(frog+3)->address = (frog+3)->address-1;
for(i=0;i<7;i++)//ÇåËãËùÓеÄspaceflag
{
if(i<3)//ËãAÕâ±ßµÄ
{
//ÀëspaceÁ½¸ö¸ñÄÚ²¢ÇÒÔÚspace×ó±ß
if((((frog+3)->address-(frog+i)->address)<=2)&&((frog+3)->address>(frog+i)->address))
(frog+i)->spaceflag = 1;
else
(frog+i)->spaceflag = -1;
}
else//BÕâ±ßµÄ
{
if((((frog+i)->address-(frog+3)->address)<=2)&&((frog+i)->address>(frog+3)->address))
(frog+i)->spaceflag = 1;
else
(frog+i)->spaceflag = -1;
}
}
}
else//ÕâÊǸô×ÅÒ»¸öÌøµÄ
{
(frog+y)->address = (frog+y)->address+2;
(frog+3)->address = (frog+3)->address-2;
for(i=0;i<7;i++)
{
if(i<3)
{
if((((frog+3)->address-(frog+i)->address)<=2)&&((frog+3)->address>(frog+i)->address))
(frog+i)->spaceflag = 1;
else
(frog+i)->spaceflag = -1;
}
else
{
if((((frog+i)->address-(frog+3)->address)<=2)&&((frog+i)->address>(frog+3)->address))
(frog+i)->spaceflag = 1;
else
(frog+i)->spaceflag = -1;
}
}
}
}
else//ÓұߵÄÇàÍÜÍù×óÌøÁË
if(((frog+y)->direct == -1)&&((frog+y)->address>(frog+3)->address))
{
if(((frog+y)->address)-((frog+3)->address) == 1)
{
(frog+y)->address = (frog+y)->address-1;
(frog+3)->address = (frog+3)->address+1;
for(i=0;i<7;i++)
{
if(i<3)
{
if((((frog+3)->address-(frog+i)->address)<=2)&&((frog+3)->address>(frog+i)->address))
(frog+i)->spaceflag = 1;
else
(frog+i)->spaceflag = -1;
}
else
{
if((((frog+i)->address-(frog+3)->address)<=2)&&((frog+i)->address>(frog+3)->address))
(frog+i)->spaceflag = 1;
else
(frog+i)->spaceflag = -1;
}
}
}
else
{
(frog+y)->address = (frog+y)->address-2;
(frog+3)->address = (frog+3)->address+2;
for(i=0;i<7;i++)
{
if(i<3)
{
if((((frog+3)->address-(frog+i)->address)<=2)&&((frog+3)->address>(frog+i)->address))
(frog+i)->spaceflag = 1;
else
(frog+i)->spaceflag = -1;
}
else
{
if((((frog+i)->address-(frog+3)->address)<=2)&&((frog+i)->address>(frog+3)->address))
(frog+i)->spaceflag = 1;
else
(frog+i)->spaceflag = -1;
}
}
}
}
else
{
printf("Id error,don't move\n");
return(1);
}
}
else
{
printf("Id error,don't move\n");
return(1);
}
}
return(0);
}
//´òÓ¡³öλÖÃ
void Printf(Frog* frog)
{
int x,y;//x = address, y = id
for(x=0;x<7;x++)//Ê×ÏÈÊÇλÖÃ0
{
for(y=0;y<7;y++)//Ëã³öÄĸöÇàÍÜÊÇλÖÃ0
{
if((frog+y)->address==x)
{
printf("%s\t", (frog+y)->id);//´òÓ¡³ö´ËÇàÍܵÄID
}
}
}
}