P1092 虫食算 —— DFS与进制处理






import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static int N;
	static int[] num = new int[30];
	static boolean[] used = new boolean[30];
	static char[][] s = new char[5][30];

	static int x(char ch) {
		return ch - 'A' + 1;
	}

	static void dfs(int x, int y, int carry) { 
		/*
		 * x:列
		 * y:行
		 * carry:进位
		 */
		if (x == 0) {
			if (carry == 0) {//最后一列不能有进位
				for (int i = 1; i < N; i++) {
					System.out.print(num[i] + " ");
				}
				System.out.println(num[N]);
			}
			return;
		}

		for (int i = x - 1; i >= 1; i--) {
			int x1 = num[x(s[1][i])]; //第一行代表的数字
			int x2 = num[x(s[2][i])]; //第二行代表的数字
			int x3 = num[x(s[3][i])]; //第三行代表的数字
			if (x1 == -1 || x2 == -1 || x3 == -1)
				continue;
			if ((x1 + x2) % N != x3 && (x1 + x2 + 1) % N != x3)
				return;
		}

		if (num[x(s[y][x])] == -1) {
			for (int i = N - 1; i >= 0; i--) {//倒着枚举
				if (!used[i]) {
					if (y != 3) { 
						num[x(s[y][x])] = i;
						used[i] = true;
						dfs(x, y + 1, carry);
						num[x(s[y][x])] = -1;
						used[i] = false;
					} else {
						int sum = num[x(s[1][x])] + num[x(s[2][x])] + carry;// 两个数加上它们的进位
						if (sum % N != i)
							continue;
						used[i] = true;
						num[x(s[3][x])] = i;
						dfs(x - 1, 1, sum / N);// 搜索下一列,进位需要改变
						num[x(s[3][x])] = -1;
						used[i] = false;
					}
				}
			}
		} else {
			if (y != 3) 
				dfs(x, y + 1, carry);
			else {
				int sum = num[x(s[1][x])] + num[x(s[2][x])] + carry;
				if (sum % N != num[x(s[3][x])]) 
					return;
				dfs(x - 1, 1, sum / N);// 搜索下一列,进位需要改变
			}
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		for (int i = 1; i <= 3; i++) {
			char[] temp = sc.next().toCharArray();
			for (int j = 0; j < temp.length; j++) {
				s[i][j + 1] = temp[j];
			}
		}
		Arrays.fill(num, -1);
		dfs(N, 1, 0);
	}
}



备忘:日后详更思路。(最近真的时间不够用aaaa,forgive me.)

### 关于SCOI2005 互不侵犯问题的DFS解法 对于SCOI2005 互不侵犯这一问题,采用深度优先搜索(DFS)的方法同样能够解决问题。这种方法通过尝试每一种可能的情况来寻找满足条件的结果。 #### DFS解题思路 在解决此问题时,DFS法会逐行放置国王,并确保任何两个国王之间不会互相攻击。具体来说: - 使用二进制数表示每一行的状态,其中`1`代表当前位置已放置国王,`0`则为空白。 - 对于每一个新的行,在所有未被先前行中的国王威胁的位置上尝试放置新国王。 - 如果当前行的所有列都遍历完毕,则回溯至上一行继续探索其他可能性。 - 当成功放置了指定数量的国王后,计数器加一;如果某次递归达到了最后一行且仍未完成目标,则返回并调整之前的决策。 为了提高效率,还需要提前计出哪些状态是合法的——即不存在连续两位都是`1`的状态,这可以通过简单的枚举实现[^4]。 #### Python代码实现 下面是一个基于上述逻辑编写的Python程序片段用于求解该问题: ```python def dfs(row, col_mask, left_diag, right_diag): global n, k, ans if row == n: if sum(bin(col)[2:].count('1') for col in cols) == k: ans += 1 return for i in range(1 << n): if bin(i).count('1') + sum(cols[:row]) > k: continue ok = ((~col_mask & ~left_diag & ~right_diag & (i)) == i) if not ok or '11' in bin(i): continue new_col_mask = col_mask | i new_left_diag = (left_diag | i) << 1 new_right_diag = (right_diag | i) >> 1 dfs(row + 1, new_col_mask, new_left_diag, new_right_diag) n, k = map(int, input().split()) cols = [0]*n ans = 0 dfs(0, 0, 0, 0) print(ans) ``` 这段代码定义了一个名为`dfs()`函数来进行深度优先搜索,它接收四个参数分别表示当前处理的是哪一行(`row`)、当前列上的占用情况(`col_mask`)、左斜线方向上的占用情况(`left_diag`)以及右斜线方向上的占用情况(`right_diag`)。全局变量`n`, `k`用来保存棋盘大小和要放置的国王数目,而`ans`则是最终答案的数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值