UVa 211 - The Domino Effect (DFS)
来源:互联网 发布:python服务器端开发 编辑:程序博客网 时间:2024/06/06 03:16
题意
多米诺骨牌有28种型号,每种型号都由两个pip组成。
比如bone 1 是由 0和0组成的,bone 18是由2和6组成的。
现在给出一个pip组成的图,要求输出所有能组成的bone型号排列。
我会说我看懂题目花了一个小时吗TAT
对于每种型号的骨牌,要么横着放,要么竖着放。一直往右DFS + 回溯即可。
代码
#include <stack>
#include <cstdio>
#include <list>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 4e5 + 10;
const int MOD = 1000007;
const int dir[][2] = { {1, 0}, {0, 1} };
int cases = 0;
typedef pair<int, int> pii;
typedef vector<int>::iterator viti;
typedef vector<pii>::iterator vitii;
const int row = 7, col = 8;
int mp[row][col], cvt[row][col], vis[30], pip[row][col];
int cnt;
bool Input()
{
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++) if (scanf("%d", &pip[i][j]) == -1) return false;
return true;
}
void Init()
{
int num = 1;
cnt = 0;
for (int i = 0; i < row; i++)
for (int j = i; j < row; j++) cvt[i][j] = cvt[j][i] = num++;
}
void Output(int p[7][8])
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++) printf("%4d", p[i][j]);
puts("");
}
puts("");
}
void DFS(int x, int y)
{
if (y == col)
{
x++, y = 0;
if (x == row)
{
cnt++;
Output(mp);
return;
}
}
if (mp[x][y]) DFS(x, y + 1);
else
{
for (int i = 0; i < 2; i++)
{
int xx = x + dir[i][0], yy = y + dir[i][1];
if (xx < row && yy < col && !mp[xx][yy])
{
int bone = cvt[pip[x][y]][pip[xx][yy]];
if (vis[bone]) continue;
mp[x][y] = mp[xx][yy] = bone;
vis[bone] = 1;
DFS(x, y + 1);
vis[bone] = 0;
mp[x][y] = mp[xx][yy] = 0;
}
}
}
}
int main()
{
//ROP;
Init();
while (1)
{
cnt = 0;
if (!Input()) break;
if (cases) printf("\n\n\n");
printf("Layout #%d:\n\n", ++cases);
Output(pip);
printf("Maps resulting from layout #%d are:\n\n", cases);
DFS(0, 0);
printf("There are %d solution(s) for layout #%d.\n", cnt, cases);
}
return 0;
}
0 0
- uva 211 - The Domino Effect(DFS)
- UVa 211 - The Domino Effect (DFS)
- UVa 211 - The Domino Effect [DFS剪枝]
- UVA 211 - The Domino Effect(dfs+剪枝)
- uva-211-The Domino Effect
- UVA 211 The Domino Effect
- UVA 211 The Domino Effect
- uva 211 The Domino Effect
- The Domino Effect (UVA
- The Domino Effect UVA
- The Domino Effect UVA
- UVA 211(p215)----The Domino Effect
- UVA 211 The Domino Effect 【搜索】
- UVa211 - The Domino Effect(DFS)
- UVa 211 - The Domino Effect(枚举+回溯)
- Uva211 The Domino Effect 【dfs回溯】【习题7-3】
- 211 - The Domino Effect(暴力)
- UVa211 - The Domino Effect
- Redis操作的封装类
- JDK自带工具之jhat
- poj2553 The Bottom of a Graph (Tarjan)
- Robotium 专题(三): 编写 Case 时用到的几个有用的方法
- 世界将如此O2O(大势所趋)
- UVa 211 - The Domino Effect (DFS)
- JDK自带工具之jstack
- 【ACM打卡】ZOJ 3019
- debian sendmail安装
- 函数实现-memcpy-strcpy-memmove-memset
- Tomcat学习之Tomcat架构
- 解决adb not responding you can wait more
- XML解析器——DOM
- mysql 如何设置自动增长序列 sequence(二) 在实际中调用