当前位置:编程学习 > C/C++ >>

zoj3513 Human or Pig----P/N分析 博弈

 Human or Pig
Time Limit: 2 Seconds      Memory Limit: 65536 KB
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.
Input   www.zzzyk.com
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.
Output
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.
Sample Input
1 2
2 3
Sample Output
Case #1:
PH
Case #2:
PHH
HPP
[cpp]
#include <fstream> 
#include <stdio.h> 
#include <iostream> 
#include <string.h> 
#include <string> 
#include <limits.h> 
#include <algorithm> 
#include <math.h> 
#include <numeric> 
#include <functional> 
#include <ctype.h> 
using namespace std; 
 
const int kMAX=1<<16; 
char grid_[kMAX]; 
int n,m; 
 
int grid(int x,int y) 

    return x*m+y; 

 
char Get(int x,int y) 

    int ret = grid(x,y); 
    char c='P'; 
    for(int k=1;x>k*y;++k) 
    { 
      int yy=grid(x-k*y,y); 
      if(grid_[yy] == 'P') 
      { 
                c='H'; 
                break; 
      } 
    } 
    for(int k=1;y>k*x;++k) 
    { 
        int yy=grid(x,y-k*x); 
        if(grid_[yy] == 'P') 
        { 
                c='H'; 
                break; 
        } 
    } 
     
    return grid_[ret]=c; 

 
int main() 

 
  int cases=1; 
  while(~scanf("%d%d",&n,&m) ) 
  { 
    printf("Case #%d:\n",cases++); 
    memset(grid_,'?',sizeof(grid_)); 
 
        for(int i=1;i<=n;++i) 
        { 
            for(int j=1;j<=m;++j) 
            putchar(Get(i,j)); 
            puts(""); 
        } 
  } 
 
  return 0; 

 
[cpp] 
#include<iostream> 
#include<cstdlib> 
#include<stdio.h> 
#include<memory.h> 
using namespace std; 
int n,m; 
int mat[100000]; 
int get(int x,int y) 

    return x*m+y; 

void solve(int x,int y) 

    int id=get(x,y); 
    //if(mat[id]!=-1) return ; 
    bool flag=true; 
    for(int i=1;;i++) 
    { 
        int px=x-i*y; 
        if(px<=0) break; 
        int iid=get(px,y); 
        if(mat[iid]==0) 
        { 
            flag=false; 
            break; 
        } 
    } 
    if(flag) 
    { 
        for(int i=1;;i++) 
       { 
        int py=y-i*x; 
        if(py<=0) break;//悲催,写成<0wrong了 
        int iid=get(x,py); 
        if(mat[iid]==0) 
        { 
            flag=false; 
            break; 
        } 
       } 
    } 
   
    if(flag) 
    mat[id]=0; 
    else 
    ma

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,