文件中包含初始的盘件描述,一共有10行,第i行有i个字符。如果第i行的第j个字符是字母”A”至”L”中的一个,则表示第i行第j列的格子上已经放了零件,零件的编号为对应的字母。如果第i行的第j个字符是”.”,则表示第i行第j列的格子上没有放零件。输入保证预放的零件已摆放在盘件中。
标签:
文件中包含初始的盘件描述,一共有10行,第i行有i个字符。如果第i行的第j个字符是字母”A”至”L”中的一个,则表示第i行第j列的格子上已经放了零件,零件的编号为对应的字母。如果第i行的第j个字符是”.”,则表示第i行第j列的格子上没有放零件。输入保证预放的零件已摆放在盘件中。
如果能找到解,向输出文件打印10行,为放完全部12个零件后的布局。其中,第i行应包含i个字符,第i行的第j个字符表示第i行第j列的格子上放的是哪个零件。如果无解,输出单独的一个字符串‘No solution’(不要引号,请注意大小写)。所有的数据保证最多只有一组解。
#include <cstdio>
#include <cstdlib>
#define MAXN 15
#define DFS_next if (y == x) DFS(x + 1, 1); else DFS(x, y + 1)
const int a[100][6] = {
{0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0}, {0, 1, 1, 0, 0, 0}, {0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0}, {0, 0, 1, 2, 0, 0}, {0, 1, 1, 1, 0, 0}, {0, 1, 2, 2, 0, 0},
{0, 1, 1, 1, 0, 0}, {0, 0, 1, 2, 0, 0}, {0, 0, 0, 1, 0, 0}, {0, 1, 2, 2, 0, 0},
{0, 0, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {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0, 0, 0, 0, 1, 0},
{0, 0, 1, 2, 3, 0}, {0, 1, 0, 0, 0, 0}, {0, 1, 2, 3, 3, 0}, {0, 0, 0, 0,-1, 0}
};
const int tot[100] = {0, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
const int inv[100] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
int vis[MAXN][MAXN], map[MAXN][MAXN], use[MAXN];
bool out(int o) { return o < 1 || o > 10; }
bool check(int o, int x, int y)
{
if (inv[o]) return 0;
for (int i = 1; i < tot[o]; i++)
{
int dx = x + a[o][i], dy = y + b[o][i];
if (out(dx) || out(dy)) return 0;
if (vis[dx][dy]) return 0;
}
return 1;
}
void DFS(int x, int y)
{
if (x == 11 && y == 1)
{
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= i; j++) printf("%c",map[i][j] + ‘A‘ - 1);
printf("\n");
}
exit(0);
}
if (vis[x][y]) { DFS_next; }
else for (int i = 1; i <= 12; i++)
{
if (use[i]) continue;
for (int j = 0; j <= 7; j++)
{
int o = i * 8 - j;
if (check(o, x, y))
{
for (int k = 0; k < tot[o]; k++)
vis[x + a[o][k]][y + b[o][k]] = 1, map[x + a[o][k]][y + b[o][k]] = i;
use[i] = 1; DFS_next; use[i] = 0;
for (int k = 0; k < tot[o]; k++) vis[x + a[o][k]][y + b[o][k]] = 0;
}
}
}
}
char ch[MAXN];
int main()
{
freopen("game.in", "r", stdin);
freopen("game.out", "w", stdout);
for (int i = 1; i <= 10; i++)
{
scanf("%s",ch);
for (int j = 0; j <= i - 1; j++)
{
if (ch[j] == ‘.‘) map[i][j + 1] = 0;
else map[i][j + 1] = ch[j] - ‘A‘ + 1, vis[i][j + 1] = 1, use[ch[j] - ‘A‘ + 1] = 1;
}
}
for (int i = 1; i <= 10; i++)
for (int j = 10; j > i; j--) vis[i][j] = 1;
DFS(1,1);
printf("No solution");
return 0;
}
标签:
原文地址:http://www.cnblogs.com/jinkun113/p/4876150.html