hdu 1072 Nightmare (广搜)

/*

题意: 2为起点,3为终点,开始时间为6,没走一步减1; 遇到4时重新计时,可重复走

解题:就是普通的广搜,唯一不同的是路可重复走,将标记是否走过的mark数组,标记为走过一次加1,每一个位子最多可走8次即可。

*/


#include<iostream>
#include<queue>
using namespace std;
int n, m, s[10][10], mark[10][10], dir[4][2] = {1,0, 0,1, -1,0, 0,-1};
int flag, s_x, s_y, e_x, e_y;

struct node
{
	int x, y, step;
	int flag_step;
};

int judge(int x, int y)
{
	if(x>=0 && x<n && y>=0 && mark[x][y]<8 && s[x][y] != 0)
		return 1;
	else
		return 0;
}

int bfs()
{
	int i;
	queue<node>q;
	node cur, next;
	cur.x=s_x; cur.y=s_y; cur.step=0;
	cur.flag_step=0;
	q.push(cur);
	while(!q.empty())
	{
		cur = q.front();
		q.pop();
		if(cur.x==e_x && cur.y==e_y) return cur.step;
		if(cur.flag_step == 5) continue;
		for( i=0; i < 4; i++ )
		{
			next.x = cur.x + dir[i][0];
			next.y = cur.y + dir[i][1];
			if(judge(next.x, next.y))
			{
				mark[next.x][next.y] += 1;
				next.step = cur.step+1;
				next.flag_step = cur.flag_step+1;
				if(s[next.x][next.y] == 4)
					next.flag_step = 0;
				q.push(next);
			}
		}
	}
	return -1;
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int i, j;
		scanf("%d%d", &n, &m);
		for( i=0; i < n; i++ )
			for(j=0; j < m;j++ )
			{
				scanf("%d", &s[i][j]);
				if(s[i][j] == 2)
					s_x=i, s_y=j;
				if(s[i][j] == 3)
					e_x=i, e_y=j;
			}
			
		memset(mark, 0, sizeof(mark));
		flag = 0;
		int ans = bfs();
		printf("%d\n", ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值