Baby Ming and Matrix games
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1207 Accepted Submission(s): 314
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($-10^{18} < sum < 10^{18}$, 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 \leq T \leq 1000$
In the second line there are two odd numbers $n, m$, and an integer sum($-10^{18} < sum < 10^{18}$, 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 \leq T \leq 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 Possible
Hint
The first sample:1+2*8=24 The third sample:1/2*6=3
题意:
一幅图,给出规模和目标值,问能否通过某些数字和符号的组合得到目标的值
.
题解:
直接暴力搜索+回溯
也许今生都忘不了这道题!!!
错了不知道几十遍,从三点多调到现在,终于敢写博客了.....
本来是一个简单的dfs加上回溯,结果由于自己的一些小疏忽,一直在细节上wa1...最终还是比较完善的解决掉了....
另外精度的控制,真的不知道要怎么办了,到底多少才是适合的???
#include<stdio.h>
#include<string.h>
#include<math.h>
int n,m,kase,vis[55][55],dx[4]={0,0,-2,2},dy[4]={-2,2,0,0};
char map[55][55];
double s;
double num(double x,double y,char ch)
{
switch (ch)
{
case '+':return x+y;
case '-':return x-y;
case '*':return x*y;
case '/':return x/y;
}
}
void dfs(int x,int y,double sum)
{
if(kase)//找到结果了
{
return;
}
if(fabs(sum-s)<1e-6)//相等了
{
kase=1;
return;
}
for(int i=0;i<4;++i)
{
int tx=x+dx[i],ty=y+dy[i];
if(tx<0||tx>=n||ty<0||ty>=m)//越界
{
continue;
}
int tp=map[tx][ty]-'0';
char op=map[x+dx[i]/2][y+dy[i]/2];
if(tp==0&&op=='/')//除零错误
{
continue;
}
if(!vis[tx][ty])
{
vis[tx][ty]=1;
dfs(tx,ty,num(sum,tp,op));
vis[tx][ty]=0;//回溯
}
}
}
void slove()
{
kase=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i+=2)
{
for(int j=0;j<m;j+=2)
{
<span style="white-space:pre"> </span>vis[i][j]=1;
dfs(i,j,map[i][j]-'0');
if(kase)
{
printf("Possible\n");//符合要求了
return;
}
vis[i][j]=0;//取消标记
}
}
printf("Impossible\n");
}
int main()
{
int t;
//freopen("shuju.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
scanf("%d%d%lf",&n,&m,&s);
for(int i=0;i<n;++i)
{
scanf("%s",map[i]);
}
slove();
}
return 0;
}