HDU1072:Nightmare

点击打开题目链接

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 9572    Accepted Submission(s): 3001


Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
 

Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
 

Output
In this problem, you have to output the translation of the history book.
 

Sample Input
        
        
        
        
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
 

Sample Output
        
        
        
        
hello, i'm from mars. i like earth!
Hint
Huge input, scanf is recommended.
 

Author
Ignatius.L
 


=====================================题目大意=====================================


Ignatius昨晚做了个噩梦,他梦见自己需要从一个有着定时炸弹的迷宫里在炸弹爆炸前逃脱。

1、炸弹爆炸剩余时间初始为6分钟。

2、Ignatius每分钟只能从当前位置(x,y)移动到(x+1,y),(x-1,y),(x,y+1),(x,y-1)中的某个位置,当然他不能走出边境也不能走

   到墙上。

3、迷宫的一些地区有着可以将炸弹爆炸剩余时间重置为6分钟的炸弹复位装置,一个炸弹复位装置可以被多次使用,使用时间可以忽

   略不计。

4、当炸弹爆炸剩余时间为零时,即使Ignatius正好在出口位置他也无法逃脱,即使Ignatius正好得到炸弹复位装置他也无法使用。

5、输入的数字地图中:0代表墙壁,1代表空地,2代表起点,3代表出口,4代表炸弹复位装置。

输出Ignatius逃出迷宫所需的最短时间,如果Ignatius无法逃出迷宫则输出-1。


=====================================算法分析=====================================


BFS:BFS算法是一种扩展式的搜索算法,而“扩展”出的节点都是在扩展指标最优化的访问下得到的。

也就是说已经访问过的节点对于扩展指标而言不存在更优的访问方案,那么已经访问过的节点自然是不需要重复访问了。

BFS的扩展指标只有一个,比如说BFS求最短步数中:扩展的指标就是步数,最优化就是指步数最短。

但是本题搜索的扩展指标显然有两个:逃窜累计时间(TCNT)与爆炸剩余时间(TRES)。

并且这两个指标相互独立:不能说最优化就是指TCNT最短,也不能说最优化就是指TRES最长。

不过考虑到题目所求的是出口的最短TCNT,也就是说对于出口这一个节点而言,最优化就是指TCNT最短。因此BFS的扩展指标可以选

择TCNT

而对于扩展得到的非出口节点,由于仅以TCNT为指标扩展不能保证它们在综合指标上最优。

因此若新的访问方案使得它们的TRES指标更优,新的访问方法便“可能”是更优访问方案。

那么自然就需要对节点通过对于这一“可能的”更优访问方案重新访问。

简而言之,将一般BFS的扩展条件:if(节点未被访问过)改为:if(节点未被访问过||新的访问方案使得节点的TRES指标

更优)即可。


=======================================代码=======================================




#include<queue>
#include<stdio.h>
#include<string.h>

using namespace std;

#define TYPE(C) map[C.x][C.y].type                     //地图上坐标C的类型
#define TCNT(C) map[C.x][C.y].tcnt                     //地图上坐标C的累计时间
#define TRES(C) map[C.x][C.y].tres                     //地图上坐标C的剩余时间

int T,N,M;

struct cord { int x,y; }Ori;

struct area { int type,tcnt,tres; }map[10][10];

int BFS()
{
    static const int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
    queue<cord>q;
    for(q.push(Ori);!q.empty();q.pop())
    {
        int n;
        struct cord cur=q.front();
        for(n=0;n<4;++n)
        {
            struct cord tmp=cur;
            tmp.x+=dir[n][0];
            tmp.y+=dir[n][1];
            //TRES(cur)-1>TRES(tmp):新的访问方案使得以TRES为指标时tmp更优化
            if(TYPE(tmp)>0&&(TCNT(tmp)==-1||TRES(cur)-1>TRES(tmp)))
            {
                TCNT(tmp)=TCNT(cur)+1;
                TRES(tmp)=TRES(cur)-1;
                if(TYPE(tmp)==3) return TCNT(tmp);     //找到出口
                if(TYPE(tmp)==4) TRES(tmp)=6;          //重置炸弹
                if(TRES(tmp)>=2) q.push(tmp);          //TRES<2的状态应忽略
            }
        }
    }
    return -1;
}

void initdate()
{
    memset(map,-1,sizeof(map));                        //注意地图类型的哨兵为-1
    int i,j;
    for(i=1;i<=N;++i)
    {
        for(j=1;j<=M;++j)
        {
            scanf("%d",&map[i][j].type);
            if(map[i][j].type==2)
            {
                Ori.x=i; Ori.y=j;
                map[i][j].tcnt=0;
                map[i][j].tres=6;
            }
        }
    }
}

int main()
{
    while(scanf("%d",&T)==1) while(T--)
    {
        scanf("%d%d",&N,&M);
        initdate();
        printf("%d\n",BFS());
    }
    return 0;
}

你可能感兴趣的:(搜索,bfs)