首页 > 其他分享 >HDU 3713 Double Maze

HDU 3713 Double Maze

时间:2022-11-09 22:34:46浏览次数:45  
标签:24 HDU x1 3713 Double x2 18 y1 y2



Problem Description Unlike single maze, double maze requires a common sequence of commands to solve both mazes. See the figure below for a quick understanding.

HDU 3713	Double Maze_#include


A maze is made up of 6*6 cells. A cell can be either a hole or a square. Moreover, a cell may be surrounded by barriers. There is ONLY one start cell (with a ball) and ONLY one end cell (with a star) in a single maze.These two cells are both squares. It is possible that the start cell and the end cell are the same one. The goal of a single maze is to move the ball from the start cell to the end cell. There are four commands in total,'L', 'D', 'R' and 'U' corresponding to moving the ball left, down, right and up one cell, respectively. The barriers may make the commands take no effect, i.e., the ball does NOT move if there is a barrier on the way.
When the ball gets to a hole or outside of the maze, it fails. A double maze is made up of two single mazes. The commands control two balls simultaneously, and the movements of two balls are according to the rules described above independently. Both balls will continue to move simultaneously if at least one of the balls has not got to the end cell.
So, a ball may move out of the end cell since the other ball has not been to the target. A double maze passes when both balls get to their end cells, or fails if either of the two mazes fails. The goal of double maze is to get the shortest sequence of commands to pass. If there are multiple solutions, get the lexical minimum one.
To simplify the input, a cell is encoded to an integer as follows. The lowest 4 bits signal the existence of the barriers around a cell. The fifth bit indicates whether a cell is a hole or not. The sixth and seventh bits are set for the start cell and end cell. Details are listed in the following table with bits counted from lowest bit. For a barrier, both of the two adjacent cells will have the corresponding barrier bit set. Note that the first two mazes in the sample input is the encoding of two mazes in the figure above, make sure you understand the encoding right.
HDU 3713	Double Maze_i++_02
 
Input The first line of input gives the total number of mazes, T (1 < T ≤ 20). Then follow T mazes. Each maze is a 6*6 matrix, representing the encoding of the original maze. There is a blank line between mazes.  
Output For every two consecutive mazes, you should treat them as a double maze and output the answer. So there are actually T - 1 answers. For each double maze, output the shortest sequence of commands to pass. If there are multiple solutions, output the lexicographically minimum one. If there is no way to pass, output -1 instead.  
Sample Input 3 16 0 18 16 18 24 20 19 24 16 28 1 18 28 17 0 22 17 25 20 17 18 88 20 2 16 48 28 17 16 24 16 16 20 23 1 16 0 18 16 18 24 20 19 24 20 29 1 18 28 17 16 22 17 8 20 1 18 24 20 19 80 48 24 16 0 24 16 16 16 22 19 18 16 18 16 18 80 24 18 24 16 24 18 18 24 0 0 18 24 24 18 0 0 24 18 18 24 18 16 18 24 56 18 24 18 24 18  
Sample Output RRLULLLRRDLU RURDRLLLURDULURRRRRDDU   给出地图,找最短路,并且字典序最小,广搜,注意四个方向的顺序。
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,j,k) for (int i = j; i <= k; i++)
const int N = 6;
int n, t;
int f[N][N][N][N]; 
int D[4][2] = { {1,0},{0,-1},{0,1},{-1,0} };
char s[5] = { "DLRU" };

struct maze
{
    int a[N][N];
    int bx, by, ex, ey;
    int d[N][N][4];
    void read()
    {
        rep(i, 0, 5) rep(j, 0, 5)
        {
            scanf("%d", &a[i][j]);
            rep(k, 0, 1) d[i][j][k ^ 1] = a[i][j] & (1 << k);
            rep(k, 2, 3) d[i][j][k] = a[i][j] & (1 << k);
            if (a[i][j] & (1 << 5)) bx = i, by = j;
            if (a[i][j] & (1 << 6)) ex = i, ey = j;
            a[i][j] = a[i][j] & (1 << 4);
        }
    }
    bool check(int x, int y) { return ex == x&&y == ey; }
    bool move(int t, int x, int y, int &xx, int &yy)
    {
        if (d[x][y][t]) { xx = x; yy = y; return true; }
        int X = x + D[t][0], Y = y + D[t][1];
        if (X < 0 || X>5 || Y < 0 || Y>5 || !a[X][Y]) return false;
        xx = X;    yy = Y; return true;
    }
}mp[2];

struct point
{
    int x1, y1, x2, y2;
    point(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0) :x1(x1), y1(y1), x2(x2), y2(y2) {};
}pre[N][N][N][N];

void putout(int x1, int y1, int x2, int y2)
{
    if (f[x1][y1][x2][y2] == -2) return;
    point q = pre[x1][y1][x2][y2];
    putout(q.x1, q.y1, q.x2, q.y2);
    printf("%c", s[f[x1][y1][x2][y2]]);
}

bool bfs(maze &a, maze &b)
{
    queue<point> p;    
    p.push(point(a.bx, a.by, b.bx, b.by));
    f[a.bx][a.by][b.bx][b.by] = -2;
    while (!p.empty())
    {
        point q = p.front(); p.pop();
        if (a.check(q.x1, q.y1) && b.check(q.x2, q.y2))
        {
            putout(q.x1, q.y1, q.x2, q.y2); 
            putchar(10);       return true;
        }
        int x1, y1, x2, y2;
        rep(i, 0, 3)
        {
            if (!a.move(i, q.x1, q.y1, x1, y1)) continue;
            if (!b.move(i, q.x2, q.y2, x2, y2)) continue;
            if (f[x1][y1][x2][y2] != -1) continue;
            f[x1][y1][x2][y2] = i;
            pre[x1][y1][x2][y2] = q;
            p.push(point(x1, y1, x2, y2));
        }
    }
    return false;
}

int main()
{
    scanf("%d", &n);
    for (mp[t = 0].read(); --n; t ^= 1)
    {
        memset(f, -1, sizeof(f)); mp[t ^ 1].read();
        if (!bfs(mp[t], mp[t ^ 1])) printf("-1\n");
    }
    return 0;
}

标签:24,HDU,x1,3713,Double,x2,18,y1,y2
From: https://blog.51cto.com/u_15870896/5838890

相关文章

  • HDU 3442 Three Kingdoms
    ProblemDescriptionThreeKingdomsisafunnygame.OftenLiuBeiisweakandhastorunaway,sointhegameLiuBeihasaskillcalled"Dunzou".Thist......
  • HDU 4127 Flood-it!
    ProblemDescriptionFlood-itisafascinatingpuzzlegameonGoogle+platform.Thegameinterfaceislikefollows:Atthebeginningofthegame,sys......
  • HDU 2757 Ocean Currents
    ProblemDescriptionForaboatonalargebodyofwater,strongcurrentscanbedangerous,butwithcarefulplanning,theycanbeharnessedtohelpthe......
  • HDU 1252 Hike on a Graph
    ProblemDescription"HikeonaGraph"isagamethatisplayedonaboardonwhichanundirectedgraphisdrawn.Thegraphiscompleteandhasallloops......
  • HDU 3345 War Chess
    ProblemDescriptionWarchessishh'sfavoritegame:Inthisgame,thereisanN*Mbattlemap,andeveryplayerhashisownMovingVal(MV).Ineach......
  • HDU 2660 Accepted Necklace
    ProblemDescriptionIhaveNpreciousstones,andplantouseKofthemtomakeanecklaceformymother,butshewon'tacceptanecklacewhichistooh......
  • HDU 1828 Picture
    ProblemDescriptionAnumberofrectangularposters,photographsandotherpicturesofthesameshapearepastedonawall.Theirsidesareallvertical......
  • HDU 3695 Computer Virus on Planet Pandora
    ProblemDescription    AliensonplanetPandoraalsowritecomputerprogramslikeus.Theirprogramsonlyconsistofcapitalletters(‘A’to‘Z’......
  • HDU 5379 Mahjong tree
    ProblemDescriptionLittlesunisanartist.Todayheisplayingmahjongalone.Hesuddenlyfeelsthatthetreeintheyarddoesn'tlookgood.Sohewant......
  • HDU 3397 Sequence operation
    ProblemDescriptionlxhgwwgotasequencecontainsncharacterswhichareall'0'sor'1's.Wehavefiveoperationshere:Changeoperations:0ab......