zoukankan      html  css  js  c++  java
  • Baby Ming and Matrix games

    Baby Ming and Matrix games

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1687    Accepted Submission(s): 481


    Problem Description
    These few days, Baby Ming is addicted to playing a matrix game.

    Given a nm matrix, the character in the matrix(i2,j2) (i,j=0,1,2...) are the numbers between 09 . There are an arithmetic sign (‘+’, ‘-‘, ‘ ’, ‘/’) between every two adjacent numbers, other places in the matrix fill with ‘#’.

    The question is whether you can find an expressions from the matrix, in order to make the result of the expressions equal to the given integer sum . (Expressions are calculated according to the order from left to right)

    Get expressions by the following way: select a number as a starting point, and then selecting an adjacent digital X to make the expressions, and then, selecting the location of X for the next starting point. (The number in same place can’t be used twice.)
     
    Input
    In the first line contains a single positive integer T , indicating number of test case.

    In the second line there are two odd numbers n,m , and an integer sum(1018<sum<1018 , divisor 0 is not legitimate, division rules see example)

    In the next n lines, each line input m characters, indicating the matrix. (The number of numbers in the matrix is less than 15 )

    1T1000
     
    Output
    Print Possible if it is possible to find such an expressions.

    Print Impossible if it is impossible to find such an expressions.
     
    Sample Input
    3 3 3 24 1*1 +#* 2*8 1 1 1 1 3 3 3 1*0 /#* 2*6
     
    Sample Output
    Possible Possible Possible
    Hint
    The first sample:1+2*8=24 The third sample:1/2*6=3
     
    #include <bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    const int maxn=1e3;
    char str[maxn][maxn];
    int T,n,m;
    ll sum;
    bool flag=false;
    int dx[5]={0,2,-2,0,0},dy[5]={0,0,0,2,-2};
    bool ok(char ch){
         if(ch=='+'||ch=='-'||ch=='*'||ch=='/')return true;
         else return false;
    }
    double cal(double a,double b,char c){
         switch(c){
            case '+':return a+b;
            case '-':return a-b;
            case '*':return a*b;
            case '/':return a/b;
         }
    }
    void dfs(int x,int y,double tot){
          if(flag)return;
          if(fabs(tot-sum)<=1e-6){
                flag=true;
                return;
          }
          for(int i=1;i<=4;i++){
                int cx=x+dx[i],cy=y+dy[i];
                if(cx<1||cy<1||cx>n||cy>m)continue;
                if(!isdigit(str[cx][cy]))continue;
                if(!ok(str[x+dx[i]/2][y+dy[i]/2]))continue;
                if(str[x+dx[i]/2][y+dy[i]/2]=='/'&&str[cx][cy]=='0')continue;
                char q=str[cx][cy];
                double ans=cal(tot,str[cx][cy]-'0',str[x+dx[i]/2][y+dy[i]/2]);
                str[cx][cy]='#';
                dfs(cx,cy,ans);
                str[cx][cy]=q;
          }
    }
    int main()
    {
        cin>>T;
        while(T--){
            flag=false;
            cin>>n>>m>>sum;
            for(int i=1;i<=n;i++)scanf("%s",str[i]+1);
            /*for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    printf("%c",str[i][j]);
                }
                cout<<endl;
            }*/
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    int now=str[i][j]-'0';
                    if(isdigit(str[i][j])){
                        str[i][j]='#';
                        dfs(i,j,now);
                        str[i][j]=now+'0';
                    }
                }
            }
            if(flag)cout<<"Possible"<<endl;
            else cout<<"Impossible"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    请使用支持 JDBC 4.0 的 sqljdbc4.jar 类库
    add jars和add external jars有什么区别
    JDBC连接sql server数据库的详细步骤和代码
    ios 开发中 动态库 与静态库的区别
    Core Data使用之一(Swift): 获取
    南邮NOJ整除的尾数
    南邮NOJ卡片游戏
    南邮NOJ卡片游戏
    南邮NOJ卡片游戏
    南邮NOJ卡片游戏
  • 原文地址:https://www.cnblogs.com/czy-power/p/10562144.html
Copyright © 2011-2022 走看看