ZOJ3750 Dot Dot Dot 枚举状态+BFS

Dot Dot Dot

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Recently DZB is playing a game called Dot Dot Dot. It's a simple game but very interesting. The rule is like this:

The game is played on a N*M grid map, each grid is adjacent with 4 grids(up, down, left, right). There's only one exit on the map, the game finished when the player reach the exit. When the game starts, the player go to the exit step by step. In each step he can only move to adjacent grid. But there's some barriers on the map, each barrier is yellow or green. The player can not go to the grid when there is a barrier or there is outside wall.

The only way to destroy the barriers is to stand on some grid which has a trigger. Each trigger is also yellow or green and can be used only once. When the player stand on it, it will sent shock waves to destroy all the barriers with the same color it could reach directly. Shock waves spread as wide as possible. It can spread on all the connected grids which no barrier is on it, that means if it reach a barrier with different color, it will be blocked too. Notice if the wave can not reach a barrier directly, it can not be destroied, even if it's the same color with the trigger.

Now DZB not only wants to finish the game, but also wants to finish the game with the minimal step, so he ask you for help.

Input

There are multiple test cases. For each test case:

The first line contains two integers N(1≤N≤40), M(1≤M≤40), N is the number of rows of the map, and M is the number of the columns.

The after N lines describe the map, each line has M characters. A character could be:

  • '.' means there's nothing, you can stand there.
  • '*' means the outside wall, we guarantee that the outside wall form a close area and the player is in it.
  • 'Y' means a yellow barrier, you can not stand there unless you have destroied the barrier.
  • 'G' means a green barrier, similar with 'Y'.
  • 'y' means a yellow trigger.
  • 'g' means a green trigger. Notice the number of yellow trigger plus the number of green trigger is no more than 5.
  • 's' means the initial position of the player.
  • 'e' means the exit, the player must go to there finally to finish the game.

There is a blank line between every two cases.

Process to the end of input.

Output

One line for each test case. The minimal step. If can not reach the exit, print -1 instead.

Sample Input
5 17
*****************
*.......Y.......*
*.y.s...Y....e..*
*.......Y.......*
*****************
Sample Output
13
 
解题思路:由于只有5个trigger,以不同的顺序到达trigger,每一种顺序可以用一个状态表示。求所有状态下的最短路就可以了。
/********************************
*	作者:Linfly
*	时间:2014-07-16 21:11
********************************/
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <stack>
#include <cstring>
using namespace std;

#define MAXN 50

struct stNode{
    int x,y;
    int f;
    stNode(int xx, int yy, int ff)
    {
        x = xx, y = yy, f =ff;
    }
    stNode()
    {
        x = y = f = 0;
    }
};

char m[MAXN][MAXN], g[MAXN][MAXN];
int N,M;
int mindist;
int sx,sy,ex,ey;
int trigger;
int seq[6];

int DX[4] = { 0,-1,0,1};
int DY[4] = {-1, 0,1,0};

void GetXY(int no, int &x, int &y)
{
    int cnt = 0;

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j<M; j++)
        {
            if (m[i][j] == 'y' || m[i][j] == 'g')
                cnt ++;
            if (cnt == no)
            {
                x = i;
                y = j;
                return ;
            }
        }
    }
}
int BFS(int curx, int cury, int dx, int dy, int &dd)
{
    int dist = 0xfffffff;
    int mdist[MAXN][MAXN];
    memset(mdist, 0x7f, sizeof(mdist));
    bool inQueue[MAXN][MAXN];
    memset(inQueue, false, sizeof(inQueue));

    queue<struct stNode> Q;
    Q.push(stNode(curx, cury, 0));
    inQueue[curx][cury] = true;
    mdist[curx][cury] = 0;
    while (!Q.empty())
    {
        stNode node = Q.front();
        Q.pop();
        for (int i = 0; i<4; i++)
        {
            int xx = node.x + DX[i];
            int yy = node.y + DY[i];
            if (xx == dx && yy == dy && dist > node.f+1) //到触发器的最短路径
            {
                dist = node.f + 1;
            }
            if (xx == ex && yy == ey && dd > node.f+1) //当前状态到终点的路径
            {
                dd = node.f + 1;
            }
            if (!inQueue[xx][yy] && node.f+1<mdist[xx][yy] && (g[xx][yy] == 's' || g[xx][yy] == '.' || g[xx][yy] == 'y' || g[xx][yy] == 'g'))
            {
                Q.push(stNode(xx, yy, node.f+1));
                inQueue[xx][yy] = true;
                mdist[xx][yy] = node.f+1;
            }

            if (g[dx][dy] - g[xx][yy] == 32) //重建图
            {
                g[xx][yy] = '#';
            }
        }
        inQueue[node.x][node.y] = false;
    }
    /*
    cout << "++++++++++++++++++++++++++++++++++++++++" << endl;
    cout << "curx:" << curx << " cury:" << cury << " dx:" << dx << " dy:" << dy<< endl;
    cout << "dist:" << dist << " dd:" << dd << endl;
    for (int i = 0; i < N; i++)
        cout << g[i] << endl;
    cout << "++++++++++++++++++++++++++++++++++++++++" << endl;
    */
    return dist;
}

bool Check(int seq[])
{
    int curx = sx, cury = sy;
    int dx, dy;
    int sumdist = 0;
    //BFS(curx, cury, ex, ey, sumdist);
    int i = 0;
    for (i = 0; i<trigger; i++)
    {
        dx = dy = 0;
        GetXY(seq[i],dx,dy); //得到触发器坐标
        int dd = 0xfffffff;
        int dist = BFS(curx, cury, dx, dy, dd);
        if (dd != 0xfffffff && sumdist+dd < mindist)
        {
            mindist = sumdist + dd;
        }
        if (dist == 0xfffffff) //无法到达状态(dx,dy)
            break;
        sumdist += dist;
        curx = dx;
        cury = dy;
        for (int k = 0; k<N; k++)
        {
            for (int j = 0; j<M; j++)
            {
                if (g[k][j] == '#')
                    g[k][j] = '.';
            }
        }
    }
    if (i == trigger)
    {
        int dd = 0xfffffff;
        BFS(curx, cury, ex, ey, dd);
        if (dd != 0xfffffff && sumdist+dd < mindist)
        {
            mindist = sumdist + dd;
        }
    }
    return true;
}

int main()
{
    while (cin >> N >> M)
    {
        memset(m , 0, sizeof(m));
        trigger = 0;
        mindist = 0xfffffff;
        for (int i = 0; i < N; i++)
        {
            cin >> m[i];
            for (int j = 0; j < M; j++)
            {
                if (m[i][j] == 'y' || m[i][j] == 'g')
                    trigger++;
                else if (m[i][j] == 's')
                {
                    sx = i;
                    sy = j;
                }
                else if (m[i][j] == 'e')
                {
                    ex = i;
                    ey = j;
                }
            }
        }
        /*
        for (int i = 0; i < N; i++)
            cout << m[i] << endl;
        */
        for (int i = 0; i<trigger; i++) //编号触发点
            seq[i] = i+1;

        do{
            /*
            for (int i = 0; i < trigger; i++)
                cout << seq[i] << " ";
            cout << endl;
            */
            memcpy(g, m, sizeof(m));
            /*
            for (int i = 0; i < N; i++)
                cout << g[i] << endl;
            */
            Check(seq);
        }while (next_permutation(seq, seq+trigger));

        if (mindist == 0xfffffff)
            cout << -1 << endl;
        else
            cout << mindist << endl;
    }
    return 0;
}


内容概要:本文详细探讨了双馈风力发电机(DFIG)在Simulink环境下的建模方法及其在不同风速条件下的电流与电压波形特征。首先介绍了DFIG的基本原理,即定子直接接入电网,转子通过双向变流器连接电网的特点。接着阐述了Simulink模型的具体搭建步骤,包括风力机模型、传动系统模型、DFIG本体模型和变流器模型的建立。文中强调了变流器控制算法的重要性,特别是在应对风速变化时,通过实时调整转子侧的电压和电流,确保电流和电压波形的良好特性。此外,文章还讨论了模型中的关键技术和挑战,如转子电流环控制策略、低电压穿越性能、直流母线电压脉动等问题,并提供了具体的解决方案和技术细节。最终,通过对故障工况的仿真测试,验证了所建模型的有效性和优越性。 适用人群:从事风力发电研究的技术人员、高校相关专业师生、对电力电子控制系统感兴趣的工程技术人员。 使用场景及目标:适用于希望深入了解DFIG工作原理、掌握Simulink建模技能的研究人员;旨在帮助读者理解DFIG在不同风速条件下的动态响应机制,为优化风力发电系统的控制策略提供理论依据和技术支持。 其他说明:文章不仅提供了详细的理论解释,还附有大量Matlab/Simulink代码片段,便于读者进行实践操作。同时,针对一些常见问题给出了实用的调试技巧,有助于提高仿真的准确性和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值