您的位置:首页 > 其它

Codeforces Round #459 (Div. 2) D. MADMAX 博弈和记忆化搜索(同时照顾两个起点的记忆化搜索)

2018-02-25 19:26 309 查看
D. MADMAXtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAs 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.

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.InputThe 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.OutputPrint 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.ExamplesinputCopy
4 4
1 2 b
1 3 a
2 4 c
3 4 b
output
BAAA
ABAA
BBBA
BBBB
inputCopy
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
output
BABBB
BBBBB
AABBB
AAABA
AAAAB
NoteHere's the graph in the first sample test case:

Here's the graph in the second sample test case:

题意:给定一个有向无环图(DAG),边权为小写字母ASCLL值,两个人A和B分别在两点出发,一人走一步,每一步算是一轮,这一轮走的边权一定大于等于上一轮走的边权,也就是当前人走的边权一定大于等于另外一个人(上一轮)走过的边权,谁先没有路可走就算输。输出一个n*n的矩阵,表示当前位置那个人赢了;
思路:博弈的思想,有固定起点的DAG模型,只不过这里要照顾两个起点
设dp[f][i][j] f为上一轮对手走过的边权,i为当前人所在位置,j为对手所在位置,该当前人走,根据博弈论,等于0时,表示当前人先手i 无路可走;dp[f][i][j]的下一步为dp[f1][j][k],f1大于f,现在先手变成了j,k为i的下一步
若存在一个 dp[f1][j][k] == 0,就说明dp[f][i][j] 有可能赢,按照博弈论上的必败态、必胜态说,这时候dp[f][i][j]为必胜态;
若所有的 dp[f1][j][k] == 1, 就说明dp[f][i][j]为必败态;
这里也用到了记忆化搜索;
代码:#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<vector>
#define Max 110
int dp[30][Max][Max];
int n,m;

struct node
{
int to,vec;
};
vector<node> v[Max];
void init()
{
int i,j;
for(i = 0; i <= n; i ++)
v[i].clear();
memset(dp,-1,sizeof(dp));
}

int dfs(int f,int x,int y)
{
if(dp[f][x][y] != -1) return dp[f][x][y];
for(int i = 0;i< v[x].size();i ++)
{
node tt = v[x][i];
if(tt.vec >= f&&dfs(tt.vec,y,tt.to) == 0)
return dp[f][x][y] = 1;
}
return dp[f][x][y] = 0;
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
int x,y;
char c;
init();
while(m--)
{
scanf("%d %d %c",&x,&y,&c);
node tt;
tt.to = y;
tt.vec = c - 'a' + 1;
v[x].push_back(tt);
}
for(i = 1; i <= n; i ++)
{
for(j = 1; j <= n; j ++)
{
if(dfs(0,i,j) == 1) printf("A");
else printf("B");
}
printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: