LightOJ 1055 - Going Together

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\n", cases);
    else
        printf("Case %d: %d\n", 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;
}

转载于:https://www.cnblogs.com/aiterator/p/6891112.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值