http://codeforces.com/contest/918/problem/D
As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter.
![](https://i-blog.csdnimg.cn/blog_migrate/e7c294d0f27003b1845785860c2aafc1.png)
Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time.
Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game?
You have to determine the winner of the game for all initial positions of the marbles.
The first line of input contains two integers n and m (2 ≤ n ≤ 100, ).
The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from vto u written c on it (1 ≤ v, u ≤ n, v ≠ u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.
Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise.
4 4 1 2 b 1 3 a 2 4 c 3 4 b
BAAA ABAA BBBA BBBB
5 8 5 3 h 1 2 c 3 1 c 3 2 r 5 1 r 4 3 z 5 4 r 5 2 h
BABBB BBBBB AABBB AAABA AAAAB
Here's the graph in the first sample test case:
![](https://i-blog.csdnimg.cn/blog_migrate/6409c23fea8c6d30a04169c27d9da74c.png)
Here's the graph in the second sample test case:
![](https://i-blog.csdnimg.cn/blog_migrate/31664ef4c2301d32c40f0fc3b8f941c0.png)
博弈。
给出一个有向无环图(DAG),边权值为字母的ASCII码值。两个人A和B分别从某两点出发,一人走一步,要求当前人走的边的字母要大于等于对手上一步走的边权。谁先无路可走谁就输。 输出一个n*n的矩阵,表示所有的出发点情况对应的输赢情况。
【分析】
在刘汝佳紫书上看过DAG最长路的DP模型,只不过这里需要同时照顾两个起点。
设dp[t][i][j]表示上一步的边是t,i为先手,j为后手,时的博弈结果。 0表示当前先手 i 无路可走。
则dp[t][i][j] 的下一步应该是dp[t1][j][k],t1是大于等于t的边值,j变成了先手,k是i的下一步。
只要存在一个dp[t1][j][k]==0(无路可走),则证明状态dp[t][i][j]有机会赢!
【代码】
#include<bits/stdc++.h>
using namespace std;
int Map[200][200];
int dp[128][200][200]; //字母,先手,后手
int n,m;
int DP(char t,int i,int j)
{
if(dp[t][i][j]>=0)return dp[t][i][j];
for(int k=1;k<=n;k++){
if(t<=Map[i][k]&&DP(Map[i][k],j,k)==0)//后手无路可走
return dp[t][i][j]=1;
}
return dp[t][i][j]=0;
}
int main()
{
while(cin>>n>>m)
{
memset(dp,-1,sizeof(dp));
memset(Map,0,sizeof(Map));
for(int i=0;i<m;i++)
{
int u,v;char ch[3];
scanf("%d%d%s",&u,&v,ch);
Map[u][v]=ch[0];
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
DP('a',i,j); //从a出发
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(dp['a'][i][j]%2)printf("A");
else printf("B");
}
printf("\n");
}
}
}