UVa229 - Scanner(枚举技巧)

来源:互联网 发布:淘宝店铺导航条怎么做 编辑:程序博客网 时间:2024/06/05 08:15

 Scanner 

A body scanner works by scanning a succession of horizontal slices through the body; the slices are imaged one at a time. The image slices can be reassembled to form a three dimensional model of the object. Write a program to construct a two dimensional image slice using data captured during the scan.

\epsfbox{p229.eps}

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 from sensor 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) 

210 10 6 4 6 8 13 15 11 60 1 2 2 2 2 4 5 5 6 7 6 5 6 6 5 5 6 6 3 2 2 1 02 4 5 5 7 6 7 10 10 10 7 3 3 5 50 0 1 3 4 4 4 4 3 4 5 7 8 8 9 9 6 4 4 2 0 0 0 010 10 6 4 6 8 13 15 11 60 1 2 2 2 2 4 5 5 6 7 6 5 6 6 5 5 6 6 3 2 2 1 02 4 5 5 7 6 7 10 10 10 7 3 3 5 50 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][N];f2 = new int[M][N];w1 = new int[M][N];w2 = new int[M][N];sum = new int[M][N];total = new int[M];g = new int[N][N];vis = new boolean[M][N];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);}}}


0 0
原创粉丝点击