|
发表于 2003-6-9 20:33:29
|
显示全部楼层
我帮你重新把格式搞了一下
形式美也很重要!
- #include <stdio.h>
- typedef
- struct node
- {
- int A, B, C, no, prec;
- }
- QU;
- typedef
- struct pair
- {
- int np, pp;
- }
- PA;
- QU stack[100];
- int top = -1;
- int n, c;
- void push(QU* pstatus)
- {
- stack[++top]= *pstatus;
- return;
- }
- void pop()
- {
- if (top>=0) top--;
- return;
- }
- /*
- 这里的 pair是指船上正常人和病人的数量(x,y),前面指正常人后面是病人
- */
- int nextpair(PA* ppair)
- {
- int i,j;
- i = ppair->np;
- j = ppair->pp;
- if ((i==-1)&&(j==-1))
- {
- i = 0;j = 0;
- }
- else
- {
- if (j<c-i)
- {
- j++;
- }
- else
- if (i<c)
- {
- i++;
- j = 0;
- }
- else
- return 0;
- }
-
- ppair->np = i;
- ppair->pp = j;
-
- return 1;
- }
- /*
- 符合条件的正常人、病人数量对
- */
- int nextvalidpair(PA* ppair)
- {
- do
- {
- if (nextpair(ppair))
- {
- if ((ppair->np+ppair->pp)<=0)
- continue;
- if ((ppair->np<ppair->pp)&&(ppair->np!=0))
- continue;
- return 1;
- }
- else
- return 0;
- }
- while(1);
- }
- /*
- 检查起始岸的状态
- */
- int startbank(PA* ppair)
- {
- QU status;
- int i, j;
-
- status = stack[top];
- i = status.A-ppair->np;
- j = status.B-ppair->pp;
- if ((i<0)||(i>n)) return 0;
- if ((j<0)||(j>n)) return 0;
- if ((i<j)&&(i>0)) return 0;
- return 1;
- }
- /*
- 检查终点岸的状态
- */
- int endbank(PA* ppair)
- {
- QU status;
- int i, j;
-
- status = stack[top];
- i = n-status.A+ppair->np;
- j = n-status.B+ppair->pp;
-
- if ((i<0)||(i>n)) return 0;
- if ((j<0)||(j>n)) return 0;
- if ((i<j)&&(i>0)) return 0;
- return 1;
- }
- /*
- 检查是否出现重复状态,也就是回路。
- */
- int duplicatestatus(PA* ppair)
- {
- QU status;
- int i, j, k, t;
- status = stack[top];
- i = status.A-ppair->np;
- j = status.B-ppair->pp;
- k = status.C;
- for (t=0;t<top;t++)
- {
- status = stack[t];
-
- if ((k== status.C)&&(i==status.A)&&(j==status.B)) return 1;
- if ((k==!status.C)&&(i==n-status.A)&&(j==n-status.B)) return 1;
- }
-
- return 0;
- }
- /*起点和终点都正常,又不产生重复状态,是期望的状态*/
- int isvalidstatus(PA* ppair)
- {
- if (!startbank(ppair))
- return 0;
- if (!endbank(ppair))
- return 0;
- if (duplicatestatus(ppair))
- return 0;
- return 1;
- }
- /*推算下一状态*/
- int nextstep()
- {
- QU status;
- PA pair;
-
- pair.np=-1;
- pair.pp=-1;
- status = stack[top];
-
- do {
- if (nextvalidpair(&pair))
- {
- if (isvalidstatus(&pair))
- {
- status.A=n-status.A+pair.np;
- status.B=n-status.B+pair.pp;
- status.C=!(status.C);
- push(&status);
- return 1;
- }
- }
- else
- return 0;
- }
- while(1);
- }
- /*回溯时尝试下一个有可能的状态*/
- int nextstatus()
- {
- QU status,prevstatus;
- PA pair;
-
- if (top>0)
- {
- status = stack[top];
- prevstatus = stack[top-1];
- pair.np = status.A+prevstatus.A-n;
- pair.pp = status.B+prevstatus.B-n;
- pop();
- status = stack[top];
-
- do {
- if (nextvalidpair(&pair))
- {
- if (isvalidstatus(&pair))
- {
- status.A=n-status.A+pair.np;
- status.B=n-status.B+pair.pp;
- status.C=!(status.C);
- push(&status);
- return 1;
- }
- }
- else
- return 0;
- }
- while (1);
- }
- else
- return 0;
- }
- main()
- {
- QU status;
- int outflag,i;
- scanf("%d,%d",&n,&c);
- status.A = n;
- status.B = n;
- status.C = 1;
- /*n=2;c=2;*/
- push(&status);
- do
- {
- if (nextstep())
- {
- status = stack[top];
- if ((status.A==n)&&(status.B==n)&&(status.C==0))
- {
- for(i=0;i<=top;i++)
- {
- status = stack[i];
- printf("(%d,%d,%d);",status.A,status.B,status.C);
- }
- return;
- }
- }
- else
- {
- outflag = 1;
- do
- {
- if (nextstatus())
- {
- outflag=0;
- }
- else
- {
- pop();
- if (top<0)
- {
- printf("no result!\n");
- return;
- }
- }
- }
- while(outflag);
- }
- }
- while(1);
-
- return;
- }
复制代码 |
|