P . Finding Seats Again

Description

A set of n2n2 computer scientists went to the movies. Fortunately, the theater they chose has a square layout: nn rows, each one with nn seats. However, these scientists are not all from the same research area and they want to seat together. Indeed, there are KK independent research groups of scientists among them (no scientist belongs to two of them) with a distiguished leader for each group. Then the leader bought the tickets for his whole group, and he did it in such a way that all his group could seat occupying a rectangular set of seats (and everyone in this set of seats belongs to the same group). Every group was placed satisfying this bizarre condition, although the scientists did not care where the actual assigned areas were.

The usher was informed of the situation and he decided to annotate in a theater map a satisfactory seats deploying. He thought that if he wrote the position of each group’s leader in the map indicating besides the corresponding group size, he could tell where to accomodate every scientist. But he discovered that it is not so easy!

The usher asks for your help. You must tell him a way to place the K rectangular areas with the given sizes, and with the corresponding leader for each group seated where it was originally assigned.

Input

Input consists of several test cases, each one defined by a set of lines:

  • the first line in the case contains two numbers nn and KK separated by blanks, with nn representing the size of the theater (0<n<200<n<20) and KK the number of groups (K≤26K≤26);
  • the next nn lines describe the usher’s map. A one-digit decimal number in the map indicates the seat of a leader and the size of his group. A point indicates that no leader will sit there.

The end of the input is indicated by the line

0 0

Output

For each test case, display an answer consisting in nn lines each one of them with nn characters representing a seat occupation for the theater. Each group is assigned to an uppercase letter and all of its members are identified with that letter. No two groups are assigned to the same letter.

Samples

Input

3 3
3.4
...
.2.
7 18
...4.2.
...45..
222..3.
...2..3
.24...2
...2.3.
22..3..
0 0

Output

ABB
ABB
ACC
AAAABCC
DDDDBEF
GHIIBEF
GHJKBEF
LLJKBMM
NOJPQQQ
NOJPRRR

题目大意:

        有一个n*n(n<20)的座位矩阵里坐着k(k\leqslant26)个研究小组。每个小组的座位都是矩形形状。输入每个小组组长的位置和该组的成员个数,找到一种可能的座位方案。

思路:

        以空地作为枚举对象,每次寻找枚举行和列,当矩形里恰好有一个数字,且数字等于矩形大小,就符号条件,当矩形大小超过9就剪枝。

#include<bits/stdc++.h>
using namespace std;
int n,k;
char mp[25][25],ans[25][25];
int dfs(int t,char ac) {
	while(ans[t/n][t%n]!='.')
		t++;
	if (t==n*n)
		return 1;
	int rw=t/n, cl=t%n, lim=n;
	for(int r=rw; r<n; r++) {
		for(int c=cl; c<lim; c++) {
			if (ans[r][c]!='.') {
				lim=c;
				break;
			}
			int sum=(r-rw+1)*(c-cl+1);
			if (sum>9) {
				lim=c;
				break;
			}
			int d=100;
			int flag=1;
			for (int i=rw; i<=r; i++) {
				for (int j=cl; j<=c; j++)
					if (mp[i][j]!='.') {
						if (d!=100) {
							flag=0;
							break;
						}
						d=mp[i][j]-'0';
					}
				if (!flag)
					break;
			}
			if (!flag) {
				lim=c;
				break;
			}
			if (d<sum) {
				lim=c;
				break;
			}
			if(d>sum)
				continue;
			for(int i=rw; i<=r; i++)
				for(int j=cl; j<=c; j++)
					ans[i][j]=ac;
			if(dfs(t+c-cl+1,ac+1))
				return 1;
			for(int i=rw; i<=r; i++)
				for (int j=cl; j<=c; j++)
					ans[i][j]='.';
		}
	}
	return 0;
}
int main() {
	cin>>n>>k;
	while(n&&k) {
		memset(ans,'.',sizeof(ans));
		for(int i=0; i<n; i++)
			cin>>mp[i];
		dfs(0,'A');
		for(int i=0; i<n; i++) {
			for(int j=0; j<n; j++)
				cout<<ans[i][j];
			cout<<endl;
		}
		cin>>n>>k;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值