[luoguP1013] 进制位(搜索)

传送门

 

纯搜索,无优化!

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> 
#define N 10

using namespace std;

int n, m, f;
int c[300], b[N];
string map[N][N];

bool check()
{
	int i, j, k, x;
	for(i = 1; i < n; i++)
		for(j = 1; j < n; j++)
		{
			x = 0; 
			for(k = 0; k < map[i][j].length(); k++)
				x = x * m + c[map[i][j][k]];
			if(x != c[map[0][j][0]] + c[map[i][0][0]]) return 0;
		}
	return 1;
}

inline void dfs(int k)
{
	if(k == n)
	{
		if(check())
		{
			for(int i = 1; i < n; i++) cout << map[0][i][0] << "=" << c[map[0][i][0]] << " ";
			cout << endl << m << endl;
			exit(0);
		}
	}
	for(int i = 0; i < m; i++)
		if(!b[i])
		{
			b[i] = 1;
			c[map[0][k][0]] = i;
			dfs(k + 1);
			b[i] = 0;
		}
}

int main()
{
	int i, j;
	cin >> n;
	for(i = 0; i < n; i++)
		for(j = 0; j < n; j++)
			cin >> map[i][j];
	for(m = n - 1; m <= 10; m++)
	{
		memset(b, 0, sizeof(b));
		dfs(1);
	}
	cout << "ERROR!\n";
	return 0;
}

  

posted @ 2017-08-02 09:15  zht467  阅读(124)  评论(0编辑  收藏  举报