Xor-Paths

本文探讨了在一个矩阵中寻找从左上角到右下角的路径,路径上的数字异或和等于特定值的问题。通过双端深度优先搜索(DFS)算法,结合哈希表记录中间状态,有效地解决了这一挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There is a rectangular grid of size n×mn×m. Each cell has a number written on it; the number on the cell (i,ji,j) is ai,jai,j. Your task is to calculate the number of paths from the upper-left cell (1,11,1) to the bottom-right cell (n,mn,m) meeting the following constraints:

  • You can move to the right or to the bottom only. Formally, from the cell (i,ji,j) you may move to the cell (i,j+1i,j+1) or to the cell (i+1,ji+1,j). The target cell can't be outside of the grid.
  • The xor of all the numbers on the path from the cell (1,11,1) to the cell (n,mn,m) must be equal to kk (xor operation is the bitwise exclusive OR, it is represented as '^' in Java or C++ and "xor" in Pascal).

Find the number of such paths in the given grid.

Input

The first line of the input contains three integers nn, mm and kk (1≤n,m≤201≤n,m≤20, 0≤k≤10180≤k≤1018) — the height and the width of the grid, and the number kk.

The next nn lines contain mm integers each, the jj-th element in the ii-th line is ai,jai,j(0≤ai,j≤10180≤ai,j≤1018).

Output

Print one integer — the number of paths from (1,11,1) to (n,mn,m) with xor sum equal to kk.

Examples

Input

3 3 11
2 1 5
7 10 0
12 6 4

Output

3

Input

3 4 2
1 3 3 3
0 3 3 2
3 0 1 1

Output

5

Input

3 4 1000000000000000000
1 3 3 3
0 3 3 2
3 0 1 1

Output

0

Note

All the paths from the first example:

  • (1,1)→(2,1)→(3,1)→(3,2)→(3,3)(1,1)→(2,1)→(3,1)→(3,2)→(3,3);
  • (1,1)→(2,1)→(2,2)→(2,3)→(3,3)(1,1)→(2,1)→(2,2)→(2,3)→(3,3);
  • (1,1)→(1,2)→(2,2)→(3,2)→(3,3)(1,1)→(1,2)→(2,2)→(3,2)→(3,3).

All the paths from the second example:

  • (1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(3,4)(1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(3,4);
  • (1,1)→(2,1)→(2,2)→(3,2)→(3,3)→(3,4)(1,1)→(2,1)→(2,2)→(3,2)→(3,3)→(3,4);
  • (1,1)→(2,1)→(2,2)→(2,3)→(2,4)→(3,4)(1,1)→(2,1)→(2,2)→(2,3)→(2,4)→(3,4);
  • (1,1)→(1,2)→(2,2)→(2,3)→(3,3)→(3,4)(1,1)→(1,2)→(2,2)→(2,3)→(3,3)→(3,4);
  • (1,1)→(1,2)→(1,3)→(2,3)→(3,3)→(3,4)(1,1)→(1,2)→(1,3)→(2,3)→(3,3)→(3,4).

 

题解:大意是从左上角(1,1)开始到右下角(n,m)只能往左或往下走的异或值为K的路径有多少条。

 用两个dfs去搜,一个从前面搜到一半,即x+y==max(n,m),另一个·从后面倒着搜,搜到一半,即x+y==max(n,m)+1;在搜的过程中异或一下num[i][j],用map标记一下到各点异或值的个数,累加起来就可以了。

代码:

#include<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>
#define ll long long
using namespace std;
map<ll,ll>mp[30][30];
int n,m,t;
ll k,num[30][30],ans;
// 双端dfs , 一个 从 (1,1)到(i,j)且 i+j==max(n,m)

void dfs1(int x,int y,ll tt)
{
	if(x+y==t)
	{
		mp[x][y][tt]++;  //用map记录搜到点(y,x)处时,值为tt的个数
		return ;
	}
	if(x+1<=n)
	   dfs1(x+1,y,tt^num[x+1][y]);
	if(y+1<=m)
	   dfs1(x,y+1,tt^num[x][y+1]);
}
// 第二个 从(n,m)到 (i,j) 且(i+j)==max(n,m)+1
void dfs2(int x,int y,ll tt)
{
	if(x+y==t+1)
	{
		if(x-1>=1)
			ans+=mp[x-1][y][tt^k];
		if(y-1>=1)
		    ans+=mp[x][y-1][tt^k];
		return ;
	}
	if(x-1>=1)
	  dfs2(x-1,y,tt^num[x-1][y]);
	if(y-1>=1)
	  dfs2(x,y-1,tt^num[x][y-1]);
}
int main()
{
	while(~scanf("%d%d%lld",&n,&m,&k))
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
		    	mp[i][j].clear();
		 	  scanf("%lld",&num[i][j]);
			}
		}
		ans=0;
		t=max(n,m);
		if(1+1<=t)
		{
			dfs1(1,1,num[1][1]);
			dfs2(n,m,num[n][m]);
		}
		else
		{
			if(num[1][1]==k)
			 ans++; 
		}
		printf("%lld\n",ans);
	}
}

 

为了找到从顶点 `1` 到顶点 `N` 的路径上边标签的最小异或值(XOR),我们可以采用深度优先搜索(DFS)算法遍历所有简单路径,并计算每条路径上的边权重的异或结果。 由于题目限制了图的最大规模 \( N \leq 10 \),这使得我们可以在合理的时间内穷举所有的路径并比较它们的结果。以下是详细的解题步骤: ### 解决方案 ```cpp #include <iostream> #include <vector> #include <unordered_map> using namespace std; typedef long long ll; const int MAX_N = 11; // 最大节点数+1 ll min_xor_sum = LLONG_MAX; // 初始化为最大值 // 图结构表示:邻接表 vector<pair<int, ll>> adj[MAX_N]; bool visited[MAX_N]; void dfs(int current, int end, ll xor_val) { if (current == end) { // 如果到达目标节点 min_xor_sum = min(min_xor_sum, xor_val); return; } for (auto& edge : adj[current]) { int next_node = edge.first; ll weight = edge.second; if (!visited[next_node]) { visited[next_node] = true; dfs(next_node, end, xor_val ^ weight); // 更新当前xor值 visited[next_node] = false; // 回溯 } } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int u, v; ll w; cin >> u >> v >> w; adj[u].emplace_back(v, w); adj[v].emplace_back(u, w); // 无向图,双向添加 } memset(visited, false, sizeof(visited)); visited[1] = true; dfs(1, n, 0LL); cout << "Minimum XOR sum is: " << min_xor_sum << endl; return 0; } ``` ### 算法解析 1. **输入读取**: - 使用标准输入流读入节点数目、边的数量及每个边的信息。 2. **图构建**: - 建立一个邻接表用于存储图形信息;因为是无向图,所以需要对称地保存两个方向的连接关系。 3. **初始化状态变量**: - 设置全局最小异或和初始值。 - 创建访问标记数组避免重复走回头路形成环。 4. **递归 DFS 遍历**: - 对于每一个未被访问过的邻居节点继续深入探索直到抵达终点为止,在此过程中累积路径中的边权进行按位异或操作。 5. **回溯处理**: - 当尝试完某一分支后需恢复原状以便其他分支可以正常工作。 6. **输出最终结果** ### 示例运行说明 对于给定的例子数据集,程序会打印出符合条件的一条最短路径对应的最小异或值即为所求的答案。此外,本代码已经考虑到了所有测试用例外部边界条件如空图等情况下的健壮性设计。 #### 注意事项 - 此处假设输入合法并且必定存在一条从起点到终点的有效路径; - 根据实际需求调整常量定义以适应更大范围的问题实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值