个人赛&cf G题:The Tower of Evil 模拟&数学

G. The Tower of Evil
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

The forces of evil are about to disappear since our hero is now on top on the tower of evil, and all what is left is the most evil, most dangerous monster! The tower has h floors (numbered from 1 to h, bottom to top), each floor has w rooms (numbered from 1 to w, left to right) composing a row. The monster stands in one of the ground floor (floor number 1) rooms (the room number d where 1 ≤ d ≤ w). Our hero stands in the top-left room of the tower. The only way for our hero to kill the evil monster is to throw down one of his power stones diagonally to the right. The stone will keep moving diagonally through the tower's rooms until it hits the right border of the tower, then it will change its direction to move down diagonally to the left until it hits the left border of the tower, then it will change its direction again and so on. This stone stops when it reaches the ground floor, if it stopped in the room of the monster, the monster is dead, otherwise the monster is still alive and the forces of evil will rise again! Help our hero to determine whether the stone will kill the monster or not!

Input

The input consists of several test cases. The first line of the input contains a single integer T, the number of the test cases. Each of the following lines contains a test case and consists of a three space-separated integers hw and d denoting the height of the tower, the width of the tower and the number of room containing the monster. (1 ≤ d ≤ 109), (2 ≤ h, w ≤ 109).

Output

For each test case print a single line: 'Yes' if the monster will be killed and 'No' otherwise.

Example
input
5
9 4 2
9 4 3
5 4 3
10 2 1
10 2 2
output
No
Yes
Yes
No
Yes
Note

In the first test case, the path of the power stone is colored in red. However the dragon is in the room denoted with D, so the monster is still alive and the answer is 'No'.

 

我思路就是这样:

以宽度所在最左边位置为一周期开始,每一个周期下降4层。

宽度为2的时候需要特判,比如10,2 ,  2 取模后为0,需要特殊处理,w为2时有两种情况即也可以Yes。


import java.util.Scanner;
  
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
			int T = sc.nextInt();
			while(T-->0) {
				int h = sc.nextInt();
				int w = sc.nextInt();
				int d = sc.nextInt();
				int t = 2*w-2;//一个周期,回到宽度的原位,可以举例子自己看,这个t是下的层数
				h %= t;
				if(w!=2){
				if(h==d||h == 2*w-d) { //其实就是两种情况,一种是直接下去反弹之前打到怪物,一直是反弹之后打到的
					System.out.println("Yes");
				}
				else 
					System.out.println("No");
			}
				if(w==2) {
					if((h==0&&d==2)||(h==1&&d==1))
						System.out.println("Yes");
				
				else
					System.out.println("No");
				}
			}
	}
}

另外附上想让OJ爆内存的纯模拟大法代码:

import java.util.Scanner;
  
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
			int T = sc.nextInt();
			while(T-->0) {
				int h = sc.nextInt();
				int w = sc.nextInt();
				int d = sc.nextInt();
				int h1 = h;
				int w1 = 1;
				
				boolean flag1 = true,flag2 = false,flag3 = true;
				do{
					
					if(flag1&&flag3) {
					  w1++;
					  h1--;
					  if (w1==w) {
						flag1 = false;
						flag2 = true;
						if(w==2&&h1==1) {
							flag3 = false;
						}
					   }
					  
					}
					if(flag2&&flag3){
					  w1--;
					  h1--;
					  if(w1==1){
						  flag1 = true;
						  flag2 =false;
					  	}
					}
					}while(h1 != 1);
						if(w1 == d)
							System.out.println("Yse");
						else
							System.out.println("No");		
			}
	}
}


原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256663.html