#HDU1813#Escape from Tetris(IDA*_搜索)

来源:互联网 发布:linux tail f 退出 编辑:程序博客网 时间:2024/06/04 19:59

Escape from Tetris

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1430    Accepted Submission(s): 409

Problem Description
由于整日整夜地对着这个棋盘,Lele终于走火入魔。每天一睡觉,他就会梦到自己会被人被扔进一个棋盘中,一直找不到出路,然后从梦中惊醒。久而久之,Lele被搞得精神衰弱。梦境是否会成为现实,谁也说不准,不过不怕一万只怕万一。现在Lele每次看到一个棋盘,都会想象一下自己被关进去以后要如何逃生。

Lele碰到的棋盘都是正方形的,其中有些格子是坏的,不可以走,剩下的都是可以走的。只要一走到棋盘的边沿(最外面的一圈),就算已经逃脱了。Lele梦见自己一定会被扔在一个可以走的格子里,但是不确定具体是哪一个,所以他要做好被扔在任意一个格子的准备。

现在Lele请你帮忙,对于任意一个棋盘,找出一个最短的序列,序列里可以包括"north"(地图里向上),"east"(地图里向右),"south"(地图里向下),"west"(地图里向左),这四个方向命令。不论Lele被扔在棋盘里的哪个好的格子里,都能按这个序列行走逃出棋盘。
逃脱的具体方法是:不论Lele被扔在哪里,Lele按照序列里的方向命令一个一个地走,每个命令走一格,如果走的时候会碰到坏的格子,则忽略这条命令。当然,如果已经逃脱了,就可以不考虑序列中剩下的命令了。

Input
本题目包含多组测试,请处理至文件结束。
每组测试第一行包含一个正整数 N (0<N<9),代表棋盘的大小是 N*N
接下来有N行,每行N个字符代表这个棋盘。
其中0代表该位置是好的,可以走,1代表该位置是坏的,不可以走。

题目数据保证,对于任意一个棋盘,都存在题目中所要求的序列

Output
对于每组数据,输出题目所要求的序列,序列中每个元素一行。
如果存在两个符合要求的序列,请输出字典序最小的那个序列。

两个测试之间请用一个空行隔开。
 

Sample Input
41101000111001001


Sample Output
eastnorth

此题的特点在于H是可以直接计算出来的,而点数也不多,所以预先对每个内点BFS计算出它逃离的最小距离,作为H值。

然后IDA*搜索,记录当前已经走到什么地方了,能走的就继续走,不然就停在原地。

剪枝条件是H值中最大的那个是否和当前步数中已超出。

Code:

StatusAcceptedTime265msMemory1700kBLength2819LangG++Submitted2017-10-05 20:49:53Shared

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int INF = 0x3f3f3f3f;struct node{    int x, y, step;    node(){}    node(int a, int b, int c){x = a, y = b, step = c;}};struct POINT{    int x, y;    POINT(){}    POINT(int a, int b){x = a, y = b;}}P[50];bool flg;int N, cnt, lim;char S[10];int H[10][10], way[1000];int dd[4][2] = {{0, 1}, {-1, 0}, {1, 0}, {0, -1}};bool map[10][10], vis[10][10];bool Inarea(int x, int y){    if(x < 1 || y < 1 || x > N || y > N)    return 0;    return 1;}bool Check(int x, int y){    if(x == 1 || y == 1 || x == N || y == N)    return 1;    return 0;}queue<node>Q;int Bfs(int x, int y){    while(! Q.empty())  Q.pop();    memset(vis, 0, sizeof vis );    Q.push(node(x, y, 0));    vis[x][y] = 1;    while(! Q.empty()){        node tmp = Q.front();   Q.pop();        if(Check(tmp.x, tmp.y))   return tmp.step;        for(int i = 0; i < 4; ++ i){            int xx = tmp.x + dd[i][0];            int yy = tmp.y + dd[i][1];            if(Inarea(xx, yy) && ! vis[xx][yy] && ! map[xx][yy]){                vis[xx][yy] = 1;                Q.push(node(xx, yy, tmp.step + 1));            }        }    }    return INF;}int Get_H(POINT *p){    int rt = 0;    for(int i = 1; i <= cnt; ++ i)        rt = max(rt, H[p[i].x][p[i].y]);    return rt;}void Dfs(int now, POINT *p){    if(now + Get_H(p) > lim)   return ;    if(now == lim){flg = 1;   return ;}    POINT tmp[50];    for(int i = 0; i < 4; ++ i){        way[now] = i;        for(int j = 1; j <= cnt; ++ j){            int xx = p[j].x + dd[i][0];            int yy = p[j].y + dd[i][1];            if(Check(p[j].x, p[j].y) || map[xx][yy])                tmp[j] = p[j];            else tmp[j] = POINT(xx, yy);        }        Dfs(now + 1, tmp);        if(flg) return ;    }}int main(){    int t = 0;    while(~scanf("%d", &N)){        if(t ++)    putchar(10);        flg = 0, cnt = 0;        for(int i = 1; i <= N; ++ i){            scanf("%s", S + 1);            for(int j = 1; j <= N; ++ j)                map[i][j] = S[j] - 48;        }        for(int i = 1; i <= N; ++ i)            for(int j = 1; j <= N; ++ j)                if(! map[i][j]){                    H[i][j] = Bfs(i, j);                    if(H[i][j]) P[++ cnt] = POINT(i, j);                }                else H[i][j] = INF;        if(cnt == 0)    continue;        for(lim = 1; ; ++ lim){            Dfs(0, P);            if(flg) break;        }        for(int i = 0; i < lim; ++ i){            if(way[i] == 0) puts("east");            else if(way[i] == 1)    puts("north");            else if(way[i] == 2)    puts("south");            else puts("west");        }    }    return 0;}