UVa211 - The Domino Effect(DFS)
来源:互联网 发布:清华社会科学学院 知乎 编辑:程序博客网 时间:2024/05/21 10:28
The Domino Effect
A standard set of Double Six dominoes contains 28 pieces (called bones) each displaying two numbers from 0 (blank) to 6 using dice-like pips. The 28 bones, which are unique, consist of the following combinations of pips:
Bone # Pips Bone # Pips Bone # Pips Bone # Pips 1 0 | 0 8 1 | 1 15 2 | 3 22 3 | 6 2 0 | 1 9 1 | 2 16 2 | 4 23 4 | 4 3 0 | 2 10 1 | 3 17 2 | 5 24 4 | 5 4 0 | 3 11 1 | 4 18 2 | 6 25 4 | 6 5 0 | 4 12 1 | 5 19 3 | 3 26 5 | 5 6 0 | 5 13 1 | 6 20 3 | 4 27 5 | 6 7 0 | 6 14 2 | 2 21 3 | 5 28 6 | 6
All the Double Six dominoes in a set can he laid out to display a 7 x 8 grid of pips. Each layout corresponds at least one ``map" of the dominoes. A map consists of an identical 7 x 8 grid with the appropriate bone numbers substituted for the pip numbers appearing on that bone. An example of a 7 x 8 grid display of pips and a corresponding map of bone numbers is shown below.
7 x 8 grid of pips map of bone numbers 6 6 2 6 5 2 4 1 28 28 14 7 17 17 11 11 1 3 2 0 1 0 3 4 10 10 14 7 2 2 21 23 1 3 2 4 6 6 5 4 8 4 16 25 25 13 21 23 1 0 4 3 2 1 1 2 8 4 16 15 15 13 9 9 5 1 3 6 0 4 5 5 12 12 22 22 5 5 26 26 5 5 4 0 2 6 0 3 27 24 24 3 3 18 1 19 6 0 5 3 4 2 0 3 27 6 6 20 20 18 1 19
Write a program that will analyze the pattern of pips in any 7 x 8 layout of a standard set of dominoes and produce a map showing the position of all dominoes in the set. If more than one arrangement of dominoes yield the same pattern, your program should generate a map of each possible layout.
Input
The input file will contain several of problem sets. Each set consists of seven lines of eight integers from 0 through 6, representing an observed pattern of pips. Each set is corresponds to a legitimate configuration of bones (there will be at least one map possible for each problem set). There is no intervening data separating the problem sets.
Output
Correct output consists of a problem set label (beginning with Set #1) followed by an echo printing of the problem set itself. This is followed by a map label for the set and the map(s) which correspond to the problem set. (Multiple maps can be output in any order.) After all maps for a problem set have been printed, a summary line stating the number of possible maps appears.
At least three lines are skipped between the output from different problem sets while at least one line separates the labels, echo printing, and maps within the same problem set.
A sample input file of two problem sets along with the correct output are shown.
Sample Input
5 4 3 6 5 3 4 60 6 0 1 2 3 1 13 2 6 5 0 4 2 05 3 6 2 3 2 0 64 0 4 1 0 0 4 15 2 2 4 4 1 6 55 5 3 6 1 2 3 14 2 5 2 6 3 5 45 0 4 3 1 4 1 11 2 3 0 2 2 2 21 4 0 1 3 5 6 54 0 6 0 3 6 6 54 0 1 6 4 0 3 06 5 3 6 2 1 5 3
Sample Output
Layout #1: 5 4 3 6 5 3 4 6 0 6 0 1 2 3 1 1 3 2 6 5 0 4 2 0 5 3 6 2 3 2 0 6 4 0 4 1 0 0 4 1 5 2 2 4 4 1 6 5 5 5 3 6 1 2 3 1Maps resulting from layout #1 are: 6 20 20 27 27 19 25 25 6 18 2 2 3 19 8 8 21 18 28 17 3 16 16 7 21 4 28 17 15 15 5 7 24 4 11 11 1 1 5 12 24 14 14 23 23 13 13 12 26 26 22 22 9 9 10 10There are 1 solution(s) for layout #1.Layout #2: 4 2 5 2 6 3 5 4 5 0 4 3 1 4 1 1 1 2 3 0 2 2 2 2 1 4 0 1 3 5 6 5 4 0 6 0 3 6 6 5 4 0 1 6 4 0 3 0 6 5 3 6 2 1 5 3Maps resulting from layout #2 are: 16 16 24 18 18 20 12 11 6 6 24 10 10 20 12 11 8 15 15 3 3 17 14 14 8 5 5 2 19 17 28 26 23 1 13 2 19 7 28 26 23 1 13 25 25 7 4 4 27 27 22 22 9 9 21 21 16 16 24 18 18 20 12 11 6 6 24 10 10 20 12 11 8 15 15 3 3 17 14 14 8 5 5 2 19 17 28 26 23 1 13 2 19 7 28 26 23 1 13 25 25 7 21 4 27 27 22 22 9 9 21 4There are 2 solution(s) for layout #2.
import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.FileReader;import java.io.PrintWriter;import java.io.OutputStreamWriter;import java.io.StreamTokenizer;import java.io.IOException;class Main{public static final boolean DEBUG = false;public StreamTokenizer tokenizer;public PrintWriter cout;BufferedReader cin;public final int ROW = 7;public final int COL = 8;public final int SIZE = 30;public int[][] grid = new int[ROW][COL];public boolean[] vis = new boolean[SIZE];public int[][] flag = new int[ROW][COL];public int[][] map = new int[ROW][ROW];public int[][] dir = {{0, 1}, {1, 0}};public int cnt;public void init() throws IOException{if (DEBUG) {cin = new BufferedReader(new FileReader("d:\\OJ\\uva_in.txt"));} else {cin = new BufferedReader(new InputStreamReader(System.in));}tokenizer = new StreamTokenizer(cin);cout = new PrintWriter(new OutputStreamWriter(System.out));int c = 1;for (int i = 0; i < ROW; i++) {for (int j = i; j < ROW; j++) {map[i][j] = map[j][i] = c++;}}}public int next() throws IOException{tokenizer.nextToken();if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {return (int)tokenizer.nval;} else return -1;}public boolean input() throws IOException{for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++) {int c = next();if (c == -1) return false;grid[i][j] = c;}}return true;}public void dfs(int x, int y){if (y >= COL) {x++;y = 0;if (x >= ROW) {cnt++;print();return;}}if (flag[x][y] != 0) dfs(x, y + 1);else {for (int i = 0; i < 2; i++) {int dx = x + dir[i][0];int dy = y + dir[i][1];if (dx >= ROW || dy >= COL || flag[dx][dy] != 0) continue;int c = map[grid[x][y]][grid[dx][dy]];if (vis[c]) continue;vis[c] = true;flag[x][y] = flag[dx][dy] = c;dfs(x, y + 1);vis[c] = false;flag[x][y] = flag[dx][dy] = 0;}}}public void print(){for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++) {cout.printf("%4d", flag[i][j]);}cout.println();}cout.println();}public void solve(int cas){if (cas != 1) {cout.println();cout.println();cout.println();}cout.println("Layout #" + cas + ":");cout.println();for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++) {cout.printf("%4d", grid[i][j]);}cout.println();}cnt = 0;cout.println();cout.println("Maps resulting from layout #" + cas + " are:");cout.println();dfs(0, 0);cout.println("There are " + cnt + " solution(s) for layout #" + cas + ".");cout.flush();}public static void main(String[] args) throws IOException{Main solver = new Main();solver.init();int cas = 1;while (solver.input()) {solver.solve(cas);cas++;}}}
- UVa211 - The Domino Effect(DFS)
- UVa211 - The Domino Effect
- Uva211 The Domino Effect 【dfs回溯】【习题7-3】
- 习题7-3 多米诺效应(The Domino Effect, ACM/ICPC World Finals 1991, UVa211)
- uva 211 - The Domino Effect(DFS)
- UVa 211 - The Domino Effect (DFS)
- UVa 211 - The Domino Effect [DFS剪枝]
- UVA 211 - The Domino Effect(dfs+剪枝)
- The Domino Effect (UVA
- The Domino Effect UVA
- The Domino Effect UVA
- uva-211-The Domino Effect
- UVA 211 The Domino Effect
- UVA 211 The Domino Effect
- uva 211 The Domino Effect
- 211 - The Domino Effect(暴力)
- UVA 211(p215)----The Domino Effect
- UVA 211 The Domino Effect 【搜索】
- 第七周项目二
- 嵌入式博文
- TCP建立连接为什么是三次握手而不是两次握手
- JBPM4.4(二)——初步运用
- Windows环境下配置Objective-C的编译环境
- UVa211 - The Domino Effect(DFS)
- Extjs4.2与struts后台交互弹框
- Linux系统下利用crontab实现程序的鲁棒性
- 输出日期时间--友元函数
- Date类和SimpleDateFormat类
- Find your present!(异或运算)
- 闲来无事---写了一个生成验证码的代码
- 第七周项目三
- Java垃圾回收机制