newcoder 筱玛的迷阵探险(搜索 + 01字典树)题解
题目描述
寒假终于到了,筱玛决定请他的朋友们一起来玩迷阵探险。
迷阵可以看做一个n×nn×n的矩阵A,每个格子上有一个有一个数Ai,j。
入口在左上角的(1,1)处,出口在右下角的(n,n)处。每一步都只能向下或向右移动一格。最后能获得的经验值为初始经验e与路径上经过的所有数的权值异或和。
求筱玛最大可能获得的经验值。
输入描述:
第一行两个整数n和e。
接下来n行,每行n个整数,描述矩阵A。
输出描述:
一个整数,表示筱玛最大可能获得的经验值。
链接:https://ac.nowcoder.com/acm/contest/545/D
思路:显然我们直接搜等于是在搜一颗二叉树,复杂度O(2^40)左右,肯定超时。但是我们可以用其他方法搜,先从左上角搜,,搜到对角线,然后把所有答案保存在01字典树里。再从右下角往回搜,遇到对角线直接在字典树搜最大异或,取最大值。

代码:
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = 0x3f3f3f3f;
struct node{
node* Next[];
node(){
for(int i = ; i < ; i++)
Next[i] = NULL;
}
};
node* e[maxn]; //走到对角线(含)
int mp[maxn][maxn];
int n, s;
void add(int x, int pos){
node* a = e[pos];
for(int i = ; i >=; i--){
int v = (x >> i) & ;
if(a ->Next[v] == NULL)
a ->Next[v] = new node();
a = a ->Next[v];
}
}
int query(int x, int pos){
node* a = e[pos];
int ret = ;
for(int i = ; i >= ; i--){
int v = (x >> i) & ;
if(a ->Next[!v] != NULL){
a = a ->Next[!v];
ret += ( << i);
}
else a = a ->Next[v];
}
return ret;
}
void dfs(int x, int y, int sum){
if(x + y == n + ){
add(sum ^ mp[x][y], x);
return;
}
dfs(x + , y, sum ^ mp[x][y]);
dfs(x, y + , sum ^ mp[x][y]);
}
int Max;
void dfsBack(int x, int y, int sum){
if(x + y == n + ){
Max = max(query(sum, x), Max);
return;
}
dfsBack(x - , y, sum ^ mp[x][y]);
dfsBack(x, y - , sum ^ mp[x][y]);
}
int main(){
scanf("%d%d", &n, &s);
for(int i = ; i <= n; i++){
e[i] = new node();
for(int j = ; j <= n; j++){
scanf("%d", &mp[i][j]);
}
}
dfs(, , s);
Max = -;
dfsBack(n, n, );
printf("%d\n", Max);
return ;
}
newcoder 筱玛的迷阵探险(搜索 + 01字典树)题解的更多相关文章
- 牛客练习赛37C 筱玛的迷阵探险 双向搜索+字典树
题意 筱玛是个快乐的男孩子.寒假终于到了,筱玛决定请他的朋友们一起来玩迷阵探险.迷阵可以看做一个的矩阵A,每个格子上有一个有一个数Ai,j.入口在左上角的(1,1)处,出口在右下角的(n,n)处.每一 ...
- AcWing:144. 最长异或值路径(dfs + 01字典树)
给定一个树,树上的边都具有权值. 树中一条路径的异或长度被定义为路径上所有边的权值的异或和: ⊕ 为异或符号. 给定上述的具有n个节点的树,你能找到异或长度最大的路径吗? 输入格式 第一行包含整数n, ...
- 字典树基础进阶全掌握(Trie树、01字典树、后缀自动机、AC自动机)
字典树 概述 字典树,又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它 ...
- Chip Factory---hdu5536(异或值最大,01字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k] ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- hdu5296 01字典树
根据二进制建一棵01字典树,每个节点的答案等于左节点0的个数 * 右节点1的个数 * 2,遍历整棵树就能得到答案. AC代码: #include<cstdio> using namespa ...
- Choosing The Commander CodeForces - 817E (01字典树+思维)
As you might remember from the previous round, Vova is currently playing a strategic game known as R ...
- hdu-4825(01字典树)
题意:中文题意 解题思路:01字典树板子题 代码: #include<iostream> #include<algorithm> #include<cstdio> ...
随机推荐
- css 文字样式
Gradient 3D text 代码区域 /*css */ body { background-color: #272727; } h1 { font-family: "Arial&quo ...
- sublime text3 增加代码片段(snipper)
有时候编写代码时有些代码片段经常会用到,如果能将这些代码片段整理,再在需要的时候通过某些条件触发,那无疑是非常方便的! 创建方法:工具->插件开发->新建代码片段 默认产生的内容是: &l ...
- GCD(III)
GCD 线程间的通信 在iOS开发过程中,我们一般在主线程里边进行UI刷新,例如:点击.滚动.拖拽等事件.我们通常把一些耗时的操作放在其他线程,比如说图片下载.文件上传等耗时操作.而当我们有时候在其他 ...
- USMART 组件移植到STM32
USMART是由ALIENTEK开发的一个串口调试助手组件,通过它可以通过串口调试助手,调用程序里面的任何函数并执行,单个函数最多支持10个输入参数,并支持函数返回值显示. USMART支持的参数类型 ...
- foreach 语句
foreach 语句很适合用来枚举 如数组.列表.集合之类的数据结构中的元素. 不必准确知道元素个数.如果基数据不包含任何元素,则foreach循环不执行 foreach(<元素> ...
- svnrdump:E175000:SSL is not supported错误的解决
参考博客:https://www.cnblogs.com/jkko123/p/6358461.html 参考博客:https://blog.csdn.net/w171066/article/detai ...
- 深度学习之循环神经网络RNN概述,双向LSTM实现字符识别
深度学习之循环神经网络RNN概述,双向LSTM实现字符识别 2. RNN概述 Recurrent Neural Network - 循环神经网络,最早出现在20世纪80年代,主要是用于时序数据的预测和 ...
- PHP 取302跳转后真实 URL 的两种方法
1 . 第一种,CURL形式[感觉处理略慢,代码偏多] $url = '将跳转的URL'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url) ...
- highchart应用示例1--2个不同类型变量2个y轴
1.ajax调用接口和处理数据 function getCityData() { var date1 = $('#datetimepicker1').val(); var date2 = $('#da ...
- Codeforce 835A - Key races
Two boys decided to compete in text typing on the site "Key races". During the competition ...