Double Matrix

You are given two n×mn×m matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing.

For example, the matrix [91110121114][91011111214] is increasing because each individual row and column is strictly increasing. On the other hand, the matrix [1213][1123] is not increasing because the first row is not strictly increasing.

Let a position in the ii-th row (from top) and jj-th column (from left) in a matrix be denoted as (i,j)(i,j).

In one operation, you can choose any two numbers ii and jj and swap the number located in (i,j)(i,j) in the first matrix with the number in (i,j)(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 nn and mm (1n,m501≤n,m≤50) — the dimensions of each matrix.

Each of the next nn lines contains mm integers ai1,ai2,,aimai1,ai2,…,aim (1aij1091≤aij≤109) — the number located in position (i,j)(i,j) in the first matrix.

Each of the next nn lines contains mm integers bi1,bi2,,bimbi1,bi2,…,bim (1bij1091≤bij≤109) — the number located in position (i,j)(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
");
    }
    else
    {
        printf("Possible
");
    }
}
原文地址:https://www.cnblogs.com/csxaxx/p/10832158.html