SRM292Div2,250

この記事は約3分で読めます。

The highway extends endlessly headed east and west. Bob the chicken walks in a sequence of straight lines. Given Bob’s path, we want to figure out how many times he crosses the road (assuming he doesn’t become road kill). We will not count it as a crossing if Bob moves onto the road, possibly wanders along the road, and then returns to the side he entered the road from.
The road has negligible width, so it is just a horizontal line running along a given y coordinate. Create a class FowlRoad that contains a method crossings that is given roadY (the y coordinate of the road), and int[]s bobX and bobY giving Bob’s path. It should return the number of times that Bob crosses the road.

The ith elements of bobX and bobY give the coordinates of a point in Bob’s path. Bob starts at the first point, and proceeds in a straight line to the next point, continuing until he finishes at the last point. Bob does not start or end on the road.
[java]
public class FowlRoad {

public int crossings(int roadY, int[] bobX, int[] bobY) {
int side = 1;
int count=0;
int side_in=0;

if (roadY>bobY[0]){
side=-1;
}

for (int i = 1; i < bobY.length; i++) {
if (side==-1) {
if (roadY<bobY[i]) {
side=1;
count++;
}else if(roadY==bobY[i]) {
side=0;
side_in=-1;
}
}else if(side==1){
if (roadY>bobY[i]) {
side=-1;
count++;
}else if(roadY==bobY[i]) {
side=0;
side_in=1;
}
}else{
if (roadY<bobY[i]&&side_in==-1) {
side=1;
count++;
}else if(roadY>bobY[i]&&side_in==1){
side=-1;
count++;
}
}
}
return count;
}

}
[/java]

コメント

タイトルとURLをコピーしました