1220.Look for homework

Description

Super scholar robs the all homework of others.The monitor decides to combat with the super scholar so as to help students to get back the homework.But super scholar lives in a castle because he doesn't want to be disturded.The outside of the castle is a maze with two dimension grids. Entering the castle must pass the maze.The monitor needs to save time because of accompanying his girlfriend,so he wants to make a student named Huachao Wei help him who hasn't a girlfriend.Now he has the map of the maze and needs to calculate the shortest path.  

Input

The input consists several cases.For each case starts with two integers n,m(2<=n,m<=10),symbolizing the length and width of the maze.The next n lines contain m numbers with no space and value for only 0 and 1.The essence is that 0 can pass ,1 can't pass. Now you are in the place of (1,1) refering to the top left corner.the export is in the place of (n,m).Each step can only walk with up,down,left and right.

Output

For each case,the first line prints the least number of steps to arrive the castle.The second line prints k characters for U,D,L,R,representing up,down,left,right.if there are many paths with the same length,please print the path with Minimum dictionary.It is guarantees that there must have a path for arriving the castle.

Sample Input

3 3
001
100
110
3 3
000
000
000

Sample Output

4
RDRD
4

DDRR

就是一个迷宫问题,然后找最短的路线,然后如果有多个最短路线就按字典序输出最靠前的。。当时做一脸**,后来想了想,让他一直按字典序查找直到出口的最短路线就是答案了。。。。就直接bfs(因为一些语法错误一直WA。。。细心,细心!)

#include<cstdio>
#include<cstring>
using namespace std;
long long a,b,n,m;
struct bbq
{
    int x,y,pre;
    char ch;
};
bbq p[105];
int vis[13][13];
int dir[4][2]= {{1,0},{0,-1},{0,1},{-1,0}};
char s[4]= {'D','L','R','U'};
int cmp(int x,int y)
{
    if(x<0||x>n-1||y<0||y>m-1||vis[x][y])
        return 0;
    return 1;
}
int serch(int x,int y)
{
    int xx,yy;
    a=0;
    b=1;


    p[a].x=x;
    p[a].y=y;
    p[a].pre=-1;
    vis[x][y]=1;
    while(a<b)
    {
        for(int i=0; i<=3; i++)
        {
            //printf("1");
            xx=p[a].x+dir[i][0];
            yy=p[a].y+dir[i][1];
            /*printf("x=%d y=%d ",p[a].x,p[a].y,a,b);
            printf("a=%d ",a);
            printf("b=%d\n",b);*/
            if(cmp(xx,yy))
            {


                vis[xx][yy]=1;
                p[b].x=xx;
                p[b].y=yy;
                p[b].pre=a;
                p[b].ch=s[i];
                b++;
            }
            if(xx==n-1&&yy==m-1)
            {
                return b-1;
            }
        }
        a++;
    }
}
char ans[100];
int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        int t,b,v=0;
        char ss[12][12];
        for(int i=0; i<n; i++)
        {
            getchar();
            scanf("%s",ss[i]);


            for(int j=0; j<m; j++)
            {
                if(ss[i][j]=='1')
                    vis[i][j]=1;
                else vis[i][j]=0;
            }
        }
        b=serch(0,0);
        while(p[b].pre!=-1)
        {
            ans[v]=p[b].ch;
            v++;
            b=p[b].pre;


        }
        printf("%d\n",v);
        for(int i=v-1;i>=0;i--)
        printf("%c",ans[i]);
        printf("\n");


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值