昨天连续用JavaA了两道水题,感觉Java手感比较在了,然后打开这道POJ1048,一看数据就囧到了- -强悍的模拟题……估计Java写出来的话Java也已经很熟了- -,最后写出来确实发现了Java的一个问题,多维数组不能直接Clone……

先上题目:

Follow My Logic

**Time Limit:** 1000MS **Memory Limit:** 10000K
**Total Submissions:** 1384 **Accepted:** 400

Description

For this problem you will determine the output of a logic circuit composed of one or more inputs, zero or more dual-input AND/OR gates, and one output. The input circuits are drawn with standard ASCII characters. Circuit paths are represented using horizontal and vertical lines, and junctions. Horizontal lines are represented with dash characters (ASCII code 45 decimal), vertical lines with vertical bar characters (ASCII code 124 decimal), and junctions with plus characters (ASCII code 43 decimal). Inputs are represented using the capital letters A through Z, and the output is represented by a question mark. AND and OR gates are represented as shown in the leftmost entries in the figure below, and their orientation will always be exactly as shown. The location of the gate inputs and output is shown by the middle figure below. Finally, gate inputs or its output can be inverted, represented by a lowercase “oh”character (ASCII code 111 decimal) at the input or output location. The figure on the right below shows a simple but complete logic circuit.

        :\               :\                 -:\                 -o:\                       A-o:\

        : )              : >                 : )-                 : )o-                       : )o-?

        :/               :/                 -:/                 --:/                       B--:/

     AND gate          OR gate       Gate with inputs    An inverted top input          Two logic inputs

                                                 and an inverted output         and the output

Input

Circuits in the input will obey the following guidelines:

1.        The maximum size of the circuit picture is 100 by 100 characters.

2.        A path always travels in a straight line unless altered by a junction. At a junction, the path can and will make a ninety degree turn. Two junctions will not be horizontally or vertically adjacent.

3.        No paths will be “broken” That is, every path character is guaranteed to be adjacent on both sides to either another path character of the same type, a junction, a gate input, a gate output, a logic input, or the logic output.

4.        Circuit paths do not cross or intersect other paths.

5.        Gate inputs always approach horizontally from the left as shown above. Gate outputs always leave horizontally to the right as shown above.

6.        Inversions may only appear immediately adjacent to a gate input or output, and will always be preceded (in the case of an input) or followed (in the case of an output) by at least one dash as shown above.

The end of a logic diagram in the input is indicated by line containing only a single asterisk in the first column.  Following this are several lines which indicate the state of the inputs in the logic diagram.  Each of these lines is a string of twenty-six “0”(zero) or “1”characters, with the first position representing the state of input A, the second position representing the state of input B, etc.  Note that input values which are not actually used in the circuit may simply be ignored.  The list of input states is terminated by a line containing only a single asterisk character in the first column.

Following the asterisk which terminates the list of input states is another circuit diagram followed by a list of input states, which is then followed by another circuit diagram and list of input states, and so on until the end of the file.  The file will always contain at least one circuit and one set of inputs for that circuit.

Output

The program is to report the value of the output (0 or 1) of each logic circuit, one value per line, for each set of input values in the list which follows the circuit.  The list of outputs for each circuit should be separated by a single blank line.

Sample Input

A---:\
    : )---?
B---:/
*
00000000000000000000000000
10000000000000000000000000
01000000000000000000000000
11000000000000000000000000
*
A---+
    |
    +---:\
        : >o---:\
    +---:/     : )---?
    |      C--o:/
B---+
*
00000000000000000000000000
11100000000000000000000000
*

Sample Output

0

0

0

1
 


1

0


题目是一个计算逻辑电路的结果……字符画……

各种悲剧……

具体写的时候出了几次错,主要还是因为题目理解有问题,英语悲剧……

先是没有理解o,然后就是输入的时候可能出现

A

|

|

+--....

这样子,悲剧的……

最后还是AC了,Java写了140行,感觉还不错,200多MS,速度感觉有点慢




import java.util.*;

public class Main {
	static char a[][] = new char[110][110];
	static char value[] = new char[30];
	static final int xx[] = {0, 1, 0, -1};
	static final int yy[] = {1, 0, -1, 0};
	static final char cc[] = {'-', '|', '-', '|'};

	static void scan(int x, int y){
		int d = 0;
		for(int i=0; i<4; ++i){
			if(a[x + xx[i]][y + yy[i]] == cc[i]){
				d = i;
				break;
			}
		}
		while (true) {
			while (a[x + xx[d]][y + yy[d]] == cc[d]) {
				a[x + xx[d]][y + yy[d]] = a[x][y];
				x += xx[d];
				y += yy[d];
			}
			if(a[x + xx[d]][y + yy[d]] == 'o'){
				a[x + xx[d]][y + yy[d]] = (char)(a[x][y] == 0?1:0);
				x += xx[d];
				y += yy[d];
				d = 0;
			}
			if(a[x + xx[d]][y + yy[d]] == ':'){
				break;
			}
			if(a[x + xx[d]][y + yy[d]] == '?' ){
				a[x + xx[d]][y + yy[d]] = a[x][y];
				break;
			}
			if(a[x + xx[d]][y + yy[d]] == '+'){
				a[x + xx[d]][y + yy[d]] = a[x][y];
				x += xx[d];
				y += yy[d];
				d = (d+1)%4;
				if(a[x + xx[d]][y + yy[d]] == cc[d]){
					a[x + xx[d]][y + yy[d]] = a[x][y];
					x += xx[d];
					y += yy[d];
					continue;
				}
				d = (d+2)%4;
				if(a[x + xx[d]][y + yy[d]] == cc[d]){
					a[x + xx[d]][y + yy[d]] = a[x][y];
					x += xx[d];
					y += yy[d];
					continue;
				}
			}
		}

	}

	static void Solve()
	{
		int ansx = 0, ansy = 0;
		for (int i = 0; i < a.length; ++i) {
			for (int j = 0; j < a[i].length; ++j) {
				if (a[i][j] == '?') {
					ansx = i;
					ansy = j;
				}
			}
		}
		for (int i = 0; i < a.length; ++i) {
			for (int j = 0; j < a[i].length; ++j) {
				if (a[i][j] >= 'A' && a[i][j] <= 'Z') {
					a[i][j] = (char) (value[a[i][j] - 'A'] - '0');
					scan(i, j);
				}
			}
		}
		while (a[ansx][ansy] == '?') {
			for (int i = 0; i < a.length; ++i) {
				for (int j = 0; j < a[i].length; ++j) {
					if ((a[i][j] == '>') && a[i - 1][j - 3] < 2 && a[i + 1][j - 3] < 2) {
						a[i][j] = (char) (a[i - 1][j - 3] | a[i + 1][j - 3]);
						if(a[ansx][ansy] != '?')
						break;
						scan(i, j);
					}
					if ((a[i][j] == ')') && a[i - 1][j - 3] < 2 && a[i + 1][j - 3] < 2) {
						a[i][j] = (char) (a[i - 1][j - 3] & a[i + 1][j - 3]);
						if(a[ansx][ansy] != '?')
						break;
						scan(i, j);
					}
				}
				if(a[ansx][ansy] != '?')
				break;
			}
		}
		System.out.println((int)(a[ansx][ansy]));
	}

	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		boolean first = true;
		while (in.hasNext()) {
			if(first)
			first = false;
			else
			System.out.println();
			for (int i = 0; i < 110; ++i)
			for (int j = 0; j < 110; ++j)
			a[i][j] = ' ';
			String tmp;
			int n = 0;
			while (true) {
				tmp = in.nextLine();
				if (tmp.startsWith("*"))
				break;
				++n;
				for(int i=1; i<=tmp.length(); ++i)
				a[n][i] = tmp.charAt(i-1);

			}
			char bak[][] = new char[110][110];
			for(int i=0; i<a.length; ++i)
			bak[i] = a[i].clone();
			while (true) {
				for(int i=0; i<a.length; ++i)
				a[i] = bak[i].clone();
				tmp = in.nextLine();
				if (tmp.startsWith("*"))
				break;
				value = tmp.toCharArray();
				Solve();
			}
		}
	}
}