| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4101 人关注过本帖
标题:青蛙过河(互换)
只看楼主 加入收藏
hao0716
Rank: 4
等 级:业余侠客
威 望:1
帖 子:353
专家分:222
注 册:2006-4-11
收藏
 问题点数:0 回复次数:12 
青蛙过河(互换)

昨天看到别人说写一个青蛙过河的程序 想起了自己以前玩的一个flash 就做了一个 不过是不一样的
规则:A1 A2 A3 B1 B2 B3 是六只青蛙 想要跳到对方的地方去 中间space是河
互换就是说B全部到A A全部到B 顺序可不一样
可以单个跳 也可以隔一个跳 不能隔一个以上跳
青蛙身上的编号对应数字键1-6 操作可跳
试下吧 程序应该没BUG了 不过代码不指望谁能看懂 我自己写完后改都晕了........

http://media.ebaumsworld.com/frogleap.swf
按照这个做的

[此贴子已经被作者于2006-8-22 15:21:54编辑过]

搜索更多相关主题的帖子: 青蛙 BUG 过河 target 
2006-08-22 09:22
hao0716
Rank: 4
等 级:业余侠客
威 望:1
帖 子:353
专家分:222
注 册:2006-4-11
收藏
得分:0 

[CODE][/CODE]
#include <stdio.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;
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;
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;

while(1)
{
Printf(frog);
a=Act(frog);

if(DEBUG)
{

for(a =0;a<7;a++)
{
printf("%d\n",(frog+a)->spaceflag);
}
}
x=0;
for(i=0;i<7;i++)
{
if((frog+i)->spaceflag == -1)
{
x=x+1;
}
}
if(DEBUG)
{
printf("%d\n",x);
}
if((x==7)&&(((frog+4)->address+(frog+5)->address+(frog+5)->address)==3))
break;
}
;


printf("congratulations, pass\n");
}

int Act(Frog* frog)
{
int y,i;
printf("please input frog id:");
scanf("%d", &y);

if(y<4)
y=y-1;

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))
{
if(((frog+3)->address)-((frog+y)->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
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++)
{
for(y=0;y<7;y++)
{
if((frog+y)->address==x)
{
printf("%s\t", (frog+y)->id);
}
}
}
}



2006-08-22 09:23
ridwsc
Rank: 1
等 级:新手上路
帖 子:168
专家分:0
注 册:2006-8-10
收藏
得分:0 
楼主,我是肯定看不懂了,这么长的代码,
又没有注释。

不过还是要谢谢

中国加油,加油,加油~~~~~ 我行~~我可以~~C
2006-08-22 09:26
hao0716
Rank: 4
等 级:业余侠客
威 望:1
帖 子:353
专家分:222
注 册:2006-4-11
收藏
得分:0 
贴上EXE文件吧
免得大家麻烦了

83cXipOh.rar (4.61 KB)

[此贴子已经被作者于2006-8-22 9:48:30编辑过]



fD3DRcz3.rar (4.6 KB)

2006-08-22 09:26
ridwsc
Rank: 1
等 级:新手上路
帖 子:168
专家分:0
注 册:2006-8-10
收藏
得分:0 

中国加油,加油,加油~~~~~ 我行~~我可以~~C
2006-08-22 09:26
hao0716
Rank: 4
等 级:业余侠客
威 望:1
帖 子:353
专家分:222
注 册:2006-4-11
收藏
得分:0 

[CODE][/CODE]
#include <stdio.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;
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;

//做循环.....
while(1)
{
Printf(frog);
a=Act(frog);

if(DEBUG)
{

for(a =0;a<7;a++)
{
printf("%d\n",(frog+a)->spaceflag);
}
}
//每次要恢复为0,不然总是会往上加,最后即使全部跳过也得不到结果
x=0;
for(i=0;i<7;i++)
{
//全部跳过后判断是否空间指示都为-1,全-1的话代表不能跳了
if((frog+i)->spaceflag == -1)
{
x=x+1;
}
}
if(DEBUG)
{
printf("%d\n",x);
}
if((x==7)&&(((frog+4)->address+(frog+5)->address+(frog+6)->address)==3))//全-1还有B的青蛙都要到达指定地点
break;
}
;


printf("congratulations, pass\n");
}

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)//看是不是直接跳
{
(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
}
}
}
}

加了注释,希望大家能看懂并提出意见,谢谢


2006-08-22 09:44
穆扬
Rank: 1
等 级:禁止发言
帖 子:1910
专家分:0
注 册:2006-6-1
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

2006-08-22 09:47
ridwsc
Rank: 1
等 级:新手上路
帖 子:168
专家分:0
注 册:2006-8-10
收藏
得分:0 
谢谢楼主,我会慢慢看的。


中国加油,加油,加油~~~~~ 我行~~我可以~~C
2006-08-22 09:48
hao0716
Rank: 4
等 级:业余侠客
威 望:1
帖 子:353
专家分:222
注 册:2006-4-11
收藏
得分:0 
eeeeeee
A1 A2 A3 SPACE B4 B5 B6
两队青蛙 中间space是河 然后要让他们互换位置
B4 B5 B6 SPACE A1 A2 A3
变成这样

按对应的数字可跳
明白没

2006-08-22 09:53
hao0716
Rank: 4
等 级:业余侠客
威 望:1
帖 子:353
专家分:222
注 册:2006-4-11
收藏
得分:0 

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
}
}
}
}



2006-08-22 11:34
快速回复:青蛙过河(互换)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038769 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved