sgu 125解题记录

7 篇文章 0 订阅

125. Shtirlits

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

There is a checkered field of size N x N cells (1 Ј N Ј 3). Each cell designates the territory of a state (i.e. N2 states). Each state has an army. Let A [i, j] be the number of soldiers in the state which is located on i-th line and on j-th column of the checkered field (1£i£N, 1£j£N, 0 £  A[i, j] £  9). For each state the number of neighbors, B [i, j], that have a larger army, is known. The states are neighbors if they have a common border (i.e. 0 £  B[i, j]  £  4). Shtirlits knows matrix B. He has to determine the number of armies for all states (i.e. to find matrix A) using this information for placing forces before the war. If there are more than one solution you may output any of them.

Input

The first line contains a natural number N. Following N lines contain the description of matrix B - N numbers in each line delimited by spaces.

Output

If a solution exists, the output file should contain N lines, which describe matrix A. Each line will contain N numbers delimited by spaces. If there is no solution, the file should contain NO SOLUTION.

Sample Input

3
1 2 1
1 2 1
1 1 0

Sample Output

1 2 3
1 4 5
1 6 7

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=125


题目大意:(参考nocow)

有一个 N x N 大小的方阵 (1 <= N <=3)。把每小块指定成一个州的领土 (N*N 个州)。

每个州有一支部队。设A[i, j] 表示第i行j列的州的战士数量 (1 <= i <= N, 1 <= j <= N, 0 <= A[i, j] <= 9)。

设B[i, j]表示第i行j列的州的邻居中军队人数比它的军队人数大的个数。B数组是已知的。

两个州相邻就是说它们有公共的边(显然0 <= B[i, j] <= 4)。 Shtirlits 知道 B。

它需要利用已知的信息算出每个州有多大的军队 (也就是求矩阵 A) 如果有多组解,输出任意一个就可以了。

解题思路记录:

第一想法——搜索:

裸搜,枚举每个格子的状态,最坏的情况要枚举9^9种情况,加上最后判断改图是否符合b数组需要一定的计算量,总共有10^9规模的计算量,超时。

试试剪枝,用b[i][j]减少每个格子的枚举量,粗略算一下最坏情况至少10^8规模计算量,超时。

第二想法——DP:

状态压缩,以格子比周围的哪些格子小为记录,第一行有2^(2+3+2)=128,第二行有2^(3+4+3)=1024,第三行有2^(2+3+2)=128,总共不到1500个状态,似乎

可行。 试着写出状态转移方程,由于可以上下左右转移状态,无法通过某种方法弄成只会存在“左->右,上->下”的状态转移,DP这方面自己实在是想不出来了T_T。


那就写个搜索试试

sub1 TLE3;

看下可不可以再剪枝,似乎当一个格子确定了a[i][j],a[i][j-1]所对应的b[i][j]就可以求出来了,看一下是否跟数据给的一样,只从第二排开始排除。

sub2 AC 15ms 2kb

额,看来一下减去好多状态,这种剪枝都可以过啊= =,好水的样子。又发现之前的第一个剪枝是错误的,因为b[i][j]只是表明周围有军队数量比你高,但不代表你

的军队数量就一定是0~9-b[i][j],因为周围的军队数量可能一样,,所以第一个剪枝错误(居然还过了)。

sub3 AC 15ms 6kb

改了理论上的错误,也顺便再减少了一些程序上的繁琐,居然内存增加了4kb。

解题报告:

采用搜索策略,对每一个格子的军队个数进行枚举。采用剪枝:

从第二排开始,每当一个格子的军队个数确定,那么该格子的上一排对应的格子的b[i][j]也应当确定下来了,可以与答案进行对比,如若符合则继续。

代码:

#include <cstdio>

const int dx[5]={0,1,0,-1,0};
const int dy[5]={0,0,1,0,-1};

int n, a[5][5], b[5][5];

void init() {
	scanf("%d",&n);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &b[i][j]);
}

bool check(int i, int j) {  //检查格子[i][j]的b[i][j]是否符合题目要求的判断函数
	int tmp=0;

	for (int k = 1; k <= 4; k++)
		if (a[i+dx[k]][j+dy[k]] > a[i][j])
			tmp++;

	if (tmp != b[i][j])  return(false);
	
	return(true); 
}

bool dfs(int x, int y) {
	int tmpy=y+1;
	int tmpx=x;

	if (x == n+1) {  //当全部都枚举完
		for (int j = 1; j <= n; j++)  //由于前两排在枚举中已经检查过了,只需要检查最后一排
			if (!check(n,j))
				return false;

		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++)
				printf("%d ", a[i][j]);
			printf("\n");
		}
		return true;
	}

	if (tmpy > n) {
		tmpy=1;
		tmpx++;
	}
	for (int i = 0; i <= 9; i++) {
		a[x][y]=i;

		if (x != 1)  //判断该格子的上一排对应的格子的b[i][j]是否符合题目要求的b[i][j]
			if (!check(x-1,y)) continue;

		if (dfs(tmpx,tmpy))  return true;
	}

	return(false);
}

void solve() {
	if (!dfs(1,1))
		printf("NO SOLUTION\n");
}

int main() {
	init();
	solve();
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值