Double Matrix

博客围绕两个n×m整数矩阵展开,定义了严格递增的行、列和矩阵。可交换两矩阵对应位置元素,目标是通过若干操作使两矩阵都递增。给出输入矩阵维度及元素的格式,输出判断结果为'Possible'或'Impossible'。

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\n");
    }
    else
    {
        printf("Possible\n");
    }
}

 

转载于:https://www.cnblogs.com/csxaxx/p/10832158.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值