题目大意:输入s1,s2按图方式合成s12如果s12与输入的目标一致,则完成,否则将下部分给s1,上部分给s2,直到完成,或,确认不能完成目标。输出最少次数,不完成则-1。
解题思路:不太懂为什么要用bfs,直接模拟就好了。。。或者暴力到10000他也会ac
ac代码:
#include <iostream>
#include <string>
using namespace std;
int n, m, temp, count=1;
string a, b, c;
string change(string &a, string &b)
{string temp3="";int len=a.size();for (int i=0; i<2*len; i++){if (i%2)temp3 += a[i/2];elsetemp3 += b[i/2];}a = b = "";for (int i=0; i<2*len; i++){if (i<len)a += temp3[i];elseb += temp3[i];}
return temp3;
}int bfs()
{ string tt=a, kk=b, temp2;temp2 = change(tt, kk);int cnt = 1;while (!(tt == a && kk == b)){if (temp2 == c)return cnt;temp2 = change(tt, kk);cnt++;}
return 0;
}int main()
{scanf("%d", &n);while (n--){scanf("%d", &m);cin >> a >> b >> c;temp = bfs();if (temp)printf("%d %d\n", count++, temp);elseprintf("%d -1\n", count++);}
return 0;
}