Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1547067
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2017-05-15 18:53:45


  1. //英雄,广度优先搜索
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. int mat[101][101];
  5. int mark[101][101];
  6. int n,m;
  7. int sx,sy,ex,ey;
  8. typedef struct _node{
  9.    int x,y;
  10. }node;

  11. node arr[10001];
  12. int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//右下左上

  13. void process()
  14. {
  15.    int i,j;
  16.    int k,u;//k个spring,power is u
  17.    int s,t,f;//s,t是队列的头和尾,f是某一层的队列的尾
  18.    int x,y;
  19.    int time=1;
  20.    for(i=0;i<n;i++){
  21.       for(j=0;j<m;j++){
  22.          mat[i][j]=mark[i][j]=0;
  23.       }
  24.    }
  25.    scanf("%d", &k);
  26.    while(k--){
  27.       scanf("%d %d %d",&i,&j,&u);
  28.       i--,j--;//1位偏移
  29.       mat[i][j]=u;
  30.    }
  31.    scanf("%d %d %d %d", &sx,&sy,&ex,&ey);
  32.    sx--,sy--,ex--,ey--;//1位偏移

  33.    s=t=0;
  34.    arr[0].x=sx,arr[0].y=sy;
  35.    if(sx==ex && sy==ey){
  36.       printf("0\n");
  37.       return;
  38.    }
  39.    mark[sx][sy]=1;
  40.    while(s<=t){
  41.       f=t;//一层遍历完 f更新一次, time更新一次
  42.       while(s<=f){
  43.          for(i=0;i<4;i++){
  44.             x=arr[s].x+dir[i][0];
  45.             y=arr[s].y+dir[i][1];
  46.             if(0<=x && x<n && 0<=y && y<m){
  47.                while(1){
  48.                   if(mat[x][y]>0){
  49.                      switch(i)
  50.                      {
  51.                      case 0://
  52.                         y=y+mat[x][y];
  53.                         if(y>=m)y=m-1;
  54.                         break;
  55.                      case 1://
  56.                         x=x+mat[x][y];
  57.                         if(x>=n)x=n-1;
  58.                         break;
  59.                      case 2://
  60.                         y=y-mat[x][y];
  61.                         if(y<0)y=0;
  62.                         break;
  63.                      case 3://
  64.                         x=x-mat[x][y];
  65.                         if(x<0)x=0;
  66.                         break;
  67.                      }
  68.                   }
  69.                   else break;
  70.                }
  71.                if(mark[x][y]==0){
  72.                   mark[x][y]=1;
  73.                   t++;
  74.                   arr[t].x=x;
  75.                   arr[t].y=y;
  76.                   if(x==ex && y==ey)
  77.                   {
  78.                      printf("%d\n",time);
  79.                      return;
  80.                   }
  81.                }
  82.             }
  83.          }//Four directions
  84.          s++;//当某一点的四个方向都遍历完后,头指针就要移动了
  85.       }//while(s<=f)
  86.       time++;
  87.    }
  88.    printf("Impossible\n");
  89. }

  90. int _tmain(int argc, _TCHAR* argv[])
  91. {
  92.    int test_case;
  93.    int i;
  94.    freopen("acm02_0408in.txt","r",stdin);
  95.    freopen("acm02_0408out.txt","w",stdout);
  96.    setbuf(stdout,NULL);
  97.    scanf("%d", &test_case);
  98.    for(i=0;i<test_case;i++)
  99.    {
  100.       scanf("%d %d",&n,&m);
  101.       process();
  102.    }
  103.     return 0;
  104. }

  1. /* in
  2. 8
  3. 3 3
  4. 1
  5. 2 2 2
  6. 2 3
  7. 2 1
  8. 3 10
  9. 1
  10. 2 2 4
  11. 2 3
  12. 2 6
  13. 3 10
  14. 1
  15. 2 2 5
  16. 2 1
  17. 2 10
  18. 10 3
  19. 1
  20. 2 2 5
  21. 1 2
  22. 10 2
  23. 10 10
  24. 0
  25. 2 2
  26. 7 8
  27. 10 10
  28. 1
  29. 2 7 5
  30. 2 8
  31. 1 1
  32. 10 10
  33. 1
  34. 2 7 10
  35. 2 8
  36. 1 1
  37. 10 10
  38. 2
  39. 2 7 5
  40. 2 5 5
  41. 2 8
  42. 1 1
  43. */

  44. /* out
  45. 1
  46. 2
  47. 4
  48. 4
  49. 11
  50. 3
  51. 2
  52. 3
  53. */

另外一种解法,用的存储空间多些,但是程序简洁一些。

  1. //英雄,广度优先搜索
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. int mat[100][100];
  5. int mark[100][100];
  6. int n,m;
  7. int sx,sy,ex,ey;
  8. typedef struct _node{
  9.    int x,y;
  10.    int s;
  11. }node;

  12. node arr[10000];
  13. int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//右下左上

  14. void process()
  15. {
  16.    int i,j;
  17.    int k,u;//k个spring,power is u
  18.    int h,t;//h,t是队列的头和尾
  19.    int x,y;

  20.    for(i=0;i<n;i++){
  21.       for(j=0;j<m;j++){
  22.          mat[i][j]=mark[i][j]=0;
  23.       }
  24.    }
  25.    scanf("%d", &k);
  26.    while(k--){
  27.       scanf("%d %d %d",&i,&j,&u);
  28.       i--,j--;//1位偏移
  29.       mat[i][j]=u;
  30.    }
  31.    scanf("%d %d %d %d", &sx,&sy,&ex,&ey);
  32.    sx--,sy--,ex--,ey--;//1位偏移

  33.    h=t=0;
  34.    arr[0].x=sx,arr[0].y=sy,arr[0].s=0;
  35.    if(sx==ex && sy==ey){
  36.       printf("0\n");
  37.       return;
  38.    }
  39.    mark[sx][sy]=1;
  40.    while(h<=t){
  41.       for(i=0;i<4;i++){
  42.          x=arr[h].x+dir[i][0];
  43.          y=arr[h].y+dir[i][1];
  44.          if(0<=x && x<n && 0<=y && y<m){
  45.             while(1){
  46.                if(mat[x][y]>0){
  47.                   switch(i)
  48.                   {
  49.                   case 0://
  50.                      y=y+mat[x][y];
  51.                      if(y>=m)y=m-1;
  52.                      break;
  53.                   case 1://
  54.                      x=x+mat[x][y];
  55.                      if(x>=n)x=n-1;
  56.                      break;
  57.                   case 2://
  58.                      y=y-mat[x][y];
  59.                      if(y<0)y=0;
  60.                      break;
  61.                   case 3://
  62.                      x=x-mat[x][y];
  63.                      if(x<0)x=0;
  64.                      break;
  65.                   }
  66.                }
  67.                else break;
  68.             }
  69.             if(mark[x][y]==0){
  70.                mark[x][y]=1;
  71.                t++;
  72.                arr[t].x=x;
  73.                arr[t].y=y;
  74.                     arr[t].s=arr[h].s+1;
  75.                if(x==ex && y==ey)
  76.                {
  77.                   printf("%d\n",arr[t].s);
  78.                   return;
  79.                }
  80.             }
  81.          }
  82.       }//Four directions
  83.       h++;//当某一点的四个方向都遍历完后,头指针就要移动了
  84.    }
  85.    printf("Impossible\n");
  86. }

  87. int _tmain(int argc, _TCHAR* argv[])
  88. {
  89.    int test_case;
  90.    int i;
  91.    freopen("acm02_0408in.txt","r",stdin);
  92.    freopen("acm02_0408out.txt","w",stdout);
  93.    setbuf(stdout,NULL);
  94.    scanf("%d", &test_case);
  95.    for(i=0;i<test_case;i++)
  96.    {
  97.       scanf("%d %d",&n,&m);
  98.       process();
  99.    }
  100.    return 0;
  101. }

阅读(1315) | 评论(0) | 转发(0) |
0

上一篇:20160419 ADV

下一篇:[20170517 ADV]排列组合

给主人留下些什么吧!~~