| ||||||||||
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 |
5555,WA了一星期了,救救。// Qsort.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <string.h> using namespace std; const int maxn = 362880 + 5; typedef struct Status { int v[10]; int flag; int px; char step[100]; }Status; typedef struct Hash { Status *node; int flag; }Hash; Hash hashTable[maxn]; Status *startNode; Status *endNode; Status* fstQueue[maxn]; int fsthead = 0; int fsttail = 0; Status* sndQueue[maxn]; int sndhead = 0; int sndtail = 0; int d[4] = {-1,1,-3,3}; char dc[4] = {'l','r','u','d'}; char resultStep[maxn]; int resultLen = 0; long PermutationToNumber(int *v) { long result = 0; long factorials = 1; for(int j = 2; j <= 9; j++) { int count = 0; for(int i = 1; i < j; i++) { if(v[j] < v[i]) count++; } result += count*factorials; factorials *= j; } return result; } int GetHashValue(Status *keyNode) { long key = PermutationToNumber(keyNode->v); if(hashTable[key].flag == 0) { hashTable[key].node = keyNode; hashTable[key].flag = keyNode->flag; return 0; } return hashTable[key].flag; } void ShowNode(Status *node) { if(node ->flag == 1) { cout << node->step; strcpy(resultStep,node->step); resultLen = strlen(node->step); } else { for(int i = strlen(node->step) -1 ;i >=0; i--) { char c = node->step[i]; if( c =='l') { cout<<'r'; resultStep[resultLen++] = 'r'; } if(c == 'r') { cout<<'l'; resultStep[resultLen++] = 'l'; } if(c == 'd') { cout<<'u';resultStep[resultLen++] = 'u'; } if(c == 'u') { cout<<'d';resultStep[resultLen++] = 'd'; } } } } void ShowPath(Status *node) { long key = PermutationToNumber(node->v); Status *sameNode = hashTable[key].node; if(node->flag == 1) { ShowNode(node); ShowNode(sameNode); } else { ShowNode(sameNode); ShowNode(node); } } int ExtendNode(Status *curNode) { int curp = curNode->px; for(int i = 0; i < 4; i++) { int newp = curp + d[i]; if(newp < 1 || newp > 9) continue; Status *extend = (Status*) malloc(sizeof(Status)); extend->px = newp; extend->flag = curNode->flag; strcpy(extend->step,curNode->step); extend->step[strlen(extend->step)] = dc[i]; extend->step[strlen(curNode->step) + 1] = '\0'; for(int j =1 ;j <= 9;j++) { extend->v[j] = curNode->v[j]; } int tmp = extend->v[newp]; extend->v[newp] = extend->v[curp]; extend->v[curp] = tmp; int ret = GetHashValue(extend); if( ret == 0) { if(extend->flag == 1) fstQueue[++fsttail] = extend; else sndQueue[++sndtail] = extend; } else if( ret == curNode->flag) continue; else { ShowPath(extend); return 1; } } return 0; } void BFS() { startNode->flag = 1; fstQueue[++fsttail] = startNode; endNode->flag = 2; sndQueue[++sndtail] = endNode; int step = 1; while(fsttail > fsthead || sndtail > sndhead) { Status *curNode; if(step == 1 && fsttail > fsthead) { curNode = fstQueue[++fsthead]; step = 2; } else if(sndtail > sndhead) { curNode = sndQueue[++sndhead]; step = 1; } if(ExtendNode(curNode)) break; } } void InitPara() { startNode = (Status*) malloc(sizeof(Status)); endNode = (Status*) malloc(sizeof(Status)); for(int i = 1; i <= 9 ;i++) { char c; cin >> c; if( c == 'x') { startNode->v[i] = 9; startNode->px = i; } else startNode->v[i] = c - '0'; } startNode->step[0] = '\0'; endNode->step[0] = '\0'; for(int i = 1; i <= 9 ;i++) { endNode->v[i] = i; } endNode->px = 9; for(int i = 0; i <= maxn ;i++) { hashTable[i].flag = 0; } } int CheckStartNode() { int cont = 0; for(int i = 2; i<= 9; i++) { if(startNode->v[i] == 9) continue; for(int j = 1; j < i; j++) { if(startNode->v[j] == 9) continue; if(startNode->v[j] > startNode->v[i]) cont++; } } if(cont % 2 != 0) { cout << "unsolvable"<<endl; return 0; } return 1; } void Test() { Status * ptr = startNode; for(int j = 1; j<= 9; j++) cout<<ptr->v[j] << " "; cout<<endl; for(int i = 0; i < resultLen; i++) { char c = resultStep[i]; int curp = ptr ->px; int newp; cout<<c<<endl; if(c == 'l') newp = curp - 1; if(c == 'r') newp = curp + 1; if(c == 'd') newp = curp + 3; if(c == 'u') newp = curp - 3; int tmp = ptr->v[newp]; ptr->v[newp] = ptr->v[curp]; ptr->v[curp] = tmp; ptr->px = newp; for(int j = 1; j<= 9; j++) cout<<ptr->v[j] << " "; cout<<endl; } } int main() { InitPara(); if(CheckStartNode()) BFS(); //int n; //cin >>n; // cout<<endl; // Test(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator