hud oj 1813 Escape from Tetris

来源:互联网 发布:淘宝上的指纹锁靠谱吗 编辑:程序博客网 时间:2024/06/05 16:12
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


这道题我刚开始用的广搜,然后越写越感觉有很多没有考虑到后来就没有相信写下去了,看到网上的大佬写的bfs()+IDA        加油吧奋斗
参考代码
#include<cstdio>#include<string.h>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>#include <numeric>//常用数字操作 一般和algorithm搭配使用#include <functional>//STL定义运算函数(代替运算符)using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const int INF = 1000000000;const int maxn =1000008;char maze[10][10];struct node{    int x,y;};char ss[4][10]= {"east","north","south","west"};int to[4][2]= {0,1,-1,0,1,0,0,-1};int dis[10][10];int n,step,deep,path[1000],L;queue<node>q;int onedge(int i,int j){    return i==0||i==n-1||j==0||j==n-1;}int ismap(int i,int j){    return i>=0&&i<n&&j>=0&&j<n;}void bfs(){    int i;    node a,next;    while(!q.empty())    {        a=q.front();        q.pop();        for(i=0;i<4;i++)        {            next.x=a.x+to[i][0];            next.y=a.y+to[i][1];            if(!ismap(next.x,next.y)) continue;            if(!maze[next.x][next.y]) continue;            if(dis[next.x][next.y]>dis[a.x][a.y]+1)            {                dis[next.x][next.y]=dis[a.x][a.y]+1;                q.push(next);            }        }    }}void init(){    int i,j;    node a;    while(!q.empty())        q.pop();    for(i=0; i<n; i++)    {        for(j=0; j<n; j++)        {            dis[i][j]=INF;            maze[i][j]=maze[i][j]=='1'?0:1;            if(maze[i][j]&&onedge(i,j))            {                a.x=i;                a.y=j;                dis[i][j]=0;                q.push(a);            }        }    }    bfs();}int get_h(char mat[10][10]){    int i,j,maxnn=0;    for(i=0;i<n;i++)    {        for(j=0;j<n;j++)        {            if(mat[i][j])               maxnn=max(maxnn,dis[i][j]);        }    }    return maxnn;}int IDA(char mat[10][10] ,int step){    if(step+get_h(mat)>deep) return 0;    if(step==deep) return 1;    int i,x,y,tx,ty;    char tem[10][10];    for(i=0;i<4;i++)    {        memset(tem,0,sizeof(tem));        for(x=0;x<n;x++)        {            for(y=0;y<n;y++)            {                if(onedge(x,y)||!mat[x][y]) continue;                tx=x+to[i][0];                ty=y+to[i][1];                if(!maze[tx][ty]) tem[x][y]=1;                else tem[tx][ty]=1;            }        }        path[step]=i;        if(IDA(tem,step+1))            return 1;    }    return 0;}int main(){    int i,flag=0;    while(~scanf("%d",&n))    {        if(flag) printf("\n");        for(i=0; i<n; i++)            scanf("%s",maze[i]);        init();        deep=0;        while(1)        {            if(IDA(maze,0))                break;                deep++;        }        for(i=0;i<deep;i++)            printf("%s\n",ss[path[i]]);        flag++;    }    return 0;}


0 0
原创粉丝点击