http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3513
A man is lost in a strange world. In this world, he is a human in the daytime, and he will become a pig at night. The strange world is rectangle which is seperated into many 1 * 1 grids, from (0,0) to (X,Y). Every grid has a coordinate ( x , y ). If he is in the grid ( x , y ), he can jump to grid ( x - k * y , y ) or ( x , y - k * x ). k is a positive integer. For example, if he is in the grid (4,9), he can only jump to the grid (4,1) or (4,5). At night, he jumps to another grid at 1:00 AM as pig. In the daytime, he jumps to another grid at 1:00 PM as a human. So he will jump exactly twice everyday.
As the figure show, the grids ( x , 0 ), ( 0 , y ) (0 <= x <= X, 0 <= y <= Y ) are the river. When he Jump into the river, he will change from pig to human or change from human to pig immediately. And his property will never change from then on. It means that if he jump into the river in the daytime, he will be a pig forever. He want to jump into the river at night, so that he change from pig to human immediately, and can be a human forever, never become a pig again.
When he become a pig at night, he will jump to a grid whose coordinate satisfies ( x - k * y , y ) or ( x , y - k * x ) arbitrarily. He will not jump out of the strange world either in the daytime or at night. At the beginning, he is at the grid ( x0 , y0 ). To ensure that he can jump into the river as a pig at last, at the beginning, he can choose to start as a pig at night or as a human in the daytime. You need to determine what time (day or night) to start in every grid of the strange world excecpt the river. Use a matrix to display it.
There are multiple cases (no more than 100).
Each case contain two integers X and Y (1 <= X * Y <= 40000) indicating the size of the strange world.
For each test case i, print case number in the form “Case #i” in one single line. And there is a X*Y matrix. The j th charater of the i th line indicating what time (day or night) to start in the grid ( i , j ). ‘H’ means that to ensure that he can jump into the river as a human, he needs to start as a human in the daytime. ‘P’ means that to ensure that he can jump into the river as a human, he needs to start as a pig at night.
1 2
2 3
Case #1:
PH
Case #2:
PHH
HPP
题意:给出一个n×m的地图,第0行和第0列是一条神奇的河。在这个世界中,每过半天,人就会变成猪,猪就会变成人,跳到河里前,如果是猪,那么就会变成人,然后不会变回猪了,否则只能永远变成猪~无论对于猪和人来说,如果当前的位置是(x,y),那么下一次可以跳到( x - k * y , y ) 或( x , y - k * x ),k是一个正整数。每跳一次就会过半天,由于人是有智慧的,他可以选择自己怎么跳,但猪非常蠢,只会随机跳,问最开始在每个位置,是猪还是人才能保证最后一定会变成人。
思路:这题最开始读了好久没读懂,开始还以为是gcd乱搞,果断wa了,后来yy了样例才才出题意。其实知道了题意就好做了~如果x==y,那么下一次一定会跳到河里,这时状态一定是猪才行,如果x%y==0||y%x==0,那么这时要是人,因为此时一定可以找到一个x==y的点跳过去,否则的话,就要去枚举一下了,如果某个点的后继所有状态中,都是人,那么这个位置的状态就是猪了,否则就是人,此时人可以选一个状态为猪的位置跳过去~
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#define N 40009
using namespace std;
char a[N];
int n, m;
inline int get(int x, int y)
{
return (x - 1) * m + y - 1;
}
char fun(int x, int y)
{
if (x == y)
{
return a[get(x, y)] = 'P';
}
if (x%y == 0 || y %x == 0)
{
return a[get(x, y)] = 'H';
}
if (x > y)
{
for(int i = 1; x - y * i > 0; i++)
{
if (a[get( x - y * i, y)] == 'P')
return a[get(x, y)] = 'H';
}
}
else
{
for (int i = 1; y - x * i > 0; i++)
{
if (a[get(x, y - x * i)] == 'P')
return a[get(x, y)] = 'H';
}
}
return a[get(x, y)] = 'P';
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
int count = 0;
while(~scanf("%d%d", &n, &m))
{
printf("Case #%d:\n", ++count);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
printf("%c", fun(i, j));
}
cout << endl;
}
}
return 0;
}