Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119765
  • 博文数量: 11
  • 博客积分: 272
  • 博客等级: 二等列兵
  • 技术积分: 181
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-19 16:44
文章存档

2012年(2)

2011年(9)

我的朋友

分类: C/C++

2011-11-04 14:17:30

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. char forrest[10][10]=
  5. {'o','o','o','o','o','o','o','o','o','o',
  6. 'o','1','1','1','1','1','1','1','1','1',
  7. 'o','1','H','1','o','o','o','o','o','o',
  8. 'o','1','o','o','o','o','o','o','o','o',
  9. 'o','1','o','o','o','o','o','o','o','o',
  10. 'o','1','o','o','o','o','o','o','o','o',
  11. 'o','1','o','o','o','o','o','o','o','o',
  12. 'o','1','o','o','o','o','o','o','o','o',
  13. 'o','1','o','o','o','o','D','o','o','o',
  14. 'o','1','o','o','o','o','o','o','o','o'
  15. };

  16. void show_map()
  17. {
  18.         int i,j;

  19.         for(i=0;i<10;i++){
  20.                 for(j=0;j<10;j++){
  21.                         printf("%c",forrest[i][j]);
  22.                 }
  23.                 printf("\n");
  24.         }
  25. }

  26. void search(int x, int y)
  27. {
  28.         if (x>9 || y>9 || x<0 || y<0)
  29.                 return;

  30.         if(forrest[x][y] == 'D'){
  31.                 printf("haha, I got home!!!\n");
  32.                 exit(0);
  33.         }

  34.         if(forrest[x][y] == 'x')
  35.                 return;

  36.         if(forrest[x][y] == '1')
  37.                 return;

  38.         if(forrest[x][y] != 'H'){
  39.                 forrest[x][y] = 'x';
  40.                 show_map();
  41.         }

  42.         getchar();

  43.         if (x==8){
  44.                 search(x, y-1);
  45.                 search(x, y+1);
  46.         }else if(y==6){
  47.                 search(x-1, y);
  48.                 search(x+1, y);
  49.         }else if ( abs(x-8) < abs(y-6) ){
  50.                 search(x-1, y);
  51.                 search(x+1, y);
  52.                 search(x, y-1);
  53.                 search(x, y+1);
  54.         }else{
  55.                 search(x, y-1);
  56.                 search(x, y+1);
  57.                 search(x-1, y);
  58.                 search(x+1, y);
  59.         }

  60.         return;
  61. }


  62. int main()
  63. {
  64.         search(2,2);

  65.         return 0;
  66. }
阅读(2017) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~