dfs暴搜即可 注意起点和终点任意 且 中间不能除0
中间的保存值直接用double存的 就水过了=。= 可以优化成分数形式 就不会有精度问题(不过就15个数 也很难有精度问题)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#define scnaf scanf
#define cahr char
#define bug puts("bugbugbug");
using namespace std;
typedef long long ll;
const int mod=1000000007;
const int maxn=50+5;
const int inf=1e9;
const double eps=1e-8;
char a[maxn][maxn];
int vis[maxn][maxn];
int n,m;
double sum;
bool dfs(int x,int y,double v);
bool check(int x1,int y1,int x2,int y2,double v)
{
if(1<=x2&&x2<=n&&1<=y2&&y2<=m);
else return 0;
// cout<<x2<<"====="<<y2<<endl;
if(vis[x2][y2]) return 0;
if(a[x1][y1]=='/'&&a[x2][y2]=='0') return 0;
if(a[x1][y1]=='+') v+=a[x2][y2]-'0';
if(a[x1][y1]=='-') v-=a[x2][y2]-'0';
if(a[x1][y1]=='*') v*=a[x2][y2]-'0';
if(a[x1][y1]=='/') v/=a[x2][y2]-'0';
return dfs(x2,y2,v);
}
bool dfs(int x,int y,double v)
{
if(fabs(v-sum)<eps) return 1;
vis[x][y]=1;
//cout<<x<<"====="<<y<<endl;
if(check(x,y+1,x,y+2,v)) return 1;
if(check(x,y-1,x,y-2,v)) return 1;
if(check(x+1,y,x+2,y,v)) return 1;
if(check(x-1,y,x-2,y,v)) return 1;
vis[x][y]=0;
return 0;
}
bool ok()
{
for(int i=1;i<=n;i+=2)
for(int j=1;j<=m;j+=2){
memset(vis,0,sizeof(vis));
if(dfs(i,j,a[i][j]-'0')) return 1;
}
return 0;
}
int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--){
scanf("%d%d%lf",&n,&m,&sum);
for(int i=1;i<=n;i++)
scanf("%s",a[i]+1);
if(ok()) puts("Possible");
else puts("Impossible");
}
return 0;
}