xtuoj 1187 Double Maze (2014XTU校赛)

 昨天刚做了一道Double Maze,忽然想起ww大大在校赛上也出了到类似的,今天顺手水了一下,,半个小时敲好,结果因为漏了一个判断条件,,debug 一个多小时。。

题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1187

Double Maze

Accepted : 42 Submit : 83
Time Limit : 1000 MS Memory Limit : 65536 KB

题目描述

一个迷宫里,有两个点,你的任务是给出尽可能短的一系列的命令,让这两个点一起执行这些命令能走到一起。如果某个点按某个命令走会走出迷宫或走到障碍上,则忽略这条命令。

输入

输入不超过1000个样例,每个样例第一行有两个整数n,m(2≤n,m≤11),之后n行每行m个字符,'.'表示能走的地方,'#'表示障碍,‘*'表示其中一个点。每个样例之间有一个空行。

输出

每个样例输出一行,包含'U','D','L','R'。'U'表示向上走的命令,'D'表示向下,'L'表示向左,'R'表示向右。要是有多个答案,输出字典序最小的序列。如果无法达到目标,输出“Sorry”(引号不要输出)。

样例输入
4 11
*.##..#..#*
...#..#.#..
.##.#.#.##.
##..###....

4 4
.*..
.###
...*
....

10 10
*.........
#########.
..........
.#########
..........
#########.
..........
.#########
.........*
##########

样例输出
Sorry
LLLUU
LLLLLLLLLUURRRRRRRRRUULLLLLLLLLUURRRRRRRRRDD


Source

XTU OnlineJudge 


解题思路:和HDU的Double Maze 基本一样,也是用四维数组存状态,但放在一个迷宫中,比HDU的还是要简单些。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 15;

struct Point{
    int x, y;
    int xx, yy;
    int x1, y1, x2, y2;
}fr, next, start, prev[N][N][N][N];

char mp[N][N];
int vis[N][N][N][N];
int dir[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
char s[4] = {'D', 'L', 'R', 'U'};
int n, m;

void print(int x, int y, int xx, int yy){
    if(x == start.x && y == start.y && xx == start.xx && yy == start.yy)
        return;
    int tx = x, ty = y, txx = xx, tyy = yy;
    x = prev[tx][ty][txx][tyy].x1;
    y = prev[tx][ty][txx][tyy].y1;
    xx = prev[tx][ty][txx][tyy].x2;
    yy = prev[tx][ty][txx][tyy].y2;
    print(x, y, xx, yy);
    for(int i = 0; i < 4; i++){
        if((x + dir[i][0] == tx && y + dir[i][1] == ty) || (xx + dir[i][0] == txx && yy + dir[i][1] == tyy)){
            putchar(s[i]);
            break;
        }
    }
    return;
}

void bfs(){
    queue<Point> Q;
    Q.push(start);
    vis[start.x][start.y][start.xx][start.yy] = 1;
    while(!Q.empty()){
        fr = Q.front();
     // cout << fr.x << " " << fr.y << " " << fr.xx << " " << fr.yy << endl;
        Q.pop();
        if(fr.x == fr.xx && fr.y == fr.yy){
         // printf("%d %d %d %d\n", fr.x, fr.y, fr.xx, fr.yy);
            print(fr.x, fr.y, fr.xx, fr.yy);
            puts("");
            return;
        }
        for(int i = 0; i < 4; i++){
            int tx = fr.x + dir[i][0], ty = fr.y + dir[i][1];
            int txx = fr.xx + dir[i][0], tyy = fr.yy + dir[i][1];
         // cout << tx << " " << ty << " " << txx << " " << tyy << " " << i << endl;
            if(mp[tx][ty] == '.' || mp[tx][ty] == '*'){
                next.x = tx;
                next.y = ty;
            }
            else{
                next.x = fr.x;
                next.y = fr.y;
            }
            if(mp[txx][tyy] == '.' || mp[txx][tyy] == '*'){
                next.xx = txx;
                next.yy = tyy;
            }
            else{
                next.xx = fr.xx;
                next.yy = fr.yy;
            }
         // cout << next.x << " " << next.y << " " << next.xx << " " << next.yy << endl;
            if(!vis[next.x][next.y][next.xx][next.yy]){
                vis[next.x][next.y][next.xx][next.yy] = 1;
             // cout << next.x << " " << next.y << " " << next.xx << " " << next.yy << endl;
                Q.push(next);
                prev[next.x][next.y][next.xx][next.yy].x1 = fr.x;
                prev[next.x][next.y][next.xx][next.yy].y1 = fr.y;
                prev[next.x][next.y][next.xx][next.yy].x2 = fr.xx;
                prev[next.x][next.y][next.xx][next.yy].y2 = fr.yy;
            }
        }
    }
    puts("Sorry");
    return;
}

int main(){
    while(scanf("%d%d", &n, &m) != EOF){
        memset(mp, '\0', sizeof(mp));
        int flag = 0;
        for(int i = 1; i <= n; i++){
            scanf("%s", mp[i] + 1);
            for(int j = 1; j <= m; j++){
                if(mp[i][j] == '*'){
                    if(!flag){
                        start.x = i;
                        start.y = j;
                        flag = 1;
                    }
                    else{
                        start.xx = i;
                        start.yy = j;
                        break;
                    }
                }
            }

        }
        memset(vis, 0, sizeof(vis));
        bfs();
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值