Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:这个题目好难,不敢下手,有没思路详细点的In Reply To:这个题目好难,不敢下手,有没思路详细点的 Posted by:goodpeter at 2007-08-19 01:30:47 #include <iostream> #include <queue> using namespace std; typedef struct{ int num[6]; int pos; int step; }code; queue<code> q; bool app[7][10][10][10][10][10][10];//用来判重的 int final[6]; int i; code newone; int u, v; int best; bool cmp(int a[6], int b[6]) { int i; for(i = 0; i < 6; i++) { if(a[i] != b[i]) { return false; } } return true; } int main() { code start; for(i = 0; i < 6; i++) { char a; cin>>a; start.num[i] = (int)(a - '0'); } start.pos = 1; start.step = 1; q.push(start); memset(app, false, sizeof(app)); for(i = 0; i < 6; i++) { char b; cin>>b; final[i] = (int)(b - '0'); } while(!q.empty()) { code temp = q.front(); q.pop(); if(cmp(temp.num, final)) { best = temp.step; break; } int tmppos = temp.pos; if((tmppos > 1) && !app[tmppos - 1][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]]) { for(i = 0; i < 6; i++) { newone.num[i] = temp.num[i]; } newone.step = temp.step + 1; newone.pos = tmppos - 1; app[tmppos - 1][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]] = true; q.push(newone); } if((tmppos < 6) && !app[tmppos + 1][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]]) { for(i = 0; i < 6; i++) { newone.num[i] = temp.num[i]; } newone.step = temp.step + 1; newone.pos = tmppos + 1; app[tmppos + 1][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]] = true; q.push(newone); } if(temp.num[tmppos] > 0) { temp.num[tmppos] = temp.num[tmppos] - 1; if(!app[tmppos][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]]) { for(i = 0; i < 6; i++) { newone.num[i] = temp.num[i]; } newone.step = temp.step + 1; newone.pos = tmppos; app[tmppos][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]] = true; q.push(newone); } temp.num[tmppos] = temp.num[tmppos] + 1; } if(temp.num[tmppos] < 9) { temp.num[tmppos] = temp.num[tmppos] + 1; if(!app[tmppos][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]]) { for(i = 0; i < 6; i++) { newone.num[i] = temp.num[i]; } newone.step = temp.step + 1; newone.pos = tmppos; app[tmppos][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]] = true; q.push(newone); } temp.num[tmppos] = temp.num[tmppos] - 1; } u = temp.num[tmppos]; v = temp.num[0]; temp.num[0] = u; temp.num[tmppos] = v; if(!app[tmppos][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]]) { for(i = 0; i < 6; i++) { newone.num[i] = temp.num[i]; } newone.step = temp.step + 1; newone.pos = tmppos; app[tmppos][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]] = true; q.push(newone); } temp.num[0] = v; temp.num[tmppos] = temp.num[5]; temp.num[5] = u; if(!app[tmppos][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]]) { for(i = 0; i < 6; i++) { newone.num[i] = temp.num[i]; } newone.step = temp.step + 1; newone.pos = tmppos; app[tmppos][temp.num[0]][temp.num[1]][temp.num[2]][temp.num[3]][temp.num[4]][temp.num[5]] = true; q.push(newone); } } cout<<best<<endl; return 0; } 我的程序tle,不知怎么优化,谁能帮我看看? Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator