zoukankan      html  css  js  c++  java
  • TOJ 3973 Maze Again && TOJ 3128 简单版贪吃蛇

    TOJ3973传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3973

    时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
    总提交: 198            测试通过:52

    描述

    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

    思路:入门广搜,从S出发到T,如果找得到路径输出字典序最小的那条,找不到则输出-1,可以在结构体内封装一个string属性,在向四个方向搜索的时候,加上对应字的母即可。本题有个小细节是,需要输出的是字典序最小的路径,这点区别于TOJ3128(简单版贪吃蛇),可以在写方向数组的时候,按四个方向的字典序从小到大写,int go[4][2] = {1,0,0,-1,0,1,-1,0}; 这样就可以在找到T的时候直接输出字符串。

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<set>
    #include<map> 
    #include<vector>
    #define LL long long
    #include<assert.h>
    using namespace std;
    int go[4][2] = {1,0,0,-1,0,1,-1,0};
    char ma[100][100];
    int vis[100][100];
    struct note{
        int x,y;
        string s;
    }pos,q;
    int n;
    int check(int x,int y){
        if(x < 0 ||x >= n || y < 0 || y >= n)return 0;
        return 1;
    }
    void bfs(int x,int y,int ex,int ey){
        string str[1000];
        int ans = 0;
        vis[x][y] = 1;
        queue<note>que;
        pos.s = "";
        pos.x = x;
        pos.y = y;
        que.push(pos);
        while(que.size()){
            q = que.front();
            que.pop();
            if(q.x == ex && q.y == ey){
                cout<<q.s<<endl;return;
            }
            for(int i = 0 ; i < 4 ; i++){
                int dx = q.x + go[i][0];
                int dy = q.y + go[i][1];
                if(check(dx,dy) && !vis[dx][dy] && ma[dx][dy] != '#'){
                    //cout<<q.s<<endl;
                    pos.x = dx;
                    pos.y = dy;
                    if(i == 0) pos.s = q.s + "D";
                    if(i == 1) pos.s = q.s + "L";
                    if(i == 2) pos.s = q.s + "R";
                    if(i == 3) pos.s = q.s + "U";
                    que.push(pos);
                    vis[dx][dy] = 1;
                }
            }
        }
        puts("-1");
        return;
    }
    int main(){
        int t;
        for(scanf("%d",&t);t--;){
            int sx,sy,ex,ey;
            memset(vis,0,sizeof(vis));
            scanf("%d",&n);
            for(int i = 0 ; i < n ; i++){
                scanf("%s",ma[i]);
                for(int j = 0 ; j < n ;j ++){
                    if(ma[i][j] == 'S'){
                        sx = i;sy = j;
                    }
                    if(ma[i][j] == 'T'){
                        ex = i;ey = j;
                    }
                }
            }
            bfs(sx,sy,ex,ey);
        }
    }
    View Code

    TOJ3128传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3128 

    时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
    总提交: 678            测试通过:202 Special Judge

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

    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

    思路:这题也是广搜的入门题,也是对S开始进行广度优先搜素。因为是特判题,所以不需要考虑字典序,注意找不到的时候输出字符串"Can't eat it!"即可。

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<set>
    #include<map> 
    #include<vector>
    #define LL long long
    #include<assert.h>
    using namespace std;
    int go[4][3]={{0,1},{0,-1},{-1,0},{1,0}};
    char ma[100][100];
    int vis[100][100];
    struct note{
        int x,y;
        string s;
    }pos,q;
    int n,m;
    int check(int x,int y){
        if(x < 0 ||x >= n || y < 0 || y >= m)return 0;
        return 1;
    }
    void bfs(int x,int y,int ex,int ey){
        vis[x][y] = 1;
        queue<note>que;
        pos.s = "";
        pos.x = x;
        pos.y = y;
        que.push(pos);
        while(que.size()){
            q = que.front();
            que.pop();
            if(q.x == ex && q.y == ey){
                cout<<q.s<<endl;
                return;
            }
            for(int i = 0 ; i < 4 ; i++){
                int dx = q.x + go[i][0];
                int dy = q.y + go[i][1];
                if(check(dx,dy) && !vis[dx][dy] && ma[dx][dy] != '#'){
                    //cout<<q.s<<endl;
                    pos.x = dx;
                    pos.y = dy;
                    if(i == 0) pos.s = q.s + "E";
                    if(i == 1) pos.s = q.s + "W";
                    if(i == 2) pos.s = q.s + "N";
                    if(i == 3) pos.s = q.s + "S";
                    que.push(pos);
                    vis[dx][dy] = 1;
                }
            }
        }
        puts("Can't eat it!");
        return;
    }
    int main(){
        int t;
        while(~scanf("%d %d",&n,&m)){
            int sx,sy,ex,ey;
            memset(vis,0,sizeof(vis));
            for(int i = 0 ; i < n ; i++){
                scanf("%s",ma[i]);
                for(int j = 0 ; j < m ;j ++){
                    if(ma[i][j] == 'S'){
                        sx = i;sy = j;
                    }
                    if(ma[i][j] == 'E'){
                        ex = i;ey = j;
                    }
                }
            }
            bfs(sx,sy,ex,ey);
        }
    }
    View Code
  • 相关阅读:
    Everything 本地磁盘文件搜索工具下载!
    Everything 本地磁盘文件搜索工具下载!
    Everything 本地磁盘文件搜索工具下载!
    4.shell基本操作简介
    4.shell基本操作简介
    4.shell基本操作简介
    4.shell基本操作简介
    apache、nginx配置自签名证书
    hadoop学习笔记(九):MapReduce程序的编写
    手动部署 Ceph Mimic 三节点
  • 原文地址:https://www.cnblogs.com/Esquecer/p/8440534.html
Copyright © 2011-2022 走看看