• UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS) 解题心得


    原题:

    Description

    Download as PDF
     

    A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (ij) denotes the cell in row i and column j in the grid. At each step, the robot can only move from one cell to an adjacent cell, i.e. from (xy) to (x + 1, y), (xy + 1), (x - 1, y) or (xy - 1). 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 kcells containing obstacles.

    Your task is to write a program to find the shortest path (with the minimum number of cells) from cell (1, 1) to cell(mn). It is assumed that both these cells do not contain obstacles.

    Input 

    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.

    For each data set, the first line contains two positive integer numbers m and n separated by space (1$ le$mn$ le$20). The second line contains an integer number k(0$ le$k$ le$20). The ith line of the next m lines contains n integer aijseparated by space (i = 1, 2,..., m;j = 1, 2,..., n). The value of aij is 1 if there is an obstacle on the cell (ij), and is 0otherwise.

    Output 

    For each data set, if there exists a way for the robot to reach the cell (mn), write in one line the integer number s, which is the number of moves the robot has to make; -1 otherwise.

    Sample Input 

    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

    我的代码:

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<queue>
      5 
      6 using namespace std;
      7 
      8 int directionX[] = { 1, 0, -1, 0 };
      9 int directionY[] = { 0, 1, 0, -1 };
     10 int m, n, k, able;
     11 int counts[25][25] = { 0 };
     12 int map[25][25] = { 0 };
     13 int Min;
     14 
     15 
     16 
     17 struct location
     18 {
     19     int x, y, step;
     20     int k;
     21     location() :x(1), y(1), step(0),k(0) {}
     22     bool operator ==(location &b)  { return x == b.x&&y == b.y; }
     23 }start,target;
     24 
     25 bool check(location & a)
     26 {
     27     if (a.x<1 || a.x>m || a.y<1 || a.y>n)        return 0;
     28     if (counts[a.x][a.y] != 0)            
     29         return 0;
     30     if (map[a.x][a.y] == 1)
     31     {
     32         if (a.k <= 0)            return 0;
     33         a.k--; return 1;
     34     }
     35     if (map[a.x][a.y] == 0)
     36     {
     37         a.k = able;
     38         return 1;
     39     }
     40 
     41 }
     42 
     43 int dfs(location & start)
     44 {
     45     location old_location, new_location;
     46     queue<location> q;
     47     old_location = start;
     48     q.push(old_location);
     49     counts[old_location.x][old_location.y] = 1;
     50     while (!q.empty())
     51     {
     52         old_location = q.front();
     53         q.pop();
     54         for (int i = 0; i < 4; i++)
     55         {
     56             new_location.x = old_location.x + directionX[i];
     57             new_location.y = old_location.y + directionY[i];
     58             new_location.k = old_location.k;
     59             if (!check(new_location))                
     60                 continue;
     61             if (new_location == target)
     62                 return old_location.step + 1;
     63             counts[new_location.x][new_location.y] = 1;
     64             new_location.step = old_location.step + 1;
     65             q.push(new_location);
     66         }
     67     }
     68     return -1;
     69 }
     70 
     71 
     72 
     73 
     74 int main() 
     75 {
     76     int t;
     77     cin >> t;
     78     while (t--)
     79     {
     80         cin >> m >> n>>k;
     81         target.x = m; target.y = n;
     82         start.k = k;
     83         able = start.k;
     84         memset(counts, 0, sizeof(counts));
     85         memset(map, 0, sizeof(map));
     86         for (int i = 1; i <= m; i++)
     87         {
     88             for (int j = 1; j <= n; j++)
     89             {
     90                 cin >> map[i][j];
     91             }
     92         }
     93         if (target == start)            Min = 0;
     94         else
     95         {
     96             Min = dfs(start);
     97         }
     98         cout << Min << endl;
     99     }
    100     
    101 
    102     return 0;
    103 }
  • 相关阅读:
    f(n)=1-1/2+1/3-1/4...+1/n
    练习2-15 求简单交错序列前N项和 (15 分)
    练习2-14 求奇数分之一序列前N项和 (15 分)
    练习2-13 求N分之一序列前N项和 (15 分)
    练习2-12 输出华氏-摄氏温度转换表 (15 分)
    【转载】如何从零开始开发一款嵌入式产品(20年的嵌入式经验分享学习,来自STM32神舟系列开发板设计师的总结
    【转载-Andrew_qian】stm32中断学习
    【转-Andrew_qian】stm32中断嵌套全攻略
    【转】写给自学者的入门指南
    【转】作为一个程序员,数学对你到底有多重要
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/4678418.html
Copyright © 2020-2023  润新知