Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233511
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: C/C++

2017-02-20 17:52:15

代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h>

  2. typedef struct point{
  3.     int x;
  4.     int y;
  5.     int Step;
  6. }Point;

  7. Point Stack[100];
  8. int head;
  9. int tail;


  10. void Init_Stack(void)
  11. {
  12.     int i;
  13.     for(i=0;i<100;i++){
  14.         Stack[i].x = 0;
  15.         Stack[i].y = 0;
  16.     }
  17.     head = 0;
  18.     tail = 0;
  19. }

  20. int Stack_Is_Empty()
  21. {
  22.     if(head == tail)
  23.         return 1;

  24.     return 0;
  25. }

  26. void Add_Stack_Head(Point p)
  27. {
  28.     Stack[tail] = p;
  29.     
  30.     tail++;
  31.     if(tail>99){
  32.         tail = 0;
  33.     }
  34. }

  35. Point Del_Stack_Head(void)
  36. {
  37.     Point p;
  38.     p = Stack[head];
  39.     Stack[head].x = 0;
  40.     Stack[head].y = 0;
  41.     head++;
  42.     if(head>99){
  43.         head = 0;
  44.     }
  45.     return p;
  46. }

  47. int buff[11][11] = {
  48.     {1,1,1,1,1,1,1,1,1,1,1},
  49.     {1,0,1,1,0,1,0,1,1,0,0},
  50.     {1,0,1,1,0,1,0,1,0,0,0},
  51.     {1,0,0,0,0,0,0,0,0,1,0},
  52.     {1,0,1,1,0,1,0,1,1,0,0},
  53.     {1,0,1,1,0,1,0,0,0,0,1},
  54.     {1,0,1,0,0,1,0,1,1,0,1},
  55.     {1,0,0,0,0,0,0,0,1,0,1},
  56.     {1,0,1,1,0,0,0,0,0,0,0},
  57.     {1,0,1,1,0,1,0,1,0,0,0},
  58.     {1,0,1,1,0,1,0,0,0,0,0}
  59. };

  60. int visit[11][11];


  61. void Init_Visit(void)
  62. {
  63.     int i,j;
  64.     for(i=0;i<11;i++){
  65.         for(j=0;j<1;j++){
  66.             visit[i][j] = 0;
  67.         }
  68.     }
  69. }

  70. int Is_Visit(int x, int y){

  71.     if(visit[x][y] == 1)
  72.         return 1;

  73.     return 0;
  74. }

  75. void Set_Visit(Point p)
  76. {
  77.     visit[p.x][p.y] = 1;
  78. }


  79. int direction[4][2] = {
  80.     {-1,0},{1,0},{0,-1},{0,1} //up down left right
  81. };


  82. int DFS(Point p)
  83. {
  84.     int i;
  85.     Point temp,Add_temp;
  86.     Set_Visit(p);
  87.     Add_Stack_Head(p);

  88.     while(!Stack_Is_Empty()){

  89.         temp = Del_Stack_Head();

  90.         for(i=0;i<4;i++){

  91.             if((temp.x+direction[i][0] > 10) || (temp.x+ direction[i][0] <1) || (temp.y + direction[i][1] > 10) || (temp.y + direction[i][1] < 1)){
  92.                 continue;
  93.             }

  94.             if((temp.x+direction[i][0] == 10) && (temp.y+direction[i][1] == 10)){
  95.                 printf("I find it %d",temp.Step);
  96.                 return 1;
  97.             }

  98.             if((!Is_Visit(temp.x+direction[i][0],temp.y +direction[i][1])) && buff[temp.x +direction[i][0]][temp.y + direction[i][1]] == 0){
  99.                 Add_temp.x = temp.x+direction[i][0];
  100.                 Add_temp.y = temp.y+direction[i][1];
  101.                 Add_temp.Step = temp.Step + 1;
  102.                 Add_Stack_Head(Add_temp);
  103.                 Set_Visit(Add_temp);
  104.             }
  105.         }
  106.     }

  107.     printf("\n end \n");
  108.     return 0;
  109. }

  110. int main(void){

  111.     Point p;
  112.     p.x = 1;
  113.     p.y = 1;
  114.     p.Step = 1;

  115.     Init_Visit();
  116.     Init_Stack();
  117.     
  118.     DFS(p);



  119.     return 0;
  120. }

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