poj 2837 Silver Matrix 不使用栈的深搜

题意:

给定k,让构造一个2^k*2^k的矩阵,使得对任意i,第i行和第i列由1,2,...2^k-1这2^k-1个数组成。

分析:

很明显是个深搜题。设n=2^k,则搜素树高度(状态空间维度)为n,每个状态可扩展n个状态,复杂度n^n,大概是512^512。。。所以必须有强力的剪枝而且不要用递归去写。一般对深搜来说,搜索树高度固定的话可以用for循环直接枚举,不固定话要用递归或栈,这题搜索树高度不固定(n为输入),怎么办呢?。。这样可以清楚明了地搞定:dfs的while循环写法。

代码:

//poj 2837
//sep9
#include <iostream>
using namespace std;
const int maxN=520;
int a[maxN][maxN];
bool used[2][maxN][2*maxN];
int n;
void solve()
{
	int x,y,i;
	x=y=1;
	a[x][y]=0;
	while(x<=n){
		int ok=0;
		for(i=a[x][y]+1;i<2*n;++i)
			if(!used[0][x][i]&&!used[1][x][i]&&!used[0][y][i]&&!used[1][y][i]){
				a[x][y]=i;
				used[0][x][i]=used[1][x][i]=used[0][y][i]=used[1][y][i]=true;
				++y;
				if(y>n){y=1,++x;}
				a[x][y]=0;
				ok=1;
				break;
			}
		if(ok==0){
			--y;
			if(y==0){y=0,--x;}
			i=a[x][y];
			used[0][x][i]=used[1][x][i]=used[0][y][i]=used[1][y][i]=false;
		}
	}
}

int main()
{
	scanf("%d",&n);
	n=(1<<n);
	memset(used,false,sizeof(used));
	solve();
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j)
			printf("%d ",a[i][j]);
		printf("\n");
	}
	return 0;
} 
时间: 2024-09-29 11:40:06

poj 2837 Silver Matrix 不使用栈的深搜的相关文章

POJ 3268 Silver Cow Party(SPFA)

Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X 

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

POJ 3422Kaka&#39;s Matrix Travels(最小费用最大流)

Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9460   Accepted: 3844 Description On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka mo

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

poj Kaka&#39;s Matrix Travels

Kaka's Matrix Travels 题目: 给出一个矩阵,求只能向下或者向右的情况下能得到的最大和.一般的是指遍历一次,而这个是可以重复走K次.每经过一次后就把该点设为0.求最大和. 算法: 想到了用网络流做.但是建图没什么自信.看了别人的才敢开始建.建图其实也不难,就是有一个拆点处理,因为,一个点走一次后其上的值就为0了.这个处理很巧妙!就是拆点后建立两条边,一条是有价值的边,一条是没价值,但是可以通过的边.因为,虽然该点没价值,但是有可能其他点要通过它,这就是这题的巧妙之处!!!思抠

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

POJ 1780 Code 欧拉回路+手写栈DFS

和西安邀请赛那道题题目差不多,现在终于会手写栈了,自己琢磨了好久,真是感动TAT #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdli