ZOJ 3865 Superbot

题意:

走迷宫。。。

思路:

dis[x][y][t][d]表示从起点出发走到(x,y),花费时间%p=t并且移动光标在d的最小话费,然后判断一下条件bfs就行了。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

#define N 20
#define M 140000
#define mod 10086
#define LL long long
#define Pi acos(-1.0)
#define inf 0x3f3f3f3f

char g[N][N]; int dis[N][N][51][4];
bool vis[N][N][51][4];

int dx[] = {0, 0, -1, 1 };
int dy[] = {-1, 1, 0, 0 };

int p, n, m;
struct node {
    int x, y, dir, t;
    node () {}
    node ( int xx, int yy, int tt, int dd ) {
        x = xx, y = yy, dir = dd, t = tt;
    }
}Q[M];

bool out ( int x, int y ) {
    if( x < 0 || x >= n || y < 0 || y >= m ) return 1;
    return 0;
}
void spfa ( int x, int y ) {
    memset( vis, 0, sizeof( vis ) );
    memset( dis, 0x3f, sizeof( dis )  );
    dis[x][y][0][0] = 0;
    vis[x][y][0][0] = 1;
    int head = 0, tail = 0;
    Q[tail++] = node ( x, y, 0, 0 );
    while( head < tail )
    {
        node now = Q[head ++];
        int nx = now.x, ny = now.y, nt = now.t, nd = now.dir;
        int xx = nx + dx[nd], yy = ny + dy[nd], dd, tt = ( nt + 1 ) % p;
        vis[nx][ny][nt][nd] = 0;
        //printf("%d %d %d %d %d\n", nx, ny, nt, nd, dis[nx][ny][nt][nd]);
        if( tt == 0 ) dd = ( nd + 3 ) % 4;
        else dd = nd;
        if( dis[nx][ny][tt][dd] > dis[nx][ny][nt][nd] + 1 ){
            dis[nx][ny][tt][dd] = dis[nx][ny][nt][nd] +1;
            if( !vis[nx][ny][tt][dd] ) {
                vis[nx][ny][tt][dd] = 1;
                Q[tail++] = node ( nx, ny, tt, dd );
            }
        }
        if( !out( xx, yy ) && g[xx][yy] != '*' ) {
            if ( tt == 0 ) dd = ( nd + 3 ) % 4;
            else dd = nd;
            if( dis[xx][yy][tt][dd] > dis[nx][ny][nt][nd] + 1 ) {
                dis[xx][yy][tt][dd] = dis[nx][ny][nt][nd] + 1;
                if( !vis[xx][yy][tt][dd] ) {
                    vis[xx][yy][tt][dd] = 1;
                    Q[tail++] = node( xx, yy, tt, dd );
                }
            }
        }
        dd = ( nd + 1 ) % 4;
        if( tt == 0 ) dd = ( dd + 3 ) % 4;
        xx = nx, yy = ny;
        if( dis[xx][yy][tt][dd] > dis[nx][ny][nt][nd] + 1 ) {
            dis[xx][yy][tt][dd] = dis[nx][ny][nt][nd] +1;
            if( !vis[xx][yy][tt][dd] ) {
                vis[xx][yy][tt][dd] =1;
                Q[tail++] = node ( xx, yy, tt, dd );
            }
        }
        dd = ( nd + 3 ) % 4;
        if( tt == 0 ) dd = ( dd + 3 ) % 4;
        if( dis[xx][yy][tt][dd] > dis[nx][ny][nt][nd] + 1 ) {
            dis[xx][yy][tt][dd] = dis[nx][ny][nt][nd] +1;
            if( !vis[xx][yy][tt][dd] ) {
                vis[xx][yy][tt][dd] =1;
                Q[tail++] = node ( xx, yy, tt, dd );
            }
        }
    }
}

int main ()
{
   //freopen("tt.txt", "r", stdin );
    int T;
    scanf("%d", &T );
    while(T-- ){
        scanf("%d%d%d", &n, &m, &p );
        for( int i = 0;i <n; ++i )
            scanf("%s", g[i] );
        int sx, sy, ex, ey;
        for( int i = 0; i <n; ++i )
            for( int j = 0; j <m; ++j ) {
                if( g[i][j] == '@')
                    sx = i, sy = j;
                if( g[i][j] == '$')
                    ex = i, ey = j;
            }
        spfa( sx, sy );
        int ans = inf;
        for( int i = 0; i < 4; ++i )
            for( int j = 0; j < p; ++j )
            ans = min ( ans, dis[ex][ey][j][i] );
        if( ans == inf )
            puts("YouBadbad");
        else printf("%d\n", ans );
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值