Codeforces Round#459 Div.2 D.MADMAX 搜索+记忆化DP

5 篇文章 0 订阅
4 篇文章 0 订阅
D. MADMAX
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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 vu and a lowercase English letter c, meaning there's an edge from vto u written c on it (1 ≤ v, u ≤ nv ≠ u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.

Output

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.


一、题意

        Max和Lucas进行一场游戏。给定一个有向无环图,每条边都有一个用小写字母标记的值,M和L各自操控一个大理石棋子在图上行走,M先手,要求每一步所走的值要大于等于上一步所走的值(步数是由两个人共同计算,即M走第一步,L走第二步,以此类推),谁先无路可走就失败。结果要求输出一个n*n的矩阵,矩阵的第i行j列表示,M起始于结点i,L起始于结点j时,两个人的胜负情况。A为M获胜,B为L获胜。

二、思路

        可以知道当两个人所在位置,上一步的值,当前谁先手这三个条件都确定的情况下,最后的结果必然是确定的。而且在图上移动的时候会有很多次到达重复状态的可能。所以利用这三个状态建立DP状态,即dp[a][b][v][t]来表示M在a结点,L在b结点,前一步的值为v,t为先手时的胜负情况,采用记忆化搜索的方式解决。而对于每个人走最优解,只需要遍历所有边,找到一条边使得自己的下个状态能够必胜即可保证当前状态必胜

三、代码

#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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值