POJ-1321

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int n, k;
const int N = 10;
char mp[N][N];
int num;
bool row[N];
int sum;

typedef struct {
	int x, y;
}Position;

void Init() {
	num = 0;
	memset(row, true, sizeof(row));
	sum = 0;
}

bool IsDone(int step) {
	return (step == k);
}

void SetVis(Position pos, bool cnt) {
	row[pos.x] = cnt;
}

bool IsValid(Position pos) {
	bool legal, r;
	legal = (mp[pos.x][pos.y] == '#');
	
	r = row[pos.x];
	return legal && r;
}

Position GetNewPos(int i, int step) {
	int y = step;
	int x = i;
	Position next_pos = {x, y};
	return next_pos;
}



void Try(int col) {
	if (IsDone(sum)) {
		++num;
		return;
	}
	if (col == n) return;
	for (int i = 0; i < n; ++i) {
		Position cur_pos = GetNewPos(i, col);
		if (!IsValid(cur_pos)) continue;
		// cout << "i = " << i << ends << "k = " << k << ends << mp[i][col] << endl;
		SetVis(cur_pos, false);
		++sum;
		Try(col + 1); 
		SetVis(cur_pos, true);
		--sum;;
	}
	Try(col + 1); // 这一步很重要,如果对于第i列的每个值都不行的话,
	// 就要遍历第i + 1列;通过在循环外加这一行代码实现。
}

int main() {
	while (scanf("%d%d", &n, &k) && (n != -1 && k != -1)) {
		Init();
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j)
				cin >> mp[i][j];
		}
		Try(0);
		cout << num << endl;
	}
	return 0;
}

垃圾poj,不识别头文件,吐槽归吐槽

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值