zoukankan      html  css  js  c++  java
  • UVA:1600 巡逻机器人

    机器人要从一个m*n(m和n的范围都在1到20的闭区间内)的网格的左上角(1,1)走到右下角(m,n)。网格中的一些格子是空地,用0表示,其它格子是障碍,用1表示。机器人每次可以往四个方向走一格,但不能连续地穿越k( [0,20] )个障碍,求最短路长度。起点和终点保证是空地。

    思路:用bfs搜索即可,由于不能连续地穿越k个障碍,所以在原本的vis2维数组上面再添加1维,变成3维数组,表示穿越的墙的层数(障碍)。

    1. #include<cstdio>  
    2. #include<cstring>  
    3. #include<iostream>  
    4. #include<queue>  
    5. using namespace std;  
    6. int n,m,k,t;  
    7. int map[25][25];  
    8. bool vis[25][25][25];//增加一个维度Z来表示跃过的墙的层数。  
    9. int dx[4]= {1,0,-1,0};  
    10. int dy[4]= {0,1,0,-1};  
    11. struct Point  
    12. {  
    13.     int x,y;  
    14.     int step;  
    15.     int layer;  
    16.     Point(int x=1,int y=1,int step=0,int layer=0):x(x),y(y),step(step),layer(layer) {}  
    17. };  
    18. int ans()  
    19. {  
    20.     queue<Point>Q;  
    21.     memset(vis,0,sizeof(vis));  
    22.     Point a(1,1,0,0);  
    23.     Point end_point(n,m);  
    24.     Q.push(a);  
    25.     vis[1][1][0]=true;  
    26.     while(!Q.empty())  
    27.     {  
    28.         Point now=Q.front();  
    29.         Q.pop();  
    30.         if(now.x==end_point.x&&now.y==end_point.y)  
    31.             return now.step;  
    32.         for(int i=0; i<4; i++)  
    33.         {  
    34.             int x=dx[i]+now.x;  
    35.             int y=dy[i]+now.y;  
    36.             int layer=now.layer;  
    37.             if(map[x][y])  
    38.                 layer++;//因为每次只会移动一步。而且遇见一层墙,所以加加。  
    39.                 else  
    40.                 layer=0;  
    41.             if(layer<=k&&!vis[x][y][layer]&&x>=1&&y>=1&&x<=n&&y<=m)  
    42.             {  
    43.                 vis[x][y][layer]=true;  
    44.                 Q.push(Point(x,y,now.step+1,layer));  
    45.             }  
    46.         }  
    47.     }  
    48.     return -1;  
    49. }  
    50. int main()  
    51. {  
    52.     cin>>t;  
    53.     while(t--)  
    54.     {  
    55.         cin>>n>>m>>k;//输入行和列以及k值!  
    56.         for(int i=1; i<=n; i++)  
    57.             for(int j=1; j<=m; j++)  
    58.                 cin>>map[i][j];//输入地图  
    59.         cout<<ans()<<endl;  
    60.     }  
    61.     return 0;  
    62. }  
  • 相关阅读:
    bzoj3473 字符串
    洛谷P4070 生成魔咒
    洛谷P3975 弦论
    AT1219 歴史の研究
    课上讲的几个新的技巧
    索引与视图(转载)
    oracle中的分支与循环语句
    Oracle to_date()函数的用法《转载》
    自定义函数的存储过程的区别
    UNION 和 UNION ALL 操作符
  • 原文地址:https://www.cnblogs.com/stoker/p/9100029.html
Copyright © 2011-2022 走看看