一开始的时候我直接先把nexts[i]转成string在原来的string中搜索,但是这样根本就得不出来结果,找了半天才发现,比如当我存进14后用这种方法就不能再存1和4了。。。。坑死我了。。。哎。。。自己挖的坑就是蛋疼。。。。
AC代码如下:
#include <iostream>
#include <string>
using namespace std;
int nexts[21][3];
int number;
int M;
int mark[21];
void DFS( string s, int m, int now ){
if( now >= 20 ){
if( nexts[m][0] == M || nexts[m][1] == M || nexts[m][2] == M ){
cout << number++ << ": " << s << " " << M << endl;
}
return;
}
for( int i = 0; i < 3; i++ ){
if( mark[nexts[m][i]] == 0 ){
string c = "";
if( nexts[m][i] < 10 ){
c += nexts[m][i] + '0';
}else{
c += nexts[m][i] / 10 + '0';
c += nexts[m][i] % 10 + '0';
}
mark[nexts[m][i]] = 1;
DFS( s + " "+ c, nexts[m][i], now + 1 );
mark[nexts[m][i]] = 0;
}
}
}
int main(){
for( int i = 1; i <= 20; i++ ){
cin >> nexts[i][0] >> nexts[i][1] >> nexts[i][2];
for( int j = 2; j > 0; j-- ){
for( int k = 0; k < j; k++ ){
if( nexts[i][k] > nexts[i][k+1] ){
int t = nexts[i][k];
nexts[i][k] = nexts[i][k+1];
nexts[i][k+1] = t;
}
}
}
}
while( cin >> M && M != 0 ){
number = 1;
memset( mark, 0, sizeof( mark ) );
mark[M] = 1;
string c = "";
if( M < 10 ){
c += M + '0';
}else{
c += M / 10 + '0';
c += M % 10 + '0';
}
DFS( c, M, 1 );
}
return 0;
}