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”.InputThe first line contains two integers nn and mm (1≤n,m≤501≤n,m≤50) — the dimensions of each matrix.Each of the next nn lines contains mm integers ai1,ai2,…,aimai1,ai2,…,aim (1≤aij≤1091≤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 (1≤bij≤1091≤bij≤109) — the number located in position (i,j)(i,j) in the second matrix.OutputPrint a string “Impossible” or “Possible”.
Examplesinput
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
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be [9111012][9101112]and [2345][2435].In the second example, we don’t need to do any operations.In the third example, no matter what we swap, we can’t fix the first row to be strictly increasing in both matrices.
思路:二维数组的重新构建;把两数组相应位置作比较,大的放一边,小的放一边,在比较重新构建的数组。代码如下
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int a[55][55], b[55][55], n, m, x, y;
bool check(int a[55][55]) {
for (int i = 1; i <= n; ++i) {
for (int j = 2; j <= m; ++j)
if (a[i][j - 1] >= a[i][j])
return false;
}
for (int j = 1; j <= m; ++j) {
for (int i = 2; i <= n; ++i)
if (a[i - 1][j] >= a[i][j])
return false;
}
return true;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
cin >> a[i][j];
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
cin >> b[i][j];
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
x = min(a[i][j], b[i][j]);
y = max(a[i][j], b[i][j]);
a[i][j] = x;
b[i][j] = y;
}
if(check(a) && check(b))
cout << "Possible" <<endl;
else cout << "Impossible" << endl;
return 0;
}