Baby Ming and Matrix games
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 562 Accepted Submission(s): 128
Problem Description
These few days, Baby Ming is addicted to playing a matrix game.
Given a n∗m matrix, the character in the matrix (i∗2,j∗2) (i,j=0,1,2...) are the numbers between 0−9 . 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.)
Given a n∗m matrix, the character in the matrix (i∗2,j∗2) (i,j=0,1,2...) are the numbers between 0−9 . 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 )
1≤T≤1000
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 )
1≤T≤1000
Output
Print Possible if it is possible to find such an expressions.
Print Impossible if it is impossible 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 PossibleHintThe first sample:1+2*8=24 The third sample:1/2*6=3
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#define eps 1e-8
using namespace std;
const int maxn=10010;
int n,m;
double k;
bool sign;
bool vis[30][30];
char map[30][30];
int mov[][2]={0,1,1,0,-1,0,0,-1};
void dfs(int x,int y,double ans,char oper,bool flag){
if(fabs(ans-k)<eps||sign){
sign=true;
return ;
}
for(int i=0;i<4;++i){
int xx=x+mov[i][0];
int yy=y+mov[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]){
vis[xx][yy]=true;
if(map[xx][yy]>='0'&&map[xx][yy]<='9'){
if(flag){
if(oper=='+')dfs(xx,yy,ans+(map[xx][yy]-'0'),oper,false);
else if(oper=='-')dfs(xx,yy,ans-(map[xx][yy]-'0'),oper,false);
else if(oper=='*')dfs(xx,yy,ans*(map[xx][yy]-'0'),oper,false);
else if(oper=='/'&&map[xx][yy]!='0')dfs(xx,yy,ans/(map[xx][yy]-'0'),oper,false);
}
}
else if(map[xx][yy]=='+'||map[xx][yy]=='-'||map[xx][yy]=='*'||map[xx][yy]=='/'){
dfs(xx,yy,ans,map[xx][yy],true);
}
vis[xx][yy]=false;
}
}
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d%lf",&n,&m,&k);
for(i=0;i<n;++i){
scanf("%s",map[i]);
}
sign=false;
for(i=0;i<n;++i){
for(j=0;j<m;++j){
memset(vis,false,sizeof(vis));
vis[i][j]=true;
if(map[i][j]>='0'&&map[i][j]<='9')
dfs(i,j,map[i][j]-'0','!',false);
if(sign)break;
}
if(j<m)break;
}
if(i<n){
printf("Possible\n");
}
else {
printf("Impossible\n");
}
}
return 0;
}