UVa 1600

题意

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

思路

BFS
加一个维度记录机器人已经穿越了几层障碍

AC代码

新手代码写的还是比较冗长了
献丑

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int k[30][30], vis[30][30][30], pre[1500];
int turn[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int m, n;

struct POINT
{
    int x, y, z;
}que[1500];

int cnt, heart;
bool flag;

void cntpls( int a ){
    int t = pre[a];
    if( t != 0 )
        cntpls(t);
    cnt++;
}

void Print( int n )
{
    cntpls(n);
    printf("%d
",cnt);
}

bool go( int x, int y, int p )
{
    if( x >= 0 && x < m && y >= 0 && y < n )
        return true;
    return false;
}

void BFS()
{
    cnt = 0;
    flag = false;
    memset(vis, 0, sizeof(vis));
    int head = 0, tile = 1, a, b, xx, yy, zz;
    que[0].x = 0, que[0].y = 0, que[0].z = 0;
    pre[0] = -1;
    while( head < tile ){
        a = que[head].x;
        b = que[head].y;
        if( a == m-1 && b == n-1 ){
            Print(head);
            flag = true;
            return;
        }
        for( int i = 0; i < 4; i++ ){
            xx = a + turn[i][0];
            yy = b + turn[i][1];
            zz = que[head].z;
            if( k[xx][yy] )
                zz++;
            else
                zz = 0;
            if( zz <= heart && !vis[xx][yy][zz] && go(xx, yy, zz) ){
                vis[xx][yy][zz] = 1;
                que[tile].x = xx;
                que[tile].y = yy;
                que[tile].z = zz;
                pre[tile] = head;
                tile++;
            }
        }
        head++;
    }
    return;
}



int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&m,&n,&heart);
        for( int i = 0; i < m; i++ )
            for( int j = 0; j < n; j++ )
                scanf("%d",&k[i][j]);
        if( m == 1 && n == 1 ){
            printf("0
");
            continue;
        }
        else
            BFS();
        if(!flag)
            printf("-1
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/JinxiSui/p/9740598.html