題意:給你兩個網格,a和b,都是n行和 m?列。網格中的所有數值都是?0?,?1?或?2?。
您可以多次對?a𝑎?執行以下操作:
- 選取網格中任意一個長寬的子矩形。您可以選擇整個網格作為子矩形。
- 子矩形有四個角。取所選子矩形中任意一對斜對角,并將它們的值加上?1?,模為?3?。
- 對于未選中的一對角,在它們的值上加上?2 modulo?3?。
需要注意的是,此操作只改變被選中的子矩形的角的值。
是否可以通過任意次數(可能為零)的上述操作將網格?a?轉換為網格?b?
在模3的情況下顯然都一定不會改變每一行或每一列的和
#include<bits/stdc++.h>
using namespace std;
void sol(){
?? ?int n,m;cin>>n>>m;
?? ?vector<int>r(n),c(m);
?? ?for(int i=0;i<n;i++){
?? ??? ?string s;cin>>s;
?? ??? ?for(int j=0;j<m;j++){
?? ??? ?r[i]+=s[j]-'0';
?? ??? ?c[j]+=s[j]-'0';?? ?
?? ??? ?}
?? ?}
?? ?for(int i=0;i<n;i++){
?? ??? ?string s;cin>>s;
?? ??? ?for(int j=0;j<m;j++){
?? ??? ?r[i]-=s[j]-'0';
?? ??? ?c[j]-=s[j]-'0';?? ?
?? ??? ?}
?? ?}
?? ?int k=0;
?? ?for(int i=0;i<n;i++){
?? ??? ?if(r[i]%3!=0){
?? ??? ??? ?k=1;
?? ??? ?}
?? ?}
?? ?for(int i=0;i<m;i++){
?? ??? ?if(c[i]%3!=0){
?? ??? ??? ?k=1;
?? ??? ?}
?? ?}
?? ?if(k==1)cout<<"NO"<<endl;
?? ?else cout<<"YES"<<endl;
}
int main(){
?? ?int t;cin>>t;
?? ?while(t--)sol();
?? ?return 0;
}