题目
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2515
题意
n*n矩阵中填入大写字母,n <= 10,要求从上到下从左到右字母序最小,相邻格子字母不同
思路
如刘书思路,状态比较小,不会导致矛盾。
感想
1. 状态较小
代码
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <tuple> #define LOCAL_DEBUG using namespace std; typedef pair<double, int> MyPair; const int MAXN = 11; int dx[4] = { 1, -1, 0, 0 }; int dy[4] = { 0, 0, 1, -1 }; char maze[MAXN][MAXN]; bool used[26]; void fillin(int i, int j, int n) { memset(used, 0, sizeof used); for (int di = 0; di < 4; di++) { int tx = i + dx[di]; int ty = j + dy[di]; if (0 > tx || tx >= n)continue; if (0 > ty || ty >= n)continue; if (maze[tx][ty] == '.')continue; used[maze[tx][ty] - 'A'] = true; } } int main() { #ifdef LOCAL_DEBUG freopen("C:\Users\Iris\source\repos\ACM\ACM\input.txt", "r", stdin); //freopen("C:\Users\Iris\source\repos\ACM\ACM\output.txt", "w", stdout); #endif // LOCAL_DEBUG int T; scanf("%d", &T); for (int ti = 1; ti <= T; ti++) { int n; scanf("%d", &n); for (int i = 0; i < n; i++)scanf("%s", maze + i); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (maze[i][j] != '.')continue; fillin(i, j, n); for (char c = 'A'; c <= 'Z'; c++) { if (!used[c - 'A']) { maze[i][j] = c; break; } } } } printf("Case %d: ", ti); for (int i = 0; i < n; i++)printf("%s ", maze[i]); } return 0; }