Baby Ming and Matrix games (Dfs evaluation expressions)

Source: Internet
Author: User

Baby Ming and Matrix games

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others) total submission (s): 1210 Accepted Submission (s): 316

Problem Descriptionthese few days, Baby Ming are addicted to playing a matrix game.
Given A n∗m Matrix, the character in The matrix(I∗2, J∗2 )   (i,j=0< Span id= "mathjax-span-24" class= "Mo" >,1 ,2 . ) /span> is the numbers between0−9 . There is an arithmetic sign (' + ', '-', '∗', '/') between every-adjacent numbers, other places in the matrix fill with ' # '.
The question is whether you can find a expressions from the matrix, with order to make the result of the expressions equal To the given integer sum. (Expressions is 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 MAK E The expressions, and then, selecting the location of X for the next starting point. (the number in same place can ' t be used twice.)

Inputin The first line contains a single positive integerT, indicating number of test case.
In the second line there is and odd numbersn,m , and an integer sum (−<sum<ten , divisor 0 is isn't legitimate, division rules see example)
In the next Nlines, each line inputmCharacters, indicating the matrix. (The number of numbers in the matrix was less than )
1≤T≤

Outputprint Possible If it is Possible to find such an expressions.
Print Impossible if it is impossible to find such an expressions.

Sample Input33 3 241*1+#*2*81 1 113 3 31*0/#*2*6

Sample outputpossiblepossiblepossible HintThe first sample:1+2*8=24the third sample:1/2*6=3

The solving method: Let the DFS expression calculation see can get sum, must be next to the operation;

Very good a search problem, originally wrote for a long time, all kinds of considerations ah, and then eggs, look at the explanation of the great God, straight each walk two steps is good, that is more simple, wrote down on the, impossible I did not capitalize the wrong time;

Find Dfs sometimes really need to find the right method, otherwise trouble is easy to mistake, because there are division operations, so there is error, 1e-8 judgment under the error, also is divided by 0 case is also considered;

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<map>#include<string>#include<vector>using namespacestd;Const intinf=0x3f3f3f3f;#defineSI (x) scanf ("%d", &x)#definePI (x) printf ("%d", X)#defineP_ printf ("")#defineMem (x, y) memset (x,y,sizeof (x))typedef __int64 LL;Const intmaxn= -;CharMP[MAXN][MAXN];intdisx[4]={0,0,2,-2};intdisy[4]={2,-2,0,0};Doublesum;intFlot;intn,m;BOOLIsjs (Chara) {    if(a=='+'|| a=='-'|| a=='*'|| a=='/')return true; Else return false;}DoublejsDoubleACharXDoubleb) {    Switch(x) { Case '+':returnA+b; Break;  Case '-':returnA-B; Break;  Case '*':returnA*b; Break;  Case '/':returnA/b; Break; }}voidDfsintXintYDoublecur) {    if(Flot)return; if(ABS (Cur-sum) <=1e-8) {Flot=1; return; }     for(intI=0;i<4; i++){        intnx=x+disx[i],ny=y+Disy[i]; if(nx<0|| ny<0|| nx>=n| | NY&GT;=M)Continue; if(!isdigit (Mp[nx][ny]))Continue; if(!isjs (mp[x+disx[i]/2][y+disy[i]/2]))Continue; Chartemp=Mp[nx][ny]; //printf ("%i64d%c%c", Cur,mp[x+disx[i]/2][y+disy[i]/2],mp[nx][ny]); //printf ("%i64d\n", JS (cur,mp[x+disx[i]/2][y+disy[i]/2],mp[nx][ny]-' 0 '));        if(mp[x+disx[i]/2][y+disy[i]/2]=='/'&&mp[nx][ny]=='0')Continue; DoubleT=js (cur,mp[x+disx[i]/2][y+disy[i]/2],mp[nx][ny]-'0'); Mp[nx][ny]='#';        DFS (NX,NY,T); Mp[nx][ny]=temp; }}intMain () {intT;    SI (T);  while(t--) {scanf ("%D%D%LF",&n,&m,&sum);  for(intI=0; i<n;i++) scanf ("%s", Mp[i]); Flot=0;  for(intI=0; i<n;i++){             for(intj=0; j<m;j++){                if(IsDigit (Mp[i][j])) {inttemp=mp[i][j]-'0'; MP[I][J]='#';                    DFS (I,J,TEMP); MP[I][J]=temp+'0'; }            }        }    //for (int i=0;i<n;i++) puts (mp[i]);        if(Flot) puts ("Possible"); ElsePuts"Impossible"); }    return 0;}

Baby Ming and Matrix games (Dfs evaluation expressions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.