ACM 广搜 贪吃蛇&&Maze

额最近做的好像都是广搜也...

贪吃蛇是我很早以前就接触,现在才能看懂的..十分尴尬,

然后这两题类型一样,我就写一起啦~

都是四个方向,然后把走的方向存下来最后一起输出。

 

TOJ 3128 简单版贪吃蛇

 

描述

 

现在我们来简化蛇的身体,假设初始化的时候蛇的身体只有一个头而已(呵,当然是假设的),那么蛇去吃食物的时候就不必考虑碰到自己的身体了。
例:

5 5
.....
S....
###.#
E....
#####

 

那么从S到E最短的走法是EEESSWWW。说明:N(north),S(south),W(west),E(east)。如果吃不到食物就输出Can't eat it!
注意:路径是最短的走的。

 

输入

 

输入数据有多组,每组输入的第一行是两个正整数R,C,表示行和列,3=<R,C<=100,下面输入R行C列的矩阵。

输入保证合法。

 

输出

 

每行输出最短的走法。

 

样例输入

5 5 ..... S.... ###.# E.... #####

样例输出

EEESSWWW

 

#include <cstring>
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
int n,m,dir[4][2]= {-1,0,1,0,0,-1,0,1};
int vis[100][100],ex,ey,sx,sy,flag;
char map[100][100];
struct node
{
    int x;
    int y;
    char c[101];
};
void bfs(int x,int y)
{
    node a,p,b;
    int i;
    vis[x][y]=1;
    a.x=x;
    a.y=y;
    memset(a.c,'\0',sizeof(a.c));
    queue<node>Q;
    Q.push(a);
    while(!Q.empty())
    {
        b=Q.front();
        Q.pop();
        if(map[b.x][b.y]=='E')
        {
            flag=1;
            printf("%s\n",b.c);
            return;
        }
        for(i=0;i<4;i++)
        {
            p=b;
            p.x=p.x+dir[i][0];
            p.y=p.y+dir[i][1];
            if(map[p.x][p.y]!='#'&&p.x>=0&&p.y>=0&&p.x<n&&p.y<m&&vis[p.x][p.y]==0)
            {
                vis[p.x][p.y]=1;
                if(i==0) strcat(p.c,"N");
                if(i==1) strcat(p.c,"S");
                if(i==2) strcat(p.c,"W");
                if(i==3) strcat(p.c,"E");
                Q.push(p);
            }
        }
    }
}
int main()
{
    int i,j;
    while(cin>>n>>m)
    {
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='S')
                sx=i, sy=j;
            }
        flag=0;
        bfs(sx,sy);
        if(flag==0)
        printf("Can't eat it!\n");
    }
    return 0;
}

 

TOJ 3973 Maze Again

 

描述

 

The maze is the same as problem D, and I strongly recommend you solve the previous one first because it.s easier than this.

This time, we want you design the command for our poor robot to move from where it is to its destination. The command sequence.s length should be the shortest. If several solutions exist, find the lexicographically minimum one.

Lexicographical sequence is the order in one dictionary. For example, “cat” is less than “do”, and “do” is less than “dog”.

 

输入

 

The first line contains a single integer T, indicating the number of test cases.

Each test case begins with one integer N (1 <= N <= 50), indicating the size of the maze. The followed N lines are N strings whose length is also N, indicating the maze.

 

输出

 

For each case, output a command string if there is a solution, otherwise output -1.

 

样例输入

2 2 S. #T 4 S#.. .#T. .##. ....

样例输出

RD DDDRRRUUL

题目意思和贪吃蛇差不多,但是这题要注意DLRU要按字典序,所以一开始的dir要写好。

 

#include <cstring>
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
int n,dir[4][2]= {1,0,0,-1,0,1,-1,0};//字典序
int vis[51][51],ex,ey,sx,sy,flag;
char map[51][51];
struct node
{
    int x;
    int y;
    char c[101];
};
void bfs(int x,int y)
{
    node a,p,b;
    int i;
    vis[x][y]=1;
    a.x=x;
    a.y=y;
    memset(a.c,'\0',sizeof(a.c));
    queue<node>Q;
    Q.push(a);
    while(!Q.empty())
    {
        b=Q.front();
        Q.pop();
        if(map[b.x][b.y]=='T')
        {
            flag=1;
            printf("%s\n",b.c);
            return;
        }
        for(i=0;i<4;i++)
        {
            p=b;
            p.x=p.x+dir[i][0];
            p.y=p.y+dir[i][1];
            if(map[p.x][p.y]!='#'&&p.x>=0&&p.y>=0&&p.x<n&&p.y<n&&vis[p.x][p.y]==0)
            {
                vis[p.x][p.y]=1;
                if(i==0) strcat(p.c,"D");
                if(i==1) strcat(p.c,"L");
                if(i==2) strcat(p.c,"R");
                if(i==3) strcat(p.c,"U");
                Q.push(p);
            }
        }
    }
}
int main()
{
    int i,j,o;
    cin>>o;
    while(o--)
    {
		cin>>n;
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='S')
                sx=i,sy=j;
            }
        flag=0;
        bfs(sx,sy);
        if(flag==0)
        printf("-1\n");
    }
    return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值