UVa229 - Scanner(枚举技巧)
2014-04-28 21:47
281 查看
Scanner |
a program to construct a two dimensional image slice using data captured during the scan.
The scanner consists of four arrays of sensors arranged around a 10×15 matrix. Array 1 consists of 10 sensors pointing to the right, array 2 has 24 sensors pointing diagonally to the top
right, array 3 has 15 sensors pointing to the top and array 4 has 24 sensors pointing to the top left. Each sensor records the thickness of that portion of the object directly in front of that sensor.
Readings from the arrays of sensors are recorded in counterclockwise order. Within an array of sensors, data are also recorded counterclockwise. A complete scan consists of 73 readings.
Input
The input file begins with a line with an integer indicating the number of image slices to follow. For each image slice, there are separate lines with 10, 24, 15, and 24 integers representing sensor data fromsensor arrays 1 through 4 respectively. The order of the readings is indicated in the diagram.
Output
For each slice, your program should print 10 lines of 15 cells. To indicate that the cell represents a part of the object, print a hash character (#)for the cell; to indicate that the cell is not a part of the object, print a period (.). Between successive output image slices, print a blank line.
It is possible for the result of a scan to be ambiguous, in that case you will have to output a blank picture as shown in the sample output.
Sample Input (First one describing object above)
2 10 10 6 4 6 8 13 15 11 6 0 1 2 2 2 2 4 5 5 6 7 6 5 6 6 5 5 6 6 3 2 2 1 0 2 4 5 5 7 6 7 10 10 10 7 3 3 5 5 0 0 1 3 4 4 4 4 3 4 5 7 8 8 9 9 6 4 4 2 0 0 0 0 10 10 6 4 6 8 13 15 11 6 0 1 2 2 2 2 4 5 5 6 7 6 5 6 6 5 5 6 6 3 2 2 1 0 2 4 5 5 7 6 7 10 10 10 7 3 3 5 5 0 0 1 3 4 4 4 4 3 2 5 7 8 8 9 9 6 4 4 2 0 0 0 0
Sample Output
.##########.... .##########.... ....######..... ......####..... .......####..## .......######## #####..######## ############### ..#########..## ....######..... ............... ............... ............... ............... ............... ............... ............... ............... ............... ...............
import java.io.IOException; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.StreamTokenizer; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; public class Main { public static final boolean DEBUG = false; public static final int ROW = 10, COL = 15; public static final int N = 30; public static final int M = 4; public boolean[][] vis; public int[][] f1, f2, w1, w2, g, sum; public int[] total; public BufferedReader cin; public PrintWriter cout; public StreamTokenizer tokenizer; public void init() { try { if (DEBUG) { cin = new BufferedReader(new InputStreamReader( new FileInputStream("d:\\OJ\\uva_in.txt"))); } else { cin = new BufferedReader(new InputStreamReader(System.in)); } } catch (Exception e) { e.printStackTrace(); } tokenizer = new StreamTokenizer(cin); cout = new PrintWriter(new OutputStreamWriter(System.out)); f1 = new int[M] ; f2 = new int[M] ; w1 = new int[M] ; w2 = new int[M] ; sum = new int[M] ; total = new int[M]; g = new int ; vis = new boolean[M] ; total[0] = ROW; total[1] = ROW + COL - 1; total[2] = COL; total[3] = ROW + COL - 1; for (int i = 0; i < ROW; i++) sum[0][i] = COL; for (int i = 0; i < ROW + COL - 1; i++) { if (i < ROW) sum[1][i] = i + 1; else if (i >= COL - 1) sum[1][i] = ROW + COL - 1 - i; else sum[1][i] = ROW; } for (int i = 0; i < COL; i++) sum[2][i] = ROW; for (int i = 0; i < ROW + COL - 1; i++) { if (i < ROW) sum[3][i] = i + 1; else if (i >= COL - 1) sum[3][i] = ROW + COL - 1 - i; else sum[3][i] = ROW; } } public String next() { try { tokenizer.nextToken(); if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null; else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int) tokenizer.nval); else if (tokenizer.ttype == StreamTokenizer.TT_WORD) { return tokenizer.sval; } return null; } catch (Exception e) { e.printStackTrace(); return null; } } public int nextInt() { return Integer.parseInt(next()); } public void input() { for (int i = 0; i < M; i++) { Arrays.fill(f1[i], 0); Arrays.fill(f2[i], 0); Arrays.fill(w1[i], 0); Arrays.fill(w2[i], 0); Arrays.fill(vis[i], false); } for (int i = 0; i < N; i++) { Arrays.fill(g[i], -1); } for (int i = 0; i < M; i++) { for (int j = 0; j < total[i]; j++) { w1[i][j] = nextInt(); f1[i][j] = sum[i][j] - w1[i][j]; } } } public void fill2(int x, int y) { if (g[x][y] == 1) { w2[0][x]++; w2[1][x + y]++; w2[2][y]++; w2[3][y - x + ROW - 1]++; } else if (g[x][y] == 0) { f2[0][x]++; f2[1][x + y]++; f2[2][y]++; f2[3][y - x + ROW - 1]++; } } public void fill(int i, int j, int v) { vis[i][j] = true; if (i == 0) { for (int k = 0; k < COL; k++) { if (g[j][k] != -1) continue; g[j][k] = v; fill2(j, k); } } else if (i == 1) { for (int x = 0; x < ROW; x++) { for (int y = 0; y < COL; y++) { if (x + y != j) continue; if (g[x][y] != -1) continue; g[x][y] = v; fill2(x, y); } } } else if (i == 2) { for (int k = 0; k < ROW; k++) { if (g[k][j] != -1) continue; g[k][j] = v; fill2(k, j); } } else if (i == 3) { for (int x = 0; x < ROW; x++) { for (int y = 0; y < COL; y++) { if (y - x + ROW - 1 != j) continue; if (g[x][y] != -1) continue; g[x][y] = v; fill2(x, y); } } } } public boolean check() { for (int i = 0; i < M; i++) { for (int j = 0; j < total[i]; j++) { if (vis[i][j]) continue; if (w2[i][j] == w1[i][j]) { fill(i, j, 0); return true; } else if (f2[i][j] == f1[i][j]) { fill(i, j, 1); return true; } } } return false; } public void solve(int cas) { int ans = 0; while (check()) ans++; if (ans != 3 * (ROW + COL) - 2) { for (int i = 0; i < N; i++) { Arrays.fill(g[i], 0); } } for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (g[i][j] == 1) cout.print("#"); else cout.print("."); } cout.println(); } if (cas != 0) cout.println(); cout.flush(); } public static void main(String[] args) { Main solver = new Main(); solver.init(); int t = solver.nextInt(); while (t-- > 0) { solver.input(); solver.solve(t); } } }
相关文章推荐
- uva 229 - Scanner(枚举技巧)
- UVA 1508 Equipment(技巧枚举)
- UVA-1312 Cricket Field (技巧枚举)
- uva 12716 GCD XOR (数论)枚举技巧(筛法) + 思维
- UVA 1481 - Genome Evolution(枚举技巧)
- UVA 1432 - Fire-Control System(技巧枚举+线性扫描)
- UVA 12174 - Shuffle(技巧枚举+预处理)
- uva 11752 - The Super Powers(数论+枚举技巧)
- (UVA - 10976)Fractions Again?!(技巧,暴力枚举)
- uva 1618(枚举技巧)
- UVA 229 - Scanner(推理)
- UVA 11012 - Cosmic Cabbages(枚举技巧)
- UVA 1326 - Jurassic Remains(技巧枚举+位运算)
- uva 1382 Distant Galaxy(离散化+枚举技巧)
- uva 471 - Magic Numbers(枚举技巧)
- UVA 10483 - The Sum Equals the Product (枚举技巧)
- UVA - 11754 Code Feat CRT (big技巧枚举 small暴力)
- UVA 10730 - Antiarithmetic?(技巧枚举)
- UVa 1523 Helicopter 解题报告(枚举排列)
- UVA12563 Jin Ge Jin Qu hao(DP, 背包+技巧)