矩阵快速幂ZOJ - 3497(矩阵在图的联通

本文介绍了一个迷宫穿越问题,通过构建矩阵并运用矩阵快速幂的方法来判断角色能否在指定步数内从起点达到终点。该方法能够有效解决路径可达性问题。

In chapter 4 of the game Trails in the Sky SC, Estelle Bright and her friends are crossing Mistwald to meet their final enemy, Lucciola.

Mistwald is a mysterious place. It consists of M * N scenes, named Scene (1, 1) to Scene (MN). Estelle Bright and her friends are initially at Scene (1, 1), the entering scene. They should leave Mistwald from Scene (MN), the exiting scene. Note that once they reach the exiting scene, they leave Mistwald and cannot come back. A scene in Mistwald has four exits, north, west, south, and east ones. These exits are controlled by Lucciola. They may not lead to adjacent scenes. However, an exit can and must lead to one scene in Mistwald.

Estelle Bright and her friends walk very fast. It only takes them 1 second to cross an exit, leaving a scene and entering a new scene. Other time such as staying and resting can be ignored. It is obvious that the quicker they leave Mistwald, the better.

Now you are competing with your roommate for who uses less time to leave Mistwald. Your roommate says that he only uses P seconds. It is known that he lies from time to time. Thus, you may want to code and find out whether it is a lie.

Input

There are multiple test cases. The first line of input is an integer T ≈ 10 indicating the number of test cases.

Each test case begins with a line of two integers M and N (1 ≤ MN ≤ 5), separated by a single space, indicating the size of Mistwald. In the next M lines, the ith line contains N pieces of scene information, separated by spaces, describing Scene (i, 1) to Scene (iN). A scene description has the form "((x1,y1),(x2,y2),(x3,y3),(x4,y4))" (1 ≤ xk ≤ M; 1 ≤ yk ≤ N; 1 ≤ k ≤ 4) indicating the locations of new scenes the four exits lead to. The following line contains an integer Q (1 ≤ Q ≤ 100). In the next Q lines, each line contains an integer P (0 ≤ P ≤ 100,000,000), which is the time your roommate tells you.

Test cases are separated by a blank line.

Output

For each P, output one of the following strings in one line: "True" if it cannot be a lie; "Maybe" if it can be a lie; "False" if it must be a lie.

Print a blank line after each case.

Sample Input
2
3 2
((3,1),(3,2),(1,2),(2,1)) ((3,1),(3,1),(3,1),(3,1))
((2,1),(2,1),(2,1),(2,2)) ((3,2),(3,2),(3,2),(3,2))
((3,1),(3,1),(3,1),(3,1)) ((3,2),(3,2),(3,2),(1,1))
3
1
2
10

2 1
((2,1),(2,1),(2,1),(2,1))
((2,1),(2,1),(2,1),(2,1))
2
1
2
Sample Output
Maybe
False
Maybe

True
False

题意:有一个n*m的矩阵,矩阵上每一个格子有四个传送门,分别通向四个格子,题目给出了每个格子的四个传送门所能到达的地方。起点在(1,1),终点是(n,m),当走到终点的时候就不能再走了,也就是说一旦你到达了终点,就会直接离开这个矩阵。问说从起点开始走P(0 ≤ P ≤ 100,000,000)步能不能到达终点。

输出的情况是True,Maybe和False。Ture对应的就是走了P步以后只能到达一个点,就是终点。Maybe对应的是走了P步之后还可以到达除了终点以外的其他点。False对应的是P步以后无法到达终点。

做法:

对矩阵做P次幂,如果可达,那么对应位置应该不为0;


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;


#define LL long long
#define INF 1E4 * 1E9
#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
const int maxn=1e2+5;
const int maxx=1e6+5;

//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
/*
2
3 2
((3,1),(3,2),(1,2),(2,1)) ((3,1),(3,1),(3,1),(3,1))
((2,1),(2,1),(2,1),(2,2)) ((3,2),(3,2),(3,2),(3,2))
((3,1),(3,1),(3,1),(3,1)) ((3,2),(3,2),(3,2),(1,1))
3
1
2
10

2 1
((2,1),(2,1),(2,1),(2,1))
((2,1),(2,1),(2,1),(2,1))
2
1
2

*/

int n,m,t,size,q,p;
struct mat
{
    int a[maxn][maxn];
     mat()
    {
        me(a);
    }
};

mat multi(mat m1,mat m2)
{
    mat ans=mat();
    for(int i=0;i<size;i++)
        for(int j=0;j<size;j++)
            if(m1.a[i][j])
                for(int k=0;k<size;k++)
				{
					if((ans.a[i][k]+m1.a[i][j]*m2.a[j][k]))
                    ans.a[i][k]=1;
				}
	return ans;
}

mat quickmulti(mat m,int n)
{
    mat ans=mat();
    int i;
    for(i=0;i<size;i++)
		ans.a[i][i]=1;
    while(n)
    {
        if(n&1) ans=multi(m,ans);
        m=multi(m,m);
        n>>=1;
    }
    return ans;
}

int main()
{
    cin>>t;
    char op;
    while(t--)
    {
        cin>>n>>m;
        mat mp=mat();
        size=n*m;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
        {
            int s=i*m+j;
            int x,y;
            getchar();
            cin>>op;
            for(int k=0;k<4;k++)
            {
                cin>>op;
                scanf("%d,%d%c%c",&x,&y,&op,&op);
                x--;y--;
                int ant=x*m+y;//标记
                if(s!=n*m-1)
                    mp.a[s][ant]=1;//起始到标记点
            }
        }
       /* for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
        {
            cout<<mp.a[i][j]<<"___________"<<i<<"************"<<j<<endl;
        }*/
        cin>>q;
        while(q--)
        {
            cin>>p;
            mat chu=mat();
            chu.a[0][0]=1;
            chu=multi(chu,quickmulti(mp,p));
            if(chu.a[0][n*m-1]==0)
				printf("False\n");
            else
			{
				int flag=0;
				for(int i=0;i<n*m-1;i++)
				{
					if(chu.a[0][i])
						flag=1;
				}
				if(flag)
					printf("Maybe\n");
				else
					printf("True\n");
			}
		}
		cout<<endl;
    }
}


















Matlab基于粒子群优化算法及鲁棒MPPT控制器提高光伏并网的效率内容概要:本文围绕Matlab在电力系统优化与控制领域的应用展开,重点介绍了基于粒子群优化算法(PSO)和鲁棒MPPT控制器提升光伏并网效率的技术方案。通过Matlab代码实现,结合智能优化算法与先进控制策略,对光伏发电系统的最大功率点跟踪进行优化,有效提高了系统在不同光照条件下的能量转换效率和并网稳定性。同时,文档还涵盖了多种电力系统应用场景,如微电网调度、储能配置、鲁棒控制等,展示了Matlab在科研复现与工程仿真中的强大能力。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的高校研究生、科研人员及从事新能源系统开发的工程师;尤其适合关注光伏并网技术、智能优化算法应用与MPPT控制策略研究的专业人士。; 使用场景及目标:①利用粒子群算法优化光伏系统MPPT控制器参数,提升动态响应速度与稳态精度;②研究鲁棒控制策略在光伏并网系统中的抗干扰能力;③复现已发表的高水平论文(如EI、SCI)中的仿真案例,支撑科研项目与学术写作。; 阅读建议:建议结合文中提供的Matlab代码与Simulink模型进行实践操作,重点关注算法实现细节与系统参数设置,同时参考链接中的完整资源下载以获取更多复现实例,加深对优化算法与控制系统设计的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值