首页 > 专题系列 > Java解POJ > POJ 1493 Machined Surfaces [解题报告] Java
2013
11-09

POJ 1493 Machined Surfaces [解题报告] Java

Machined Surfaces

问题描述 :

An imaging device furnishes digital http://poj.org/images of two machined surfaces that eventually will be assembled in contact with each other. The roughness of this final contact is to be estimated.

A digital image is composed of the two characters, “X” and ” ” (space). There are always 25 columns to an image, but the number of rows, N, is variable. Column one (1) will always have an “X” in it and will be part of the left surface. The left surface can extend to the right from column one (1) as contiguous X’s.

Similarly, column 25 will always have an “X” in it and will be part of the right surface. The right surface can extend to the left from column 25 as contiguous X’s.

Digital-Image View of Surfaces


Left Right

XXXX XXXXX ←1
XXX XXXXXXX
XXXXX XXXX
XX XXXXXX
. .
. .
. .
XXXX XXXX
XXX XXXXX ←N
↑ ↑
1 25

In each row of the image, there can be zero or more space characters separating the left surface from the right surface. There will never be more than a single blank region in any row.

For each image given, you are to determine the total “void” that will exist after the left surface has been brought into contact with the right surface. The “void” is the total count of the spaces that remains between the left and right surfaces after theyhave been brought into contact.

The two surfaces are brought into contact by displacing them strictly horizontally towards each other until a rightmost “X” of the left surface of some row is immediately to the left of the leftmost “X” of the right surface of that row. There is no rotation or twisting of these two surfaces as they are brought into contact; they remain rigid, and only move horizontally.

Note: The original image may show the two surfaces already in contact, in which case no displacement enters into the contact roughness estimation.

输入:

The input consists of a series of digital images. Each image data set has the following format:

First line –

A single unsigned integer, N, with value greater than zero (0) and less than 13. The first digit of N will be the first character on a line.

Next N lines –

Each line has exactly 25 characters; one or more X’s, then zero or more spaces, then one or more X’s.

The end of data is signaled by a null data set having a zero on the first line of an image data set and no further data.

输出:

For each image you receive as a data set, you are to reply with the total void (count of spaces remaining after the surfaces are brought into contact). Use the default output for a single integer on a line.

样例输入:

4
XXXX                XXXXX
XXX               XXXXXXX
XXXXX                XXXX
XX                 XXXXXX
2
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXX              XX
0

样例输出:

4
0
0

解题代码:

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;

public class Main {
 static final int N = 100000;
 static String str[] = new String[N];
 static int n;
 static void start(){
	for(int i=0;i< n;++i) str[i] = new String();
  }

public static void main(String[]args) throws Exception{
	int i;
	Scanner cin = new Scanner(System.in);
	while(true){
		n = cin.nextInt();
		if(n<=0) break;
		cin.nextLine();
		for(i=0;i< n;++i){
			str[i] = cin.nextLine();
			//System.out.println(str[i]);
		}
		System.out.println(solve());
	}
  }

 public static int solve(){
  int i,j,max_length=0,length=str[0].length(),ans=0,cnt;
  for(i=0;i< n;++i){
	ans = 0;
	for(j=0;j< length;++j){
		if(str[i].charAt(j)=='X')
			++ans;
	}
	max_length = ans>max_length?ans:max_length;
}
ans = 0;
for(i=0;i< n;++i){
	cnt = 0;
	for(j=0;j< length;++j){
		if(str[i].charAt(j)=='X')
			++cnt;
	}
	ans+=max_length-cnt;
 }
 return ans;
 }
}

  1. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。