LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 662|回复: 6

渡河问题,尚有小问题,求教!

[复制链接]
发表于 2003-6-9 18:15:09 | 显示全部楼层 |阅读模式
#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;
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;
}


发表于 2003-6-9 18:19:12 | 显示全部楼层
粘贴代码时要保持缩进,请按置顶贴子的要求做。不然,读起来太费劲。
发表于 2003-6-9 20:11:24 | 显示全部楼层
我靠
兄弟是哪位啊?
我是林!!!
我早上就说过了你这个风格写得太差
这回被kj兄批评了吧
发表于 2003-6-9 20:33:29 | 显示全部楼层
我帮你重新把格式搞了一下
形式美也很重要!


  1. #include <stdio.h>

  2. typedef
  3. struct node
  4. {
  5.     int A, B, C, no, prec;
  6. }
  7. QU;

  8. typedef
  9. struct pair
  10. {
  11.     int np, pp;
  12. }
  13. PA;

  14. QU stack[100];

  15. int top = -1;
  16. int n, c;

  17. void push(QU* pstatus)
  18. {
  19.    stack[++top]= *pstatus;
  20.    return;
  21. }

  22. void pop()
  23. {
  24.    if (top>=0) top--;
  25.    return;
  26. }

  27. /*
  28. 这里的 pair是指船上正常人和病人的数量(x,y),前面指正常人后面是病人
  29. */
  30. int nextpair(PA* ppair)
  31. {
  32.    int i,j;

  33.    i = ppair->np;
  34.    j = ppair->pp;

  35.    if ((i==-1)&&(j==-1))
  36.    {
  37.       i = 0;j = 0;
  38.    }
  39.    else
  40.    {  
  41.       if (j<c-i)
  42.       {
  43.           j++;
  44.       }
  45.       else
  46.       if (i<c)
  47.       {
  48.          i++;
  49.          j = 0;
  50.       }
  51.       else
  52.       return 0;
  53.    }
  54.    
  55.    ppair->np = i;
  56.    ppair->pp = j;
  57.    
  58.    return 1;
  59. }

  60. /*
  61. 符合条件的正常人、病人数量对
  62. */
  63. int nextvalidpair(PA* ppair)
  64. {
  65.     do
  66.     {
  67.       if (nextpair(ppair))
  68.       {
  69.          if ((ppair->np+ppair->pp)<=0)
  70.          continue;
  71.          if ((ppair->np<ppair->pp)&&(ppair->np!=0))
  72.          continue;
  73.          return 1;
  74.       }
  75.       else
  76.       return 0;
  77.     }
  78.     while(1);
  79. }

  80. /*
  81. 检查起始岸的状态
  82. */
  83. int startbank(PA* ppair)
  84. {
  85.     QU status;
  86.     int i, j;
  87.    
  88.     status = stack[top];
  89.     i = status.A-ppair->np;
  90.     j = status.B-ppair->pp;

  91.     if ((i<0)||(i>n)) return 0;
  92.     if ((j<0)||(j>n)) return 0;
  93.     if ((i<j)&&(i>0)) return 0;
  94.     return 1;
  95. }

  96. /*
  97. 检查终点岸的状态
  98. */
  99. int endbank(PA* ppair)
  100. {
  101.     QU status;
  102.     int i, j;
  103.    
  104.     status = stack[top];
  105.     i = n-status.A+ppair->np;
  106.     j = n-status.B+ppair->pp;
  107.    
  108.     if ((i<0)||(i>n)) return 0;
  109.     if ((j<0)||(j>n)) return 0;
  110.     if ((i<j)&&(i>0)) return 0;

  111.     return 1;
  112. }

  113. /*
  114. 检查是否出现重复状态,也就是回路。
  115. */
  116. int duplicatestatus(PA* ppair)
  117. {
  118.     QU status;
  119.     int i, j, k, t;

  120.     status = stack[top];
  121.     i = status.A-ppair->np;
  122.     j = status.B-ppair->pp;
  123.     k = status.C;

  124.     for (t=0;t<top;t++)
  125.     {
  126.        status = stack[t];
  127.       
  128.        if ((k== status.C)&&(i==status.A)&&(j==status.B)) return 1;
  129.        if ((k==!status.C)&&(i==n-status.A)&&(j==n-status.B)) return 1;
  130.     }
  131.    
  132.     return 0;
  133. }


  134. /*起点和终点都正常,又不产生重复状态,是期望的状态*/
  135. int isvalidstatus(PA* ppair)
  136. {
  137.      if (!startbank(ppair))
  138.      return 0;
  139.      if (!endbank(ppair))
  140.      return 0;
  141.      if (duplicatestatus(ppair))
  142.      return 0;

  143.      return 1;
  144. }

  145. /*推算下一状态*/
  146. int nextstep()
  147. {
  148.    QU status;
  149.    PA pair;
  150.    
  151.    pair.np=-1;
  152.    pair.pp=-1;
  153.    status = stack[top];
  154.    
  155.    do {
  156.          if (nextvalidpair(&pair))
  157.          {
  158.              if (isvalidstatus(&pair))
  159.              {
  160.                   status.A=n-status.A+pair.np;
  161.                   status.B=n-status.B+pair.pp;
  162.                   status.C=!(status.C);
  163.                   push(&status);
  164.                   return 1;
  165.              }
  166.          }
  167.          else
  168.          return 0;
  169.       }
  170.       while(1);
  171. }

  172. /*回溯时尝试下一个有可能的状态*/
  173. int nextstatus()
  174. {
  175.       QU status,prevstatus;
  176.       PA pair;
  177.       
  178.       if (top>0)
  179.       {
  180.          status = stack[top];
  181.          prevstatus = stack[top-1];
  182.          pair.np = status.A+prevstatus.A-n;
  183.          pair.pp = status.B+prevstatus.B-n;

  184.          pop();
  185.          status = stack[top];
  186.          
  187.          do {
  188.                if (nextvalidpair(&pair))
  189.                {
  190.                     if (isvalidstatus(&pair))
  191.                     {
  192.                         status.A=n-status.A+pair.np;
  193.                         status.B=n-status.B+pair.pp;
  194.                         status.C=!(status.C);
  195.                         push(&status);
  196.                         return 1;
  197.                     }
  198.                }
  199.                else
  200.                return 0;
  201.             }
  202.             while (1);
  203.       }
  204.       else
  205.       return 0;
  206. }





  207. main()
  208. {
  209.      QU status;
  210.      int outflag,i;

  211.      scanf("%d,%d",&n,&c);
  212.      status.A = n;
  213.      status.B = n;
  214.      status.C = 1;

  215.      /*n=2;c=2;*/

  216.      push(&status);
  217.      do
  218.      {
  219.          if (nextstep())
  220.          {
  221.               status = stack[top];
  222.               if ((status.A==n)&&(status.B==n)&&(status.C==0))
  223.               {
  224.                    for(i=0;i<=top;i++)
  225.                    {
  226.                        status = stack[i];
  227.                        printf("(%d,%d,%d);",status.A,status.B,status.C);
  228.                    }
  229.                    return;
  230.                }
  231.           }
  232.           else
  233.           {
  234.               outflag = 1;
  235.               do
  236.               {  
  237.                     if (nextstatus())
  238.                     {
  239.                          outflag=0;
  240.                     }
  241.                     else
  242.                     {
  243.                          pop();
  244.                          if (top<0)
  245.                          {
  246.                              printf("no result!\n");
  247.                              return;
  248.                          }
  249.                     }
  250.               }
  251.               while(outflag);
  252.           }
  253.      }
  254.      while(1);
  255.      
  256.      return;
  257. }
复制代码
发表于 2003-6-9 20:42:53 | 显示全部楼层
你最好先阅读一下置顶的帖子吧
把你的问题和要求说说清楚
这样莫名其妙的提问
是很摧残人的啊~~~~~~~~~
发表于 2003-6-9 22:40:20 | 显示全部楼层
最初由 scream 发表
我靠
兄弟是哪位啊?
我是林!!!
我早上就说过了你这个风格写得太差
这回被kj兄批评了吧

你们两个认识?
发表于 2003-6-10 21:18:55 | 显示全部楼层
哈哈,是我认识这个程序
我估计他是我们班上同学
因为我上午刚被人要求讲解这个程序
不过不是我写的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表