uva1600 Patrol Robot 巡逻机器人(BFS)

</pre>Description<div class="textBG" style="border-width: 2px; border-style: dotted; padding: 10px; font-family: 'times new roman'; font-size: 17px; background-color: rgb(234, 235, 255);"><span style="float: right;"><a target=_blank style="color: blue; text-decoration: none;" href="http://7xjob4.com1.z0.glb.clouddn.com/1e64a3999d53f31d7746044a838fd900" target="_blank"><img width="100" height="26" title="Download as PDF" alt="Download as PDF" src="http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png" border="0" /></a></span> <div style="clear: both;"></div><p style="margin-top: 0px; margin-bottom: 0pt;">A robot has to patrol around a rectangular area which is in a form of <span class="MATH"><em>m</em><tt>x</tt><em>n</em></span> grid (<span class="MATH"><em>m</em></span> rows and <span class="MATH"><em>n</em></span> columns). The rows are labeled from 1 to <span class="MATH"><em>m</em></span>. The columns are labeled from 1 to <span class="MATH"><em>n</em></span>. A cell <span class="MATH">(<em>i</em>, <em>j</em>)</span> denotes the cell in row <span class="MATH"><em>i</em></span> and column <span class="MATH"><em>j</em></span> in the grid. At each step, the robot can only move from one cell to an adjacent cell, i.e. from <span class="MATH">(<em>x</em>, <em>y</em>)</span> to <span class="MATH">(<em>x</em> + 1, <em>y</em>)</span>, <span class="MATH">(<em>x</em>, <em>y</em> + 1)</span>, <span class="MATH">(<em>x</em> - 1, <em>y</em>)</span> or <span class="MATH">(<em>x</em>, <em>y</em> - 1)</span>. Some of the cells in the grid contain obstacles. In order to move to a cell containing obstacle, the robot has to switch to turbo mode. Therefore, the robot cannot move continuously to more than <span class="MATH"><em>k</em></span> cells containing obstacles.</p><p style="margin-top: 0px; margin-bottom: 0pt;">Your task is to write a program to find the shortest path (with the minimum number of cells) from cell (1, 1) to cell <span class="MATH">(<em>m</em>, <em>n</em>)</span>. It is assumed that both these cells do not contain obstacles.</p><p style="margin-top: 0px; margin-bottom: 0pt;"></p><h2 style="margin-bottom: 0px;"><a target=_blank name="t0"></a><span style="color: rgb(255, 0, 0); font-size: 18px;"><a target=_blank name="SECTION0001001000000000000000" style="color: blue; text-decoration: none;" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49764" target="_blank">Input</a> </span></h2><p style="margin-top: 0px; margin-bottom: 0pt;">The input consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.</p><p style="margin-top: 0px; margin-bottom: 0pt;">For each data set, the first line contains two positive integer numbers <span class="MATH"><em>m</em></span> and <span class="MATH"><em>n</em></span> separated by space <span class="MATH">(1<img width="18" height="31" align="MIDDLE" alt="$ \le$" src="" border="0" /><em>m</em>, <em>n</em><img width="18" height="31" align="MIDDLE" alt="$ \le$" src="" border="0" />20)</span>. The second line contains an integer number <span class="MATH"><em>k</em></span><span class="MATH">(0<img width="18" height="31" align="MIDDLE" alt="$ \le$" src="" border="0" /><em>k</em><img width="18" height="31" align="MIDDLE" alt="$ \le$" src="" border="0" />20)</span>. The <span class="MATH"><em>i</em><sup>th</sup></span> line of the next <span class="MATH"><em>m</em></span> lines contains <span class="MATH"><em>n</em></span> integer <span class="MATH"><em>a</em><sub>ij</sub></span> separated by space <span class="MATH">(<em>i</em> = 1, 2,..., <em>m</em>;<em>j</em> = 1, 2,..., <em>n</em>)</span>. The value of <span class="MATH"><em>a</em><sub>ij</sub></span> is <tt>1</tt> if there is an obstacle on the cell <span class="MATH">(<em>i</em>, <em>j</em>)</span>, and is <tt>0</tt> otherwise.</p><p style="margin-top: 0px; margin-bottom: 0pt;"></p><h2 style="margin-bottom: 0px;"><a target=_blank name="t1"></a><span style="color: rgb(255, 0, 0); font-size: 18px;"><a target=_blank name="SECTION0001002000000000000000" style="color: blue; text-decoration: none;" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49764" target="_blank">Output</a> </span></h2><p style="margin-top: 0px; margin-bottom: 0pt;">For each data set, if there exists a way for the robot to reach the cell <span class="MATH">(<em>m</em>, <em>n</em>)</span>, write in one line the integer number <span class="MATH"><em>s</em></span>, which is the number of moves the robot has to make; <tt>-1</tt> otherwise.</p><p style="margin-top: 0px; margin-bottom: 0pt;"></p><h2 style="margin-bottom: 0px;"><a target=_blank name="t2"></a><span style="color: rgb(255, 0, 0); font-size: 18px;"><a target=_blank name="SECTION0001003000000000000000" style="color: blue; text-decoration: none;" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49764" target="_blank">Sample Input</a> </span></h2><p style="margin-top: 0px; margin-bottom: 0pt;"></p><pre style="white-space: pre-wrap; -ms-word-wrap: break-word;">3 
2 5 
0 
0 1 0 0 0 
0 0 0 1 0 
4 6 
1 
0 1 1 0 0 0
0 0 1 0 1 1
0 1 1 1 1 0
0 1 1 1 0 0
2 2 
0 
0 1 
1 0

Sample Output 

7 
10 
-1
 
 
 
 
 
 
 
代码如下 :(不要直接复制 有些许不一样)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int maze[22][22];
int n,m,ax,ay,gx,gy,k,T;
struct node
{
    int x;
    int y;
    int num;
}a,b;
int j;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int d[22][22];
int vis[22][22];
int bfs()
{j=0;
memset(d,0,sizeof(d));
memset(vis,0,sizeof(vis));
 a.x=1;a.y=1;a.num=0;
    queue<node>q;
    q.push(a);
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        if(a.x==m&&a.y==n)
                    return d[a.x][a.y];
        for(int i=0;i<4;i++)
        {
            b.x=a.x+dx[i];
            b.y=a.y+dy[i];
            b.num=a.num;
            if(maze[b.x][b.y]==1)
            {
                a.num++;
                b.num=a.num;
            }
            if(maze[b.x][b.y]==0)
            b.num=0;
            if(b.x>0&&b.x<=m&&b.y>0&&b.y<=n&&vis[b.x][b.y]==0&&b.num<=k)
            {



             d[b.x][b.y]=d[a.x][a.y]+1;
                    vis[b.x][b.y]=-1;
                    q.push(b);

             
              j++;
              if(j>m*n)
            return -1;

           


            }
        }

    }
    return -1;
}
int main()
{while(~scanf("%d",&t)&&t)
{
    while(t--)
{
    

   scanf("%d%d",&m,&n);
    
       
            scanf("%d",&k);
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&maze[i][j]);

            }
        }
        int t=bfs();
       cout<<t<<endl;
      
}
    }
    return 0;

}

 
 
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值