#define _CRT_SECURE_NO_WARNINGS #pragma comment(linker, "/stack:16777216") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i) #define REP(i, N) FOR(i, 0, N) #define RREP(i, N) RFOR(i, N, 0) #define FILL(A,value) memset(A,value,sizeof(A)) #define ALL(V) V.begin(), V.end() #define SZ(V) (int)V.size() #define PB push_back #define MP make_pair #define Pi 3.14159265358979 typedef long long Int; typedef unsigned long long UINT; typedef vector VI; typedef pair PII; const int INF = 2000000000; const int MAX = 64; const int MAX2 = 1007; const int BASE = 1000000000; const int MOD = 1000000007; struct test { int n, m; char S[MAX][MAX]; test(){} void Write() { cout << n << ' ' << m << endl; FOR (i,0,n) { FOR (j,0,m) cout << S[i][j]; cout << endl; } } void Read() { cin >> n >> m; FOR (i,0,n) scanf("%s", S[i]); } vector A; int C[MAX]; int P[MAX]; void Solve() { FOR (i,0,n) FOR (j,0,m) if (S[i][j] == 'L') A.PB(MP(i, j)); bool res = 0; FOR (mask,0,(1 << SZ(A))) { bool U[MAX][MAX]; FILL(U, 0); FILL(C, 0); FOR (i,0,SZ(A)) { if ((mask & (1 << i)) != 0) U[A[i].first][A[i].second] = 1; else { ++ C[A[i].first]; P[A[i].first] = A[i].second; } } bool ok = 1; RFOR (i,n,0) { int Min = INF, Max = -1; FOR (j,0,m) { if (i+1 < n && U[i+1][j] == 1) U[i][j] = 1; if (U[i][j] == 0 && S[i][j] == 'E') { Min = min(Min, j); Max = max(Max, j); } } if (C[i] >= 2) continue; if (C[i] == 0 && Max == -1) continue; if (C[i] == 1 && (P[i] <= Min || P[i] >= Max)) continue; ok = 0; break; } if (ok) { res = 1; break; } } cout << (res == 1 ? "Possible" : "Impossible") << endl; } }; test GetRandom(int n, int m, int cntE, int cntL) { test res; res.n = n; res.m = m; FOR (i,0,n) FOR (j,0,m) res.S[i][j] = '.'; FOR (i,0,cntE) { int x = rand() % n; int y = rand() % m; res.S[x][y] = 'E'; } set S; FOR (i,0,cntL) { int x, y; do { x = rand() % n; y = rand() % m; } while (S.find(MP(x, y)) != S.end()); res.S[x][y] = 'L'; S.insert(MP(x, y)); } return res; } test GetRandomBetter(int n, int m, int cntE, int cntL, int add) { test res; res.n = n; res.m = m; FOR (i,0,n) FOR (j,0,m) res.S[i][j] = '.'; set S; set Q; vector A; vector C; FOR (i,0,cntL) { int x, y; do { x = rand() % n; y = rand() % m; } while (S.find(MP(x, y)) != S.end()); res.S[x][y] = 'L'; S.insert(MP(x, y)); A.PB(MP(x, y)); } bool B[MAX][MAX]; FILL(B, 0); int dx[] = {-1, 0, 0}; int dy[] = {0, -1, 1}; FOR (i,0,SZ(A)) { int d = rand() % 3; int x = A[i].first; int y = A[i].second; while (1) { x += dx[d]; y += dy[d]; if (x < 0 || x >= n) break; if (y < 0 || y >= m) break; if (res.S[x][y] == '.') { if (Q.find(MP(x, y)) == Q.end()) { Q.insert(MP(x, y)); C.PB(MP(x, y)); } } } } FOR (i,0,cntE) { if (C.empty()) break; int pos = rand() % SZ(C); res.S[C[pos].first][C[pos].second] = 'E'; } FOR (i,0,add) { int x = rand() % n; int y = rand() % m; if (res.S[x][y] == '.') res.S[x][y] = 'E'; } return res; } void OpenInput(int cnt) { char buf[256]; sprintf(buf, "D:\\witua\\Cook-off August\\SHOOTING\\%d.in", cnt); freopen(buf, "wb", stdout); } void OpenOutput(int cnt) { char buf[256]; sprintf(buf, "D:\\witua\\Cook-off August\\SHOOTING\\%d.out", cnt); freopen(buf, "wb", stdout); } void WriteInput(vector A) { cout << SZ(A) << endl; FOR (i,0,SZ(A)) { A[i].Write(); } } void WriteOutput(vector A) { FOR (i,0,SZ(A)) A[i].Solve(); } void Do(vector A, int cnt) { OpenInput(cnt); WriteInput(A); OpenOutput(cnt); WriteOutput(A); } int main() { int cnt; cin >> cnt; FOR (i,0,cnt) { test t; t.Read(); t.Solve(); } /*int cnt = 0; vector A; A.clear(); FOR (i,0,3) A.PB(GetRandom(5, 5, 10, 5)); FOR (i,0,3) A.PB(GetRandom(5, 5, 10, 7)); FOR (i,0,4) A.PB(GetRandom(5, 5, 17, 13)); Do(A, cnt ++); A.clear(); FOR (i,0,3) A.PB(GetRandom(20, 20, 10, 5)); FOR (i,0,3) A.PB(GetRandom(20, 20, 5, 16)); FOR (i,0,4) A.PB(GetRandom(20, 20, 3, 14)); Do(A, cnt ++); A.clear(); FOR (i,0,3) A.PB(GetRandomBetter(50, 50, 50, 10, 0)); FOR (i,0,3) A.PB(GetRandomBetter(50, 50, 75, 13, 0)); FOR (i,0,4) A.PB(GetRandomBetter(50, 50, 150, 16, 0)); Do(A, cnt ++); A.clear(); FOR (i,0,3) A.PB(GetRandomBetter(50, 50, 300, 16, 0)); FOR (i,0,3) A.PB(GetRandomBetter(50, 50, 500, 16, 0)); FOR (i,0,4) A.PB(GetRandomBetter(50, 50, 1500, 16, 0)); Do(A, cnt ++); A.clear(); FOR (i,0,3) A.PB(GetRandomBetter(50, 50, 200, 16, rand() % 20 + 1)); FOR (i,0,3) A.PB(GetRandomBetter(50, 50, 500, 16, rand() % 20 + 1)); FOR (i,0,4) A.PB(GetRandomBetter(50, 50, 700, 16, rand() % 20 + 1)); Do(A, cnt ++);*/ return 0; }