zoukankan      html  css  js  c++  java
  • UVA 1600 Patrol Robot

    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 aij separated 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 0 otherwise.

    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
    
    
    
    
    有限制条件的BFS题,注意统计每个节点的剩余“命数”。碰到0的时候“满血复活”
    #include <bits/stdc++.h>
    using namespace std;
    
    int m, n, k, ans;
    int table[22][22];
    bool vis[22][22][22];
    
    struct Node{
        int r, c;
        int step;
        int k;
        Node(int r = 0, int c = 0, int step = 0, int k = 0) : r(r), c(c), step(step), k(k) {}
    };
    
    void input()
    {
        memset(table, 0, sizeof(table));
        memset(vis, false, sizeof(vis));
        cin >> m >> n >> k;
        for(int i = 1; i <= m; i++)
            for(int j = 1; j <= n; j++)
                cin >> table[i][j];
    }
    
    Node walk(const Node & x, const int i)
    {
        if(i==0) return Node(x.r+1, x.c, x.step+1, x.k);
        if(i==1) return Node(x.r, x.c+1, x.step+1, x.k);
        if(i==2) return Node(x.r-1, x.c, x.step+1, x.k);
        if(i==3) return Node(x.r, x.c-1, x.step+1, x.k);
    }
    
    bool is_inside(const Node & x)
    {
        return x.r>=1 && x.r<=m && x.c>=1 && x.c<=n;
    }
    
    void bfs()
    {
        queue<Node> qu;
        qu.push(Node(1, 1, 0, k));
        while(!qu.empty())
        {
            Node u = qu.front(); qu.pop();
            if(u.r == m && u.c == n){
                ans = u.step;
                return;
            }
            if(u.k >= 0)
                for(int i = 0; i < 4; i++){
                    Node v = walk(u, i);
                    if(table[v.r][v.c]) v.k--;
                    else v.k = k;
                    if(is_inside(v) && !vis[v.r][v.c][v.k] && v.k>=0){
                        qu.push(v);
                        vis[v.r][v.c][v.k] = true;
                    }
                }
        }
        ans = -1;
    }
    
    int main()
    {
        int T; cin >> T;
        while(T--)
        {
            input();
            bfs();
            cout << ans << endl;
        }
        return 0;
    }


  • 相关阅读:
    bigtint;int;smallint;tinyint
    修改sqlserver2008默认的数据库文件保存路径
    通过代码来调用log4net写日志
    supersocket中的日志处理
    QuickStart下的CommandFilter项目 github上自己修改过的版本
    演练:实现支持基于事件的异步模式的组件
    BroadcastService的测试用例
    2-Medium下的MultipleCommandAssembly
    如何获取supersocket的源代码
    supersocket中quickstart文件夹下的MultipleCommandAssembly的配置文件分析
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312775.html
Copyright © 2011-2022 走看看