HDU 1072 Nightmare

初次编辑时间:2011-07-03

source page: http://acm.hdu.edu.cn/showproblem.php?pid=1072

Nightmare

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8011    Accepted Submission(s): 3855


Problem Description
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.

Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.

Here are some rules:
1. We can assume the labyrinth is a 2 array.
2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
There are five integers which indicate the different type of area in the labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius' start position, Ignatius starts his escape from this position.
3: The exit of the labyrinth, Ignatius' target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.

Output
For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.

Sample Input
  
  
3 3 3 2 1 1 1 1 0 1 1 3 4 8 2 1 1 0 1 1 1 0 1 0 4 1 1 0 4 1 1 0 0 0 0 0 0 1 1 1 1 4 1 1 1 3 5 8 1 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 4 1 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1

Sample Output
  
  
4 -1 13

source code:

两种方法:

题目很普通,就是比平常的BFS题多了一个时间重置点,对此只要另开一个数组,记录到达当前点的剩余时间,大于走这个点,反之不走。


先是数组模拟:

#include<stdio.h>
#include<string.h>
struct node{
    int x;
    int y;
    int time;
    int step;
 }q[1000],p;//定义队列q和中间变量p
int n,m,si,sj,T,front,tail,rear,step;
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
int mark[10][10],map[10][10];
void BFS()
{
    int i,j;
    //第一个点入队
    q[0].x=si;
    q[0].y=sj;
    q[0].time=T;
    q[0].step=0;
    mark[si][sj]=6;
    front=0; tail=0; rear=0;
    while(front<=rear){//队首指针不等于队尾指针
        for(j=0;j<4;j++){//扩展队列中的每一个新加入的点
            p.x=q[front].x+dx[j];
            p.y=q[front].y+dy[j];
            p.time=q[front].time-1;
            p.step=q[front].step+1;
            if(map[p.x][p.y]==3&&p.time>0){
                step=p.step;
                return ;
            }
            if(map[p.x][p.y]==4&&p.time>0){
                //mark[p.x][p.y]=6; //这行让我很蛋疼,刚开始一直错,检查了好几个小时才发现它是多余的
                p.time=6;
            }

            if(p.x>=0&&p.y>=0&&p.x<n&&p.y<m&&map[p.x][p.y]!=0&&p.time>mark[p.x][p.y]){
                //符合条件就入队,即添加到队尾
                rear++;
                q[rear].x=p.x;
                q[rear].y=p.y;
                q[rear].time=p.time;
                q[rear].step=p.step;
                mark[p.x][p.y]=p.time;
                //printf("%d %d %d %d\n",q[rear].x,q[rear].y,q[rear].time,q[rear].step);
            }
        }
        front++;
    }
}

int main(){
    int i,j,ca;
    scanf("%d",&ca);
    while(ca--){
        scanf("%d%d",&n,&m);
        memset(mark,0,sizeof(mark));
        for(i=0;i<n;i++)
            for(j=0;j<m;j++){
                scanf("%d",&map[i][j]);
                if(map[i][j]==2){
                    si=i;sj=j;
                }
            }

        T=6; step=-1;
        BFS();
        /*
        printf("\n");
        for(i=0;i<n;i++){
            for(j=0;j<m;j++)
                printf("%d ",mark[i][j]);
            printf("\n");
        }
        printf("\n");
        */
        printf("%d\n",step);
    }
    return 0;
}


再是STL中的队列:
不知为什么用C写竟然错了,C++就没错,看来已有运用STL函数还是用C++比较安全

#include<iostream>
#include<queue> //包含的头文件
using namespace std;
struct Node
{
    int x,y;
    int time;
    int step;
}p,q;
int map[9][9];
int mark[9][9];
int m,n,mins,si,sj;
int dir[4][2]={-1,0,1,0,0,-1,0,1};
void bfs()
{
    int k;
    queue<Node>Q;//建立node类型的队列,队列名为Q
    p.step=0;
    p.time=6;
    p.x=si;
    p.y=sj;
    Q.push(p);//p入队
    mark[si][sj]=p.time;
    while(!Q.empty()){//若队列非空
        q=Q.front();//取队首元素
        Q.pop(); //清除第一个元素
        下面是另外的含义

        //Q.back(); 显示最后一个元素
        //Q.size();输出现有元素个数
        if(map[q.x][q.y]==3&&q.time>0){
                mins=q.step;
                return ;
            }
        if(map[q.x][q.y]==4)
            q.time=6;
        for(k=0;k<4;k++){
            p.x=q.x+dir[k][0];
            p.y=q.y+dir[k][1];
            p.time=q.time-1;
            p.step=q.step+1;
            if(p.x>=0&&p.x<m&&p.y>=0&&p.y<n&&map[p.x][p.y]!=0&&q.time>0&&mark[p.x][p.y]<p.time){
                //符合条件则入队
                mark[p.x][p.y]=p.time;
                Q.push(p);//从已有元素后增加元素
            }
        }
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){
        cin>>m>>n;
        int i,j;
        for(i=0;i<m;i++){
            for(j=0;j<n;j++){
                cin>>map[i][j];
                if(map[i][j]==2){
                    si=i;
                    sj=j;
                }
                mark[i][j]=0;
            }
        }
        mins=-1;
        bfs();
        cout<<mins<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本项目是一个基于SpringBoot开发的华府便利店信息管理系统,使用了Vue和MySQL作为前端框架和数据库。该系统主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的Java学习者,包含项目源码、数据库脚本、项目说明等,有论文参考,可以直接作为毕设使用。 后台框架采用SpringBoot,数据库使用MySQL,开发环境为JDK、IDEA、Tomcat。项目经过严格调试,确保可以运行。如果基础还行,可以在代码基础之上进行改动以实现更多功能。 该系统的功能主要包括商品管理、订单管理、用户管理等模块。在商品管理模块中,可以添加、修改、删除商品信息;在订单管理模块中,可以查看订单详情、处理订单状态;在用户管理模块中,可以注册、登录、修改个人信息等。此外,系统还提供了数据统计功能,可以对销售数据进行统计和分析。 技术实现方面,前端采用Vue框架进行开发,后端使用SpringBoot框架搭建服务端应用。数据库采用MySQL进行数据存储和管理。整个系统通过前后端分离的方式实现,提高了系统的可维护性和可扩展性。同时,系统还采用了一些流行的技术和工具,如MyBatis、JPA等进行数据访问和操作,以及Maven进行项目管理和构建。 总之,本系统是一个基于SpringBoot开发的华府便利店信息管理系统,使用了Vue和MySQL作为前端框架和数据库。系统经过严格调试,确保可以运行。如果基础还行,可以在代码基础之上进行改动以实现更多功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值