Chinaunix首页 | 论坛 | 博客
  • 博客访问: 153997
  • 博文数量: 33
  • 博客积分: 2057
  • 博客等级: 大尉
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-19 16:37
文章分类
文章存档

2013年(2)

2012年(23)

2011年(8)

分类: C/C++

2012-07-21 10:28:12


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define M 12
  4. #define N 16
  5. #define MAXSIZE 100

  6. // data initiation

  7. int m[M][N] = {{0,1,0,0,0,1,1,0,0,0,1,1,1,1,1},
  8.              {1,0,0,0,1,1,0,1,1,1,0,0,1,1,1},
  9.              {0,1,1,0,0,0,0,1,1,1,1,0,0,1,1},
  10.              {1,1,0,1,1,1,1,0,1,1,0,1,1,0,0},
  11.              {1,1,0,1,0,0,1,0,1,1,1,1,1,1,1},
  12.              {0,0,1,1,0,1,1,1,0,1,0,0,1,0,1},
  13.              {0,0,1,1,0,1,1,1,0,1,0,0,1,0,1},
  14.              {0,1,1,1,1,0,0,1,1,1,1,1,1,1,1},
  15.              {0,0,1,1,0,1,1,0,1,1,1,1,1,0,1},
  16.              {1,1,0,0,0,1,1,0,1,1,0,0,0,0,0},
  17.              {0,0,1,1,1,1,1,0,0,0,1,1,1,1,0},
  18.              {0,1,0,0,1,1,1,1,1,0,1,1,1,1,0}};
  19. typedef struct {
  20.     int x,y;
  21.     int value;
  22.     int footprint;
  23.     int di;
  24. } Cell;

  25. Cell mig[M][N];

  26. void datainit() {
  27.   int i,j;
  28.   Cell c;
  29.   for (i=0;i<M;i++)
  30.     for (j=0;j<N;j++) {
  31.         c.x = i;
  32.         c.y = j;
  33.         c.value = m[i][j];
  34.         c.di = 0;
  35.         c.footprint = 0;
  36.         mig[i][j] = c;
  37.     }
  38. }

  39. // a stack
  40. typedef Cell eletype;
  41. typedef struct {
  42.     int top;
  43.     eletype *stack[MAXSIZE];
  44. } mystack;

  45. //mystack *ms=(mystack *)malloc(sizeof(int)*100);

  46. void push(mystack *s, eletype *e) {
  47.     if (s->top>=MAXSIZE-1) { printf("mystack overflow!"); }
  48.     else {
  49.      ++s->top;
  50.      s->stack[s->top]=e;
  51.     }
  52. }
  53. void pop(mystack *s) {
  54.     if (s->top<0) { printf("mystack empty"); }
  55.     else {s->top--;}
  56. }


  57. int isempty(mystack *s) {
  58.     if (s->top<0) return 1;
  59.     else return 0;
  60. }

  61. void datainit(void);
  62. void push(mystack *s, eletype *e);
  63. void pop(mystack *s);
  64. int isempty(mystack *s);
  65. /*
  66. int main() {
  67.     datainit();
  68.     int i,j;
  69.     for(i=0;i<M;i++) {for(j=0;j<N;j++) printf("%d",mig[i][j].value); printf("\n");}
  70.     mystack ms;
  71.     (&ms)->top=-1;
  72.     push(&ms,mig[0][0]);
  73.     push(&ms,mig[0][1]);
  74.     printf(" %d ",(&ms)->top);
  75.     int k; for(k=(&ms)->top;k>=0;k--) printf("stack:%d-%d\n",(&ms)->stack[k].x,(&ms)->stack[k].y);
  76.     eletype *curele;
  77.     curele = &mig[0][0]; curele->di++;
  78.     printf(" %d %d\n",curele->di,(&mig[0][0])->di);
  79.     return 0;
  80. }*/




  81. void main() {
  82.     datainit();
  83.     mystack ms;
  84.     (&ms)->top=0;
  85.     mig[0][0].footprint=1;
  86.     (&ms)->stack[0]=&mig[0][0];
  87.     eletype *curele;
  88.     while (!isempty(&ms)) {
  89.         curele = (&ms)->stack[(&ms)->top];
  90.         if (curele->x==M-1 && curele->y==N-1) {
  91.             printf("ok! GOt an solution.\n");
  92.             int k;
  93.             for (k=0;k<=ms.top;k++) printf("%d %d\n",(ms.stack[k])->x, (ms.stack[k])->y);
  94.             break;
  95.         }
  96.         if (curele->di<8) {
  97.             int i,j;
  98.             switch(curele->di) {
  99.              case 0: i=0;j=1;break;
  100.              case 1: i=1;j=1;break;
  101.              case 2: i=1,j=0;break;
  102.              case 3: i=1;j=-1;break;
  103.              case 4: i=0;j=-1;break;
  104.              case 5: i=-1;j=-1;break;
  105.              case 6: i=-1,j=0;break;
  106.              case 7: i=-1;j=1;break;
  107.              default: i=0;j=0;
  108.             }
  109.             int nx=curele->x+i;
  110.             int ny=curele->y+j;
  111.             curele->di++;
  112.             eletype *nextele;
  113.             if (nx>=0 && nx<M && ny>0 && ny<N) {
  114.                 nextele=&mig[nx][ny];
  115.                 if (nextele->value==0 && nextele->footprint==0) {nextele->footprint=1; push(&ms,nextele);}
  116.             }
  117.         }
  118.         else pop(&ms);
  119.     }
  120. }

阅读(454) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~