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.
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.
一、题意
二、思路
三、代码
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int MAXN = 110;
const int MOD7 = 1e9 + 7;
const int MOD9 = 1e9 + 9;
const int INF = 2e9;
const double EPS = 1e-6;
const double PI = 3.14159265358979;
const int dir_4r[] = { -1, 1, 0, 0 };
const int dir_4c[] = { 0, 0, -1, 1 };
const int dir_8r[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
const int dir_8c[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
struct Edge {
int to;//下一个结点
int val;//该边的值
Edge(int t, int v) :to(t), val(v) {}
};
int dp[MAXN][MAXN][27][2];//dp[a][b][v][t]来表示M在a结点,L在b结点,前一步的值为v,t为先手时的胜负情况
vector<Edge> mat[MAXN];
char ans[MAXN][MAXN];
int dfs(int a, int b, int v, int t) {
if (dp[a][b][v][t] != 0)
return dp[a][b][v][t];
int win;
if (t == 0) {//M先手
win = 'B';
for (int i = 0; win != 'A' && i < mat[a].size(); ++i) {
int to = mat[a][i].to;
int val = mat[a][i].val;
if (val >= v)
if (dfs(to, b, val, 1) == 'A')//采取最优的走法,即自己必胜
win = 'A';
}
} else {//L先手
win = 'A';
for (int i = 0; win != 'B' && i < mat[b].size(); ++i) {
int to = mat[b][i].to;
int val = mat[b][i].val;
if (val >= v)
if (dfs(a, to, val, 0) == 'B')//采取最优的走法,即自己必胜
win = 'B';
}
}
dp[a][b][v][t] = win;
return win;
}
int main() {
int n, m, v, u;
char ch[10];
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
scanf("%d%d%s", &v, &u, ch);
v--;
u--;
mat[v].push_back(Edge(u, ch[0] - 'a' + 1));
}
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) {
ans[i][j] = dfs(i, j, 0, 0);//v = 0表示第一步
}
for (int i = 0; i < n; ++i) {
ans[i][n] = '\0';
printf("%s\n", ans[i]);
}
//system("pause");
return 0;
}