• 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;
    }
  • 相关阅读:
    基于springboot1.5.9整合shiro时出现静态文件找不到的问题
    基于springboot多模块项目使用maven命令打成war包放到服务器上运行的问题
    关于使用map存放数据乱序”问题“
    springboot1.5.9整合websocket实现实时显示的小demo
    SpringBoot1.5.10.RELEASE配置mybatis的逆向工程
    SpringBoot1.5.10.RELEASE整合druid时,在druid monitor界面出现(*) property for user to setup
    SpringBoot1.5.10.RELEASE整合druid
    SpringBoot1.5.10.RELEASE项目如何在tomcat容器中运行
    JS学习-01
    List,Set,Map在java.util包下都是接口 List有两个实现类:ArrayList和LinkedList Set有两个实现类:HashSet和LinkedHashSet AbstractSet实现了Set
  • 原文地址:https://www.cnblogs.com/czy-power/p/10562144.html
Copyright © 2020-2023  润新知