hdu 1072 Nightmare bfs

   裸的bfs,感觉每个点可以走多次,于是用个三维数据记录了,具体没细想,早睡早起,要开始复习期末了

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int map[10][10],n,m;
struct node
{
    int x,y,t,s;
};
bool vis[10][10][7];
int dis[10][10];
queue<node> q;
int ex,ey;
node now,next;
int bfs(int x,int y)
{
    while(!q.empty())q.pop();
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[x][y]=1;
    vis[x][y][6]=1;
    now.x=x;now.y=y;now.t=6;now.s=1;
    q.push(now);
    int i;
    while(!q.empty()&&!dis[ex][ey])
    {
        now=q.front();q.pop();
        next.s=now.s+1;
        if(now.t==1)continue;
        for(i=0;i<4;i++)
        {
            next.t=now.t-1;
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            if(next.x<0||next.y<0||next.x>=n||next.y>=m||map[next.x][next.y]==0)continue;
            if(map[next.x][next.y]==4)next.t=6;
            if(vis[next.x][next.y][next.t])continue;
            if(!dis[next.x][next.y]||dis[next.x][next.y]>next.s)dis[next.x][next.y]=next.s;
            q.push(next);
            vis[next.x][next.y][next.t]=1;
        }
    }
    return dis[ex][ey]-1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        int i,j,x,y;
        for(i=0;i<n;i++)
          for(j=0;j<m;j++)
          {
             scanf("%d",&map[i][j]);
             if(map[i][j]==2)x=i,y=j;
             if(map[i][j]==3)ex=i,ey=j;
          }
        printf("%d\n",bfs(x,y));
    }
}

 

时间: 2024-08-22 15:12:52

hdu 1072 Nightmare bfs的相关文章

hdu 1495 非常可乐(BFS)

click here ~~ Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多.但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0

hdu 2612 Find a way (BFS)

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6758    Accepted Submission(s): 2253 Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally

【HDU 4771 Stealing Harry Potter&amp;#39;s Precious】BFS+状压

2013杭州区域赛现场赛二水... 类似"胜利大逃亡"的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点,搜索下一个最近的宝藏,直至找到全部k个宝藏.有点贪心的感觉. 由于求最短时间,BFS更快捷,但耗内存,这道题就卡在这里了... 这里记录了我几次剪枝的历史...题目要求内存上限32768KB,就差最后600KB了...但我从理论上觉得已经不能再剪了,留下的结点都是盲目式搜索必然要访问的结点

2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)

/* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k 种, 每一种有多个! 只要求找到每一种的其中一个就可以! 找钥匙的顺序按照 第1种, 第2种, 第3种 ....第k种! 找钥匙的时间是一步, 走到相邻空地的时间是一步, 打蛇的时间就是两步! 求找到师傅的最少步数! 这里说一下 state[N][N][10][35]表示的含义: ->state[

HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approa

HDOJ/HDU 1372 Knight Moves(经典BFS)

Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks tha

HDU 4081 Qin Shi Huang&#039;s National Road System (次小生成树算法)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=4081 题目: Problem Description During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China ---- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the

hdu 4531吉哥系列故事之乾坤大挪移

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4531 这道搜索题挺恶心的...比赛时没有写出来. 首先要解决的问题是怎样判断符合条件的状 态,即所有一样的颜色是连在一起的.我是采用最简单也最搓的方法,按上下左右顺序给每一个小三角 形标号1-36,然后建立一张邻接矩阵图,然后bfs判断. 然后就是主要的暴力枚举部分,每次有 12种状态转移的选择,开始时用dfs,但爆栈了.然后改成bfs,又各种TLE.然后就是不断地优化优化. 判重的状态可以用一个

hdu 1312 Red And Black

hdu 1312 的传送门 Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red ti