UVa11507 - Bender B. Rodríguez Problem

Bender B. Rodríguez Problem 

Bender is a robot built by Mom's Friendly Robot Company at its plant in Tijuana, Mexico in 2996. He is a Bending-Unit 22, serial number 2716057 and chassis number 1729. He was created for the task of bending metal wires.


Bender needs to bend a wire of length L (L$ \geq$2 an integer). The wire is represented in the Bender's brain (a MOS Technology 6502 microprocessor) as a line stucked in the origin of a tridimensional cartesian coordinate system, and extended along the x positive axis (+x), so that the fixed extreme of the wire is in the coordinate (0,0,0) and the free extreme of the wire is in the coordinate (L,0,0).

\epsfbox{p11507a.eps}


Bender bends the wire at specific points, starting at the point (L-1,0,0) and ending at the point (1,0,0). For each i from L-1 to 1, Bender can take one of the following decisions:

  • Not to bend the wire at point (i,0,0).
  • To bend the wire at point (i,0,0) an angle of $ {\frac{​{\pi}}{​{2}}}$ to be parallel to the axis +y, -y, +z or -z.


For example, if L=3 and Bender bends the wire at (2,0,0) on the +y axis direction, and at (1,0,0) on the -y axis direction, the result would be:

\epsfbox{p11507b.eps}


Given a sequence of bends, you must determine what direction is pointed by the last segment of the wire (+x in the example). You can suppose that the wire can intercept itself, after all it is the future!

Input 

The first line of each test case gives an integer L (2$ \leq$L$ \leq$100000) indicating the length of the wire.


The second line of each test case contains the L-1 decisions taken by Bender at each point, separated by spaces. The j-th decision in the list (for each 1$ \leq$j$ \leq$L-1) corresponds to the decision taken at the point (L-j,0,0), and must be one of the following:

  • No if the wire isn't bended at point (L-j,0,0).
  • +y if the wire is bended at point (L-j,0,0) on the +y axis.
  • -y if the wire is bended at point (L-j,0,0) on the -y axis.
  • +z if the wire is bended at point (L-j,0,0) on the +z axis.
  • -z if the wire is bended at point (L-j,0,0) on the -z axis.

The end of the input is indicated when L=0.

Output 

For each case in the input, print one line with the direction pointed by the last segment of the wire, +x, -x, +y, -y, +z or -z depending on the case.

Sample Input 

3
+z -z
3
+z +y
2
+z
4
+z +y +z
5
No +z No No
0

Sample Output 

+x
+z
+z
-x
+z

1、当最后的一端与x轴平行时,下一步转向哪个方向就是哪个方向

2、不与x轴平行时,当前方向与下一步方向一致时,正负方向是异或的结果,但是与x平行

import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;

public class Main
{
	private static final boolean DEBUG = false;
	private BufferedReader cin;
	private PrintWriter cout;
	private StreamTokenizer tokenizer;
	private int n;
	
	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));
			}

			cout = new PrintWriter(new OutputStreamWriter(System.out));
			
			tokenizer = new StreamTokenizer(cin);
			tokenizer.resetSyntax();
			tokenizer.wordChars('a', 'z');
			tokenizer.wordChars('A', 'Z');
			tokenizer.wordChars('0', '9');
			tokenizer.wordChars(128 + 32, 255);
			tokenizer.wordChars('-', '-');
			tokenizer.wordChars('+', '+');
			tokenizer.whitespaceChars(0, ' ');
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private 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
				return tokenizer.sval;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean input() 
	{
		n = Integer.parseInt(next());
		
		if (n == 0) return false;
		
		return true;
	}
	
	public void solve() 
	{
		boolean sign = true;
		char axis = 'x';
		
		for (int i = 0; i < n - 1; i++) {
			String s = next();
			if ("No".compareTo(s) == 0) continue;
			if (axis == 'x') {
				sign = !(sign ^ (s.charAt(0) == '+'));
				axis = s.charAt(1);
			} else if (s.charAt(1) == axis) {
				sign = sign ^ (s.charAt(0) == '+');
				axis = 'x';
			}
		}
		
		if (sign) cout.print("+");
		else cout.print("-");
		cout.println(axis);
		cout.flush();
	}

	
	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		while (solver.input()) {
			solver.solve();
		}
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值