zoukankan      html  css  js  c++  java
  • hdu--1104--Remainder(简单的bfs)

    Remainder

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4078    Accepted Submission(s): 1014


    Problem Description
    Coco is a clever boy, who is good at mathematics. However, he is puzzled by a difficult mathematics problem. The problem is: Given three integers N, K and M, N may adds (‘+’) M, subtract (‘-‘) M, multiples (‘*’) M or modulus (‘%’) M (The definition of ‘%’ is given below), and the result will be restored in N. Continue the process above, can you make a situation that “[(the initial value of N) + 1] % K” is equal to “(the current value of N) % K”? If you can, find the minimum steps and what you should do in each step. Please help poor Coco to solve this problem. 

    You should know that if a = b * q + r (q > 0 and 0 <= r < q), then we have a % q = r.
     
    Input
    There are multiple cases. Each case contains three integers N, K and M (-1000 <= N <= 1000, 1 < K <= 1000, 0 < M <= 1000) in a single line.

    The input is terminated with three 0s. This test case is not to be processed.
     
    Output
    For each case, if there is no solution, just print 0. Otherwise, on the first line of the output print the minimum number of steps to make “[(the initial value of N) + 1] % K” is equal to “(the final value of N) % K”. The second line print the operations to do in each step, which consist of ‘+’, ‘-‘, ‘*’ and ‘%’. If there are more than one solution, print the minimum one. (Here we define ‘+’ < ‘-‘ < ‘*’ < ‘%’. And if A = a1a2...ak and B = b1b2...bk are both solutions, we say A < B, if and only if there exists a P such that for i = 1, ..., P-1, ai = bi, and for i = P, ai < bi)
     
    Sample Input
    2 2 2
    -1 12 1
    0 0 0 0
     
    Sample Output
    0
    2
    *+
     1 /*
     2     Name: hdu--1104--Remainder
     3     Copyright: ©2017 日天大帝
     4     Author: 日天大帝
     5     Date: 22/04/17 09:11
     6     Description: bfs
     7                 a = b * q + r (q > 0 and 0 <= r < q),
     8                 题上的取余运算和%运算不一样,%运算能产生负值所以要 (n%k+k)%k这样,才等于题意的取余
     9                 这个题用vis标记产生过的数据,同时用%km和%k进行剪枝优化 
    10                  
    11 */
    12 #include<cstring>
    13 #include<iostream>
    14 #include<queue>
    15 #include<string> 
    16 using namespace std;
    17 struct node{
    18     int num,steps;
    19     string str;
    20 };
    21 const int MAX = 1005;
    22 bool vis[MAX];
    23 int n,m,k,mk,final_cmp;
    24 void bfs(){
    25     node start;
    26     start.num = n;
    27     start.str = "";
    28     start.steps = 0;
    29     vis[(n%k+k)%k] = 1;
    30     queue<node> q;
    31     q.push(start);
    32     
    33     while(!q.empty()){
    34         node a,temp = q.front();q.pop();
    35         if(final_cmp == ((temp.num%k)+k)%k){
    36             cout<<temp.steps<<endl<<temp.str<<endl;
    37             return ;
    38         }
    39         for(int i=0; i<4; ++i){
    40             a = temp;
    41             if(i == 0){
    42                 a.num += m;
    43                 a.str += '+';
    44             }else if(i == 1){
    45                 a.num -= m;
    46                 a.str += '-' ;
    47             }else if(i == 2){
    48                 a.num *= m;
    49                 a.str += '*';
    50             }else{
    51                 a.num = (a.num%m+m)%m;
    52                 a.str += '%' ;
    53             }
    54             a.num %= mk;//%k之后%m结果就错啦,10%(15) !=10%3%5
    55             if(vis[(a.num%k+k)%k])continue;//%k缩小范围剪枝 
    56             a.steps++;
    57             vis[(a.num%k+k)%k] = 1;
    58             q.push(a);
    59         }
    60     }
    61     cout<<"0"<<endl;
    62 }
    63 int main(){
    64     ios::sync_with_stdio(false);
    65     
    66     while(cin>>n>>k>>m,n||m||k){//输入有正负不能用n+m+k 
    67         memset(vis,0,sizeof(vis));
    68         mk = m*k;
    69         final_cmp = ((n+1)%k+k)%k;
    70         bfs() ;
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    python全栈开发 * 继承性 层叠性 盒模型 标准文档流 * 180809
    python全栈开发 * css 选择器 浮动 * 180808
    python全栈开发 * 表格标签 表单标签 css 引入方式 * 180807
    python全栈开发 * 线程队列 线程池 协程 * 180731
    saltstack的jinjia模板
    saltstack cmd状态模块和条件判断
    saltstack 配置管理之状态间关系
    saltstack lamp自动化案例实战
    saltstack 配置管理之状态模块
    saltstack 远程执行之返回写入到mysql
  • 原文地址:https://www.cnblogs.com/slothrbk/p/6747002.html
Copyright © 2011-2022 走看看