You are given two and all columns are strictly increasing.

For example, the matrix [1123] is not increasing because the first row is not strictly increasing.

Let a position in the (i,j).

In one operation, you can choose any two numbers (i,j) in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.

You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".

Input

The first line contains two integers 1≤n,m≤50) — the dimensions of each matrix.

Each of the next (i,j) in the first matrix.

Each of the next (i,j) in the second matrix.

Output

Print a string "Impossible" or "Possible".

Examples

Input
2 2
2 10
11 5
9 4
3 12
Output
Possible
Input
2 3
2 4 5
4 5 6
3 6 7
8 10 11
Output
Possible
Input
3 2
1 3
2 4
5 10
3 1
3 6
4 8
Output
Impossible,
本题题意给你两个矩阵,让你通过改变相同位置的两个矩阵的数值,使两个矩阵都满足纵向和横向严格递增;
我们的方法是通过将每一个位置的矩阵值进行分开,较小值放到一个矩阵,较大值放到另一个矩阵,如果这个结果满足条件,及整体满足条件
接下来是代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long 
const int maxn = 55;
const int inf = 0x3f3f3f3f;
ll a[maxn][maxn], b[maxn][maxn];
int main()
{
    int n, m;
    cin >> n >> m;
    memset(a, inf, sizeof(a));
    memset(b, inf, sizeof(b));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            scanf("%lld", &a[i][j]);
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            scanf("%lld", &b[i][j]);
            if (a[i][j] > b[i][j])
            {
                ll t;
                t = b[i][j];b[i][j] = a[i][j];a[i][j] = t;
            }
        }
    }
    int flag = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (a[i][j] >= a[i + 1][j] || a[i][j] >= a[i][j + 1])
            {
                flag = 1;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (b[i][j] >= b[i + 1][j] || b[i][j] >= b[i][j + 1])
            {
                flag = 1;
            }
        }
    }
    if (flag == 1)
    {
        printf("Impossible\n");
    }
    else
    {
        printf("Possible\n");
    }
}

 

相关文章:

  • 2021-07-04
  • 2021-07-19
  • 2021-08-08
  • 2021-12-26
  • 2022-01-10
猜你喜欢
  • 2021-11-15
  • 2021-10-27
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案