n*m的矩阵只能向右读数,相当于一层又一层的 嵌套,然后根据读取的顺序,输入两行等长的数字1-n*m,第一行和和第二行是一一对应的,按照读取的顺序输出第二行的解密密码。
C++
#include<bits/stdc++.h> using namespace std; const int maxn = 1e3 + 10; int a[maxn][maxn]; int b[maxn * maxn + 10]; int c[maxn * maxn + 10]; int cnt = 1; void work(int n, int m) { if (n == 1) { for (int i = 1; i <= m; i++) cout<<b[a[1][i]]; return ; } else if (m == 1) { for (int i = 1; i <= n; i++) cout<<b[a[i][1]]; return ; } else { int i = 1, j = 1; int sum = n * m; while (sum>0) { for (int k = 1; k < m; k++) { sum--; cout<<b[a[i][j]]; j++; } for (int k = 1; k < n; k++) { sum--; cout<<b[a[i][j]]; i++; } for (int k = 1; k < m; k++) { sum--; cout<<b[a[i][j]]; j--; } for (int k = 1; k < n; k++) { sum--; cout<<b[a[i][j]]; i--; } i++; j++; n -= 2; m -= 2; if (n == 1) { for (int k = 1; k <= m; k++) { sum--; cout<<b[a[i][j]]; j++; } } else if (m == 1) { for (int k = 1; k <= n; k++) { sum--; cout<<b[a[i][j]]; i++; } } } } } int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0); int n, m; 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 * m; i++) cin >> c[i]; for (int i = 1; i <= n * m; i++) cin>>b[c[i]]; work(n, m); return 0; }
C
1 #include<stdio.h> 2 int a[1001][1001], b[1001 * 1001], c[1001 * 1001], A, B, C; 3 int main() 4 { 5 int n, m, i, j; 6 scanf("%d%d", &n, &m); 7 for (i = 0; i < n; i++) 8 for (j = 0; j < m; j++) 9 scanf("%d", &a[i][j]); 10 for (i = 0; i < n * m; i++) 11 scanf("%d", &b[i]); 12 for (i = 0; i < n * m; i++) 13 scanf("%d", &c[b[i] - 1]); 14 for (A = 0, B = n - 1, C = m - 1; A <= B && A <= C; A++, B--, C--) 15 { 16 if (A == B) 17 { 18 for (i = A, j = A; j <= C; j++) 19 printf("%d", c[a[i][j] - 1]); 20 continue; 21 } 22 if (A == C) 23 { 24 for (i = A, j = A; i <= B; i++) 25 printf("%d", c[a[i][j] - 1]); 26 continue; 27 } 28 for (i = A, j = A; j <= C; j++) 29 printf("%d", c[a[i][j] - 1]); 30 for (i = A + 1, j = C; i <= B - 1; i++) 31 printf("%d", c[a[i][j] - 1]); 32 for (i = B, j = C; j >= A; j--) 33 printf("%d", c[a[i][j] - 1]); 34 for (i = B - 1, j = A; i >= A + 1; i--) 35 printf("%d", c[a[i][j] - 1]); 36 } 37 return 0; 38 }