dfs,比较简单
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
const int INF = ~0U >> 1;
const int maxn = 50;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
char str[maxn][maxn];
int T, n, m, sum;
int vis[maxn][maxn];
bool ok(int nx, int ny) {
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m) return true;
return false;
}
bool dfs(int x, int y, double value) {
if(value == sum) {
return true;
}
for(int i = 0; i < 4; ++i) {
int nx = dx[i] + x, ny = dy[i] + y;
if(ok(nx, ny)) {
if(str[nx][ny] == '+') {
for(int j = 0; j < 4; ++j) {
int X = dx[i] + nx, Y = dy[i] + ny;
if(ok(X, Y) && str[X][Y] >= '0' && str[X][y] <= '9' && vis[X][Y] == 0) {
vis[X][Y] = 1;
if(dfs(X, Y, value + (str[X][Y] - '0'))) return true;
vis[X][Y] = 0;
}
}
}
if(str[nx][ny] == '-') {
for(int j = 0; j < 4; ++j) {
int X = dx[i] + nx, Y = dy[i] + ny;
if(ok(X, Y) && str[X][Y] >= '0' && str[X][y] <= '9' && vis[X][Y] == 0) {
vis[X][Y] = 1;
if(dfs(X, Y, value - (str[X][Y] - '0'))) return true;
vis[X][Y] = 0;
}
}
}
if(str[nx][ny] == '*') {
for(int j = 0; j < 4; ++j) {
int X = dx[i] + nx, Y = dy[i] + ny;
if(ok(X, Y) && str[X][Y] >= '0' && str[X][y] <= '9' && vis[X][Y] == 0) {
vis[X][Y] = 1;
if(dfs(X, Y, value * (str[X][Y] - '0'))) return true;
vis[X][Y] = 0;
}
}
}
if(str[nx][ny] == '/') {
for(int j = 0; j < 4; ++j) {
int X = dx[i] + nx, Y = dy[i] + ny;
if(ok(X, Y) && str[X][Y] >= '0' && str[X][y] <= '9' && vis[X][Y] == 0) {
vis[X][Y] = 1;
if(dfs(X, Y, value / (str[X][Y] - '0'))) return true;
vis[X][Y] = 0;
}
}
}
}
}
return false;
}
int main() {
scanf("%d", &T);
while(T--) {
memset(vis, 0, sizeof(vis));
scanf("%d%d%d", &n, &m, &sum);
for(int i = 1; i <= n; ++i)
scanf("%s", str[i] + 1);
bool is = false;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
if(str[i][j] >= '0' && str[i][j] <= '9') {
vis[i][j] = 1;
if(dfs(i, j, str[i][j] - '0')) {
is = true;
goto leap;
}
vis[i][j] = 0;
}
}
}
leap:
if(is) printf("Possible\n");
else printf("Impossible\n");
}
return 0;
}