HDU 1072 Nightmare BFS
来源:互联网 发布:网络舆论 新浪微舆情 编辑:程序博客网 时间:2024/05/07 23:25
题意:在N∗M 的矩阵内只可以走六步,如果碰到4就可以再走六步,问能不能走出迷宫?(如果能走出,输出最少的秒数,如果不能走出,输出-1。)
思路:BFS呀,我开始蠢,写了DFS。建一个t数组,tij 代表走到(i,j) 还剩几步可以走,如果碰到4的话,设置一个时间,重新加入队列即可。
http://acm.hdu.edu.cn/showproblem.php?pid=1072
/********************************************* Problem : HDU 1072 Author : NMfloat InkTime (c) NM . All Rights Reserved .********************************************/#include <map>#include <set>#include <queue>#include <cmath>#include <ctime>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define rep(i,a,b) for(int i = (a) ; i <= (b) ; i ++)#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --)#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)#define cls(a,x) memset(a,x,sizeof(a))#define eps 1e-8using namespace std;const int MOD = 1e9+7;const int INF = 0x3f3f3f3f;const int MAXN = 1e5+5;const int MAXE = 2e5+5;struct Node { int x,y,t;//t代表走到这一秒最少的时间 int cost;//消耗的时间 Node(){} Node(int _x,int _y,int _t,int _cost){x = _x ; y = _y ; t = _t;cost = _cost;}};typedef long long LL;typedef unsigned long long ULL;int T,n,m,k;queue<Node>q;int fx[] = {0,1,-1,0,0};int fy[] = {0,0,0,-1,1};int sx,sy;int Map[10][10];int t[10][10];//走到这一步最短的时间int ok ;void BFS() { while(!q.empty()) q.pop(); q.push(Node(sx,sy,6,0)); t[sx][sy] = 6; while(!q.empty()) { Node tmp = q.front(); q.pop(); // printf("x : %d y : %d tmp : %d cost : %d\n",tmp.x,tmp.y,tmp.t,tmp.cost); if(tmp.t == 0) continue; if(Map[tmp.x][tmp.y] == 3) { ok = 1; printf("%d\n",tmp.cost); break; } rep(i,1,4) { int tmpx = tmp.x + fx[i]; int tmpy = tmp.y + fy[i]; int tmpt = tmp.t - 1; if(tmpx >= 1 && tmpx <= n && tmpy >= 1 && tmpy <= m) { if(t[tmpx][tmpy] < tmpt) { t[tmpx][tmpy] = tmpt; if(Map[tmpx][tmpy] == 1 || Map[tmpx][tmpy] == 3) { q.push(Node(tmpx,tmpy,tmp.t-1,tmp.cost+1)); } else if(Map[tmpx][tmpy] == 4) { t[tmpx][tmpy] = 6; q.push(Node(tmpx,tmpy,6,tmp.cost+1)); } } } } }}void input() { scanf("%d %d",&n,&m); rep(i,1,n) rep(j,1,m) { scanf("%d",&Map[i][j]); if(Map[i][j] == 2) { sx = i ; sy = j; } } }void solve() { cls(t,0); ok = 0; BFS(); if(!ok) puts("-1");}int main(void) { //freopen("a.in","r",stdin); scanf("%d",&T); while(T--) { //while(~scanf("%d %d",&n,&m)) { //while(scanf("%d",&n),n) { input(); solve(); } return 0;}
0 0
- BFS hdu 1072 Nightmare
- HDU 1072 Nightmare BFS
- hdu 1072 Nightmare BFS
- hdu 1072 Nightmare bfs
- Hdu 1072 Nightmare[Bfs]
- hdu 1072 Nightmare--bfs
- HDU 1072 Nightmare(BFS)
- HDU 1072 Nightmare (BFS)
- HDU 1072 Nightmare (BFS)
- bfs-hdu-1072-Nightmare
- HDU 1072 Nightmare【BFS】
- HDU 1072 Nightmare BFS
- HDU 1072 Nightmare BFS
- hdu 1072 Nightmare (bfs)
- hdu 1072 Nightmare (BFS)
- HDOJ/HDU 1072 Nightmare (bfs)
- hdu 1072 Nightmare bfs()
- HDU 1072 Nightmare 限制BFS
- 分治法
- android 多级下拉菜单实现教程
- 使用PHP代码实现网站的301转向方法
- VLFeat教程k-means
- C语言学习笔记----伊能C语言学习笔记----指针与地址的区别是什么?
- HDU 1072 Nightmare BFS
- jmeter-结果查看器plugin
- P04(*) 获取列表的长度【重补】
- 【C#数据结构】快速排序
- Objective-C总Runtime的那点事儿(一)消息机制
- 线程阻塞与Handler相关超详细原理讲解
- 黑马程序员——Java之集合(下)
- android四大组件
- 用UICollectionView修改微博,相对布局色熟悉