zoukankan      html  css  js  c++  java
  • LightOJ 1055

    You are playing a computer game in which three robots (Aneed, Ben and Cindy) are trapped in a labyrinth. Initially all three are situated in three different locations in the maze. There are three outlets through which the robots have to exit. As expected, there are several obstacles in the maze and the robots can't go through them.

    The maze can be modeled as a square grid with N x N cells. The robots are placed on three different cells into the maze. You can command them to move. A single command will be activated for the three robots simultaneously. A robot will move to a new position (whatever its current situation is) if it is an empty cell within the maze or it is one of the free target cells, otherwise the command will be ignored for that robot. Your task is to command them such a way that all of them are on three exit cells (in any order).

    image

    A move consists of one of the following (Each move takes 1 unit of time):

    Move North The robots move one cell north.
    Move East The robots move one cell east.
    Move South The robots move one cell south.
    Move West The robots move one cell west.

    Each cell consists of one of the following characters:

    A - Initial position of Aneed
    B - Initial position of Ben
    C - Initial position of Cindy
    . - An empty cell
    # - An obstacle
    X - A target cell
    

    You can assume that for every maze each of the letters (A B C) will appear exactly once and the letter X will appear exactly three times.

    Input

    Input starts with an integer T (≤ 50), denoting the number of test cases.

    Each case starts with an integer N (2 < N < 10). Each of the next N lines contains N characters that fill up the maze.

    Output

    For each case, output the case number followed by the minimum time required. If it is impossible to move them as described, print 'trapped' instead of the time.

    Sample Input

    3
    7
    .....#.
    .......
    .#B....
    ...A.#.
    .CX....
    .X.X.#.
    .#.....
    3
    ABC
    ...
    XXX
    3
    ABC
    ###
    XXX
    

    Output for Sample Input

    Case 1: 2
    Case 2: 2
    Case 3: trapped
    

    题目大意就是说有三个小孩分别在"A","B","C"三个位置,现在他们全部都走到"X"的位置,求最小步数。

    暴搜, 然后用一个6维数组标明状态(也可以用一个6位长度字符串标明)

    代码如下:

    #include<bits/stdc++.h>
    using namespace std;
    
    char mp[15][15];
    int n;
    bool visited[11][11][11][11][11][11];
    int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
    
    struct node
    {
        pair<int, int> a, b, c;
        int step;
        node(pair<int, int>&x, pair<int,int> &y, pair<int, int> &z, int s)
        {
            a = x, b = y, c = z;
            step = s;
        }
    };
    
    queue<node> que;
    
    bool judge(pair<int, int> &res)
    {
        if(res.first >= 0 && res.first < n && res.second >= 0 && res.second < n && mp[res.first][res.second] != '#')
            return false;
        return true;
    }
    
    int bfs(pair<int, int> &a, pair<int,int> &b, pair<int, int> &c)
    {
        memset(visited, false, sizeof(visited));
        visited[a.first][a.second][b.first][b.second][c.first][c.second] = true;
        while(!que.empty())
            que.pop();
    
        que.push(node(a, b, c, 0));
        while(!que.empty())
        {
            node f = que.front();
            que.pop();
    
            if(mp[f.a.first][f.a.second] == 'X' && mp[f.b.first][f.b.second] == 'X' && mp[f.c.first][f.c.second] == 'X')
                return f.step;
    
            for(int i=0; i<4; ++ i)
            {
                pair<int, int> x, y, z;
                x = make_pair(f.a.first + dir[i][0], f.a.second + dir[i][1]);
                y = make_pair(f.b.first + dir[i][0], f.b.second + dir[i][1]);
                z = make_pair(f.c.first + dir[i][0], f.c.second + dir[i][1]);
    
                if(judge(x))
                    x = f.a;
                if(judge(y))
                    y = f.b;
                if(judge(z))
                    z = f.c;
    
                for(int j=3; j>=0; -- j)
                {
                    if(x == y || x == z)
                        x = f.a;
                    if(y == x || y == z)
                        y = f.b;
                    if(z == x || z == y)
                        z = f.c;
                }
    
                if(visited[x.first][x.second][y.first][y.second][z.first][z.second] == false)
                {
                    visited[x.first][x.second][y.first][y.second][z.first][z.second] = true;
                    que.push(node(x, y, z, f.step + 1));
                }
            }
        }
        return -1;
    
    }
    
    void solve(int cases)
    {
        scanf("%d", &n);
        pair<int,int> a, b, c;
        for(int i = 0; i < n; ++ i)
            for(int j = 0; j < n; ++ j)
            {
                scanf(" %c", &mp[i][j]);
                if(mp[i][j] == 'A')
                    a.first = i, a.second = j;
                else if(mp[i][j] == 'B')
                    b.first = i, b.second = j;
                else if(mp[i][j] == 'C')
                    c.first = i, c.second = j;
            }
    
        int ans = bfs(a, b, c);
        if(ans == -1)
            printf("Case %d: trapped
    ", cases);
        else
            printf("Case %d: %d
    ", cases, ans);
    }
    
    int main()
    {
        //freopen("out.txt", "w", stdout);
        int t;
        scanf("%d", &t);
        for(int i=1; i<=t; ++ i)
            solve(i);
        return 0;
    }
    
    
    
  • 相关阅读:
    视频编码之释——从H.261 到H.264
    bitmap图像介绍
    用搜索引擎搜索我的名字 @_@
    blog标题由来
    ORACLE双机热备安装及物理迁移 for win2000
    审核再次失败
    asp.net学习历程
    痛并快乐着
    开心,blog点击率超过1000
    XP下ASP.NET不能访问ORACLE数据库的解决方案
  • 原文地址:https://www.cnblogs.com/aiterator/p/6891112.html
Copyright © 2011-2022 走看看