跳到正文

剪贴板

剪贴板 fcq0x0r6

原文
剪贴板信息

剪贴板信息

收录
更新

AtCoder ABC302 C - Almost Equal 代码

C++
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int N = 10;

inline int read()
{
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
	return x * f;
}

int n, m;
string a[N];
int f[N];		// 排列
bool st[N];		// 放置重复元素

bool Chk(string a, string b)
{
	int t = 0;
	for (int i = 0; i < a.size(); i ++ )
		if (a[i] != b[i])
		{
			t ++ ;
			if (t == 2) return false;
		}

	return true;
}

bool chk()
{
	for (int i = 1; i < n; i ++ )
		if (!Chk(a[f[i]], a[f[i + 1]]))
			return false;

	return true;
}

void dfs(int u)
{
	if (u == n + 1)
	{
		if (chk())		// 如果全部满足条件,输出 Yes 并结束程序
		{
			puts("Yes");
			exit(0);
		}
		return;
	}

	for (int i = 1; i <= n; i ++ )
		if (!st[i])
		{
			f[u] = i, st[i] = true;		// 选择这个元素
			dfs(u + 1);		// 搜索
			st[i] = false;		// 恢复现场
		}
}

signed main()
{
	n = read(), m = read();		// 读入

	for (int i = 1; i <= n; i ++ )
		cin >> a[i];

	dfs(1);

	puts("No");

	return 0;
}