Uva 11624 - Fire!

Uva 11624 - Fire!
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.

There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word acm'' can be followed by the wordmotorola”. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.

Input Specification
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters ‘a’ through ‘z’ will appear in the word. The same word may appear several times in the list.

Output Specification
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.

If there exists such an ordering of plates, your program should print the sentence “Ordering is possible.“. Otherwise, output the sentence “The door cannot be opened.“.

Sample Input


3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok


Output for the Sample Input
The door cannot be opened.
Ordering is possible.
The door cannot be opened.


Joe进入一个迷宫,但不幸的是,迷宫里着火了。Joe一次可以向前后或左右走一个单位,火一次可以向前后左右同时扩散一个单位。问你,Joe能不能逃出迷宫?

用bfs,讲火和人同时压入队列中(先让火入队,再让Joe入队),火走过 的地方就把那个地方标记为’F’,Joe走到时候,如果遇到F,就不能往前走。如果Joe达到边界,就可以结束搜索了。不过有一点需要注意,那就是题目要求输出的是走出迷宫的时间,所以我们还需要加1,才能走出迷宫。


代码↓↓↓↓↓↓


 #include<iostream>
 #include <string.h>
 #include <cstdio>
 #include <queue>
using namespace std;
int R, C;
char a[1002][1002];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct Coor
{
    int x, y, state, step;
}joe, fire;

int bfs()
{
    queue q;
    Coor t;
    for (int i = 0; i < R; i++)
    {
        for (int j = 0; j < C; j++)
        {
            if (a[i][j] == 'F')
            {
                fire.x = i;
                fire.y = j;
                fire.state = 0;
                q.push(fire);
            }
        }
    }
    q.push(joe);
    while(!q.empty())
    {
        Coor now = q.front();
        q.pop(); 
        if (now.state)
        {
            if (now.x == 0 || now.x == R - 1 || now.y == 0 || now.y == C - 1)
            {
                return now.step + 1;
            }
            for (int i = 0; i < 4; i++)
            {
                t.x = now.x + dir[i][0];
                t.y = now.y + dir[i][1];
                if (t.x>= 0 && t.x < R && t.y >= 0 && t.y < C && a[t.x][t.y] == '.')
                {
                    a[t.x][t.y] = 'J';
                    t.state = 1;
                    t.step = now.step + 1;
                    q.push(t);
                }
            }
        }
        else
        {
            for (int i = 0; i < 4; i++)
            {
                t.x = now.x + dir[i][0];
                t.y = now.y + dir[i][1];
                if (t.x>= 0 && t.x < R && t.y >= 0 && t.y < C && (a[t.x][t.y] == '.' || a[t.x][t.y] == 'J'))
                {
                    a[t.x][t.y] = 'F';
                    t.state = 0;
                    q.push(t);
                }
            }
        }
    }
    return -1;
}


int main()
{
 #ifndef  ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
 #endif
    int t, i, j;
    cin >> t;
    char *p;
    while (t--)
    {
        cin >> R >> C;
        getchar();
        memset(a, 0, sizeof(a));
        for (i = 0; i < R; i++)
        {
            gets(a[i]);
            if ((p = strchr(a[i], 'J')) != NULL)
            {
                joe.x = i;
                joe.y = p - a[i];
                joe.step = 0;
                joe.state = 1;
                a[i][joe.y] = '.';
            }
        }
        if ((i = bfs()) != -1)
        {
            cout << i << endl;
        }
        else
        {
            cout << "IMPOSSIBLE" << endl;
        }
    }
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值