分析:
DFS,注意判断是否遍历结束,返回false
代码示例:
#include<bits/stdc++.h>
using namespace std;
int n,k;
string s,t;
struct {int op;int x;int y;
}cha[10];
int vis[10];
bool dfs(int dep){if(s==t)return true;if(dep==k+1)return false;for(int i=1;i<=k;i++){if(!vis[i]){if(cha[i].op==2){swap(s[cha[i].x],s[cha[i].y]);}else{int nw=s[cha[i].x]-'0';nw=(nw+cha[i].y)%10;s[cha[i].x]=nw+'0';}vis[i]=1;if(dfs(dep+1))return true;vis[i]=0;if(cha[i].op==2){swap(s[cha[i].x],s[cha[i].y]);}else{int nw=s[cha[i].x]-'0';nw=(nw-cha[i].y+10)%10;s[cha[i].x]=nw+'0';}}}return false;
}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);cin>>n;cin>>s;cin>>t;cin>>k;for(int i=1;i<=k;i++){cin>>cha[i].op>>cha[i].x>>cha[i].y;}if(dfs(1))cout<<"Yes\n";else cout<<"No\n";return 0;
}