zoukankan      html  css  js  c++  java
  • 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 (M, N). Estelle Bright and her friends are initially at Scene (1, 1), the entering scene. They should leave Mistwald from Scene (M, N), 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.

    输入

    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 ≤ M, N ≤ 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 (i, N). 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.

    输出

    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.

    样例输入

    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

    样例输出

    Maybe
    False
    Maybe

    True
    False

    思路

    先根据输入构建邻接矩阵,注意输入格式问题。这里不解释邻接矩阵和如何构建邻接矩阵。
    矩阵的乘方表示下一次行动后可到达的地方,k次方表示k次行动后能到达的地方,证明先留坑。
    乘方的时候要注意不能到达最后一个位置,故矩阵乘法运算时要去掉最后一行。
    这里有一个点,因为数值对于本题没有任何意义,为防止数据溢出,可以乘好后强制转换为1。
    最后扫一遍第0个点能到达的,如果只有最后一个是1就是True,如果不止就是Maybe,如果最后一个是0就是False。

    代码

    #include <cstdio>
    #include<cstring>
    #define ll long long
    #define maxn 25
    using namespace std;
    
    int k;
    int m, n, mn;
    
    struct Mat
    {
    	ll f[maxn][maxn];
    	void cls(){memset(f, 0, sizeof(f));}//全部置为0 
    	Mat() {cls();}
    	friend Mat operator * (Mat a, Mat b)
    	{
    		Mat res;
    		for(int i = 0; i < mn-1; i++) for(int j = 0; j < mn; j++)
    		{
    			for(int k = 0; k < mn-1; k++)
    				res.f[i][j] += a.f[i][k] * b.f[k][j];
    			if(res.f[i][j]) res.f[i][j] = 1;
    		}
    		return res;
    	}
    };
    
    Mat quick_pow(Mat a)  
    {  
        Mat ans;
        for(int i = 0; i < maxn; i++) ans.f[i][i] = 1;
        int b = k;
        while(b != 0)  
        {
            if(b & 1) ans = ans * a;
            b >>= 1;
            a = a * a;
        }
        return ans;  
    }
    
    int main()
    {
    	int t; scanf("%d", &t);
    	while(t--)
    	{
    		Mat A;
    		scanf("%d %d
    ", &m, &n);
    		mn = m * n;
    		for(int i = 0; i < m; i++)
    			for(int j = 0; j < n; j++)
    			{
    				int X[4], Y[4];
    				char s[100];
    				scanf("%s", s);
    				sscanf(s,"((%d,%d),(%d,%d),(%d,%d),(%d,%d))",  
                           &X[0], &Y[0], &X[1], &Y[1], &X[2], &Y[2], &X[3], &Y[3]); 
    				for(int k = 0; k < 4; k++)
    				{
    					X[k]--; Y[k]--;
    					A.f[i*n+j][X[k]*n+Y[k]] = 1;
    				}
    			}
    		int Q; scanf("%d", &Q);
    		while(Q--)
    		{
    			scanf("%d", &k);
    			Mat B = quick_pow(A);
    			int sum = 0;
    			for(int i = 0; i < n*m - 1; i++)
    				if(B.f[0][i]) sum++;
    			if(B.f[0][n*m-1])
    			{
    				if(sum) printf("Maybe
    ");
    				else printf("True
    ");
    			}
    			else printf("False
    ");
    		}
    		printf("
    ");
    	}
    	return 0;
    }
    
  • 相关阅读:
    hdu 4970 树状数组 “改段求段”
    hdu 2242 无向图/求用桥一分为二后使俩个bcc点权值和之差最小并输出 /缩点+2次新图dfs
    hdu3715 2-sat+二分
    hdu 3639 有向图缩点+建反向图+搜索
    hdu 3072 有向图缩点成最小树形图计算最小权
    hdu 3061 hdu 3996 最大权闭合图 最后一斩
    hdu 3879 hdu 3917 构造最大权闭合图 俩经典题
    hdu 4738 无向图缩点断桥 // 细节坑题
    hdu3452 无向树去掉最小的边集使任何叶子与根不连通 / 最小割
    hdu 3657 最小割的活用 / 奇偶方格取数类经典题 /最小割
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8397748.html
Copyright © 2011-2022 走看看