Finding Seats Again UVA - 11846

有点复杂的一道题目,考察的也是深搜。用一个二维数组保存结果,每次深搜之前首先找到这个二维数组中没有放置任何值的位置,然后从该位置开始可以考虑从行的方向以及从列的方向可以放置字母的个数。同时注意对于我们选定的区域能够放置的字母的个数不能超过9。然后对于选定的区域进行判断,这个区域必须只能存在一个数字,并且数字的字面值必须与区域的面积是相等的,然后就可以放置字幕,然后递归向后判断了,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;

int n, K;
char area[25][25],res[25][25];

bool dfs(char t,int ind){
	while (ind < n*n&&res[ind / n][ind%n] != '.') ind++;
	if (ind == n*n) return true;
	int cur_x = ind / n, cur_y = ind%n;
	for (int i = cur_x; i < n; i++){
		for (int j = cur_y; j < n; j++){
			int amount = (i - cur_x + 1)*(j - cur_y + 1);
			if (amount > 9) break;
			if (res[i][j] != '.') break;
			int numn = 0,num=-1;
			bool out = false;
			for (int indx = cur_x; indx <= i; indx++){
				for (int indy = cur_y; indy <= j; indy++){
					if (area[indx][indy] != '.'){
						numn++;
						num = area[indx][indy] - '0';
					}
					if (res[indx][indy] != '.'){
						out = true;
						break;
					}
				}
				if (numn > 1 || out || (num != -1 && num != amount)) break;
			}
			if (numn > 1 || out || (num != -1 && (num < amount))) break;
			if (numn == 0 || num > amount) continue;
			for (int indx = cur_x; indx <= i; indx++){
				for (int indy = cur_y; indy <= j; indy++){
					res[indx][indy] = t;
				}
			}
			if (dfs(t + 1, ind + (j-cur_y+1))) return true;
			for (int indx = cur_x; indx <= i; indx++){
				for (int indy = cur_y; indy <= j; indy++){
					res[indx][indy] = '.';
				}
			}
		}
	}
	return false;
}

int main(){
	while (cin >> n >> K){
		if (n == 0 && K == 0) break;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				cin >> area[i][j];
				res[i][j] = '.';
			}
		}
		dfs('A',0);
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				cout << res[i][j];
			}
			cout << endl;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值