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;
    }
  • 相关阅读:
    jQuery EasyUI API 中文文档
    easyui datagrid使用
    Jquery EasyUI的添加,修改,删除,查询等基本操作介绍
    JQueryEasyUI datagrid框架的基本使用
    使用easyUI 创建复杂的toolbar到datagrid
    转换和删除重复命令tr
    格式化文本数据抽取工具awk
    流编辑器sed
    查找文本工具grep
    查找文件工具find
  • 原文地址:https://www.cnblogs.com/czy-power/p/10562144.html
Copyright © 2011-2022 走看看