Sicily oj 1822. Fight Club(区间dp)

来源:互联网 发布:基站定位数据库 编辑:程序博客网 时间:2024/05/01 23:01

黑书上的原题,主要是你得想到拆点把一个点抽象成两个点。

题意: 一群人决斗, 给出一个人与另一个人决斗的胜负关系, 问结果合理决策每个人是否可以胜利.
解题思路:
1. 假设判断第i人是否可以胜出, 将第i人拆成2个人, 可以得出, 只要第i人能够与自己相遇
那么第i人就可以胜利.
2. 设dp[i][j]表示第i人是否可以与第j人相遇, vis[i][j]表示第i人与第j的决斗胜负关系.
(1). dp[i][j] = 能够相遇; 满足: dp[i][k]&&dp[j][k]&&(vis[i][k]||vis[j][k])
(2). dp[i][j] = 不能相遇;
3. 因为n个人围成一个圆, 我们可以扩大一倍人数, 方便计算i 等价 i+n;

PS:没看到最后要输出一个换行,还在那里找错误,sad啊、、、

1822. Fight Club

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Fight club is an organization where you release your pressure and emotion by fighting the other club members. The fight begins like this: n people standing in a circle. Then two adjacent guys are chosen to fight. The winner will stay and the loser will be sent to the hospital (there is no tie in the fighting). The cruel fight continues until there is only one left.
It is somewhat strange to see that when two guys fight each other, the result is always definite, and you know the result of every pair of fighters. Though they enjoy fighting, they still want to be the winner. You are to tell each of them, if the fight is arranged properly, can he be the winner.

Input

First line contains integer T (T<=5), the number of test cases.
For each test case, the first line contains n (1<=n<=40), the number of people. Following is the description of a n*n matrix A. Following n lines each contains n ‘0’ or ‘1’, separated by a single blank. The ith number in jth line indicates the fighting result of ith and jth fighter. If it is ‘1’, then the ith fighter will always beat the jth fighter, otherwise he will always lose. You can safely assume that aij and aji are different and aii=0 for every distinct i and j.
The people are numbered counterclockwise. 

Output

For each test case, output n lines. If the ith fighter can be the survivor, output 1, otherwise output 0.
Output a blank line after each test case. 

Sample Input

220 10 030 1 10 0 10 0 0

Sample Output

10100
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7#define M 1000100///#define LL __int64#define LL long long#define INF 0x3fffffff#define PI 3.1415926535898using namespace std;const int maxn = 110;int vis[maxn][maxn];int dp[maxn][maxn];int main(){    int T;    cin >>T;    while(T--)    {        int n;        cin >>n;        for(int i = 0; i < n; i++)            for(int j = 0; j < n; j++)                cin >>vis[i][j];        memset(dp, 0 ,sizeof(dp));        for(int i = 0; i < 2*n; i++)            dp[i][i+1] = 1;        for(int len = 2; len <= n; len++)        {            for(int i = 0; i+len < 2*n; i++)            {                for(int j = i+1; j < i+len; j++)                {                    if(dp[i][j] && dp[j][i+len])                    {                        int x = (i+n)%n;                        int y = (j+n)%n;                        int z = (i+len+n)%n;                        if(vis[x][y] || vis[z][y])                            dp[i][len+i] = 1;                    }                }            }        }        for(int i = 0; i < n; i++)        {            if(dp[i][i+n])                cout<<1<<endl;            else                cout<<0<<endl;        }        cout<<endl;    }    return 0;}


0 0
原创粉丝点击