hdu 1072 Nightmare(bfs)

小记:完全拿来练手感,练手速,练自己的bfs逻辑能力的


思路:bfs,输入时记下起点和终点,在bfs判断时,我们往4个方向判的时候就直接判断其时间是否等于0,即如果往旁边走,时间是否等于0,等于的话就不需要再往下了。

然后就是边界判断,且不能是墙。这时我们再看它是否可以reset炸弹的时间,可以就reset,然后我们要对每一点进行标记,防止多做无用功。

标记这里同时也起了标记到达这点剩余最多的时间,如果再到这点来的,而它的剩余时间没有这点历史上剩余的最多的多,那么就没必要入队了。

然后便是将符合要求的全部入队,出队时,判断是否是终点即可。 因为之前判了时间为0的点了,所以这里不会出现到达终点为0的情况。

其实题目的描述挺吓人的,说什么可以在一点reset几次,其实reset一次就行了,如果reset这一次之后还是到达不了终点,你再去reset还是到达不了。


代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define eps 10e-8

const int MAX_ = 60;
const int N = 100010;
const int INF = 0x7fffffff;

struct Point {
    int x, y;
    int re;
    int step;
}p2, p1;

int dir[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
int vis[MAX_][MAX_];
int a[MAX_][MAX_], num[10][10][100];
int n, m, k, ti;

char str[MAX_][MAX_];


int bfs(Point x)
{
    queue<Point> q;

    mst(vis, 0);
    x.step = 0;
    x.re = 6;
    q.push(x);

    while(!q.empty()){
        Point cur;
        cur = q.front(); q.pop();

        if(cur.x == p2.x && cur.y == p2.y ){
            return cur.step;
        }

        REP(i, 0, 6){
            Point nt;
            nt.x = cur.x + dir[i][1];
            nt.y = cur.y + dir[i][0];
            nt.step = cur.step+1;
            nt.re = cur.re-1;
            if(nt.re == 0)continue;

            if((nt.x > -1 && nt.x < n) &&
               (nt.y > -1 && nt.y < m) &&
               a[nt.x][nt.y] ){
                    if(a[nt.x][nt.y] == 4)nt.re = 6;

                    if(!vis[nt.x][nt.y] || nt.re > vis[nt.x][nt.y]){
                        vis[nt.x][nt.y] = nt.re;
                        q.push(nt);
                    }
            }
        }
    }
    return -1;
}

int main(){
	int T, cnt;
	scanf("%d", &T);
	while(T-- && scanf("%d%d", &n, &m)){
	    cnt = 0;

        REP(i, 0, n)REP(j, 0, m){
            scanf("%d", &a[i][j]);
            if(a[i][j] == 2){
                p1.x = i;
                p1.y = j;
            }
            else if(a[i][j] == 3){
                p2.x = i;
                p2.y = j;
            }
        }

        int ans;
        ans = bfs(p1);
        printf("%d\n",ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值