1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
#include <iostream> #include <stack> #include <time.h>
typedef struct Data{ int data[9]; int value; int step; int lastAction; Data *next; Data *last; }Data, *DataList;
int tempPosition[9][2] = { {0, 5}, {1, 7}, {2, 6}, {3, 13}, {4, 15}, {5, 14}, {6, 9}, {7, 11}, {8, 10}, };
int distance[9][9] = { {0, 1, 2, 1, 2, 3, 2, 3, 4}, {1, 0, 1, 2, 1, 2, 3, 2, 3}, {2, 1, 0, 3, 2, 1, 4, 3, 2}, {1, 2, 3, 0, 1, 2, 1, 2, 3}, {2, 1, 2, 1, 0, 1, 2, 1, 2}, {3, 2, 1, 2, 1, 0, 3, 2, 1}, {2, 3, 4, 1, 2, 3, 0, 1, 2}, {3, 2, 3, 2, 1, 2, 1, 0, 1}, {4, 3, 2, 3, 2, 1, 2, 1, 0}, };
int GetNextAction(Data data){ int *temp = data.data; int i = 0; for(i = 0; i < 9; i++){ if(temp[i] == 0){ break; } } int tp = tempPosition[i][1]; int last = data.lastAction; if(last == 1){ tp = tp - 4; }else if(last == 2){ tp = tp - 8; }else if(last == 3){ tp = tp - 1; }else if(last == 4){ tp = tp - 2; } return tp; }
int Judge(int *arr1, int *arr2){ for(int i = 0; i < 9; i++){ if(arr1[i] != arr2[i]){ return 0; } } return 1; }
void Print(Data data){ for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ std::cout << data.data[i * 3 + j] << " "; } std::cout << std::endl; } std::cout << std::endl; }
void InitList(DataList &head){ head = (Data *)malloc(sizeof(Data)); head->lastAction = 0; head->next = NULL; head->last = NULL; }
int Gn(int *arr){ int sum = 0; for(int i = 0; i < 9; i++){ sum += distance[arr[i]][i]; } return sum; }
void InsertList(DataList &head, int *arr, int value, int lastAction){ Data *temp = head; Data *newNode = (Data *)malloc(sizeof(Data));
for(int i = 0; i < 9; i++){ newNode->data[i] = arr[i]; } newNode->value = value; newNode->step = head->step + 1; newNode->next = NULL; newNode->last = head; while(temp->next != NULL){ if(temp->next->value < value){ temp = temp->next; }else{ break; } } newNode->next = temp->next; temp->next = newNode; }
int main(){ int *originalData = new int[9]; int *targetArray = new int[9]; int *arraytest1 = new int[9]; int *arraytest2 = new int[9]; std::cout << "请输入原始数据:" << std::endl; for(int i = 0; i < 9; i++){ std::cin >> originalData[i]; } std::cout << "请输入变换后数据:" << std::endl; for(int i = 0; i < 9; i++){ std::cin >> targetArray[i]; } for(int i = 0; i < 9; i++){ arraytest1[i] = originalData[i]; arraytest2[i] = targetArray[i]; } int cnt1 = 0, cnt2 = 0; for(int i = 0; i < 9; i++){ if(arraytest1[i] == 0){ continue; } for(int j = i - 1; j >= 0; j--){ if(arraytest1[j] > arraytest1[i]){ cnt1++; } } } for(int i = 0; i < 9; i++){ if(arraytest2[i] == 0){ continue; } for(int j = i - 1; j >= 0; j--){ if(arraytest2[j] > arraytest2[i]){ cnt2++; } } } if(cnt1%2 != cnt2%2){ std::cout << "E0: 输入有误!问题无解" << std::endl; return 0; } for(int i = 0; i < 9; i++){ for(int j = 0; j < 9; j++){ if(arraytest1[i] < arraytest1[j]){ int temp = arraytest1[i]; arraytest1[i] = arraytest1[j]; arraytest1[j] = temp; } } } for(int i = 0; i < 9; i++){ for(int j = 0; j < 9; j++){ if(arraytest2[i] < arraytest2[j]){ int temp = arraytest2[i]; arraytest2[i] = arraytest2[j]; arraytest2[j] = temp; } } } for(int i = 0; i < 9; i++){ if(arraytest1[i] != arraytest2[i]){ std::cout << "E1: 输入有误!请检查输入" << std::endl; return 0; } } delete[] arraytest1; delete[] arraytest2; clock_t start, finish; double Total_time; start = clock(); DataList head; InitList(head); head->step = -1; DataList temp = head; InsertList(head, originalData, Gn(originalData), 0); while(true){ temp = temp->next; if(Judge(targetArray, temp->data)){ std::cout << "找到了!需要" << temp->step << "步" << std::endl; break; } int i = 0; for(i = 0; i < 9; i++){ if(temp->data[i] == 0){ break; } } int nextAction = GetNextAction(*temp); if(nextAction>=8){ int arrayList[9]; for(int j = 0; j < 9; j++){ arrayList[j] = temp->data[j]; } arrayList[i] = arrayList[i-3]; arrayList[i-3] = 0; InsertList(temp, arrayList, Gn(arrayList)+temp->step, 1); } if(nextAction%8>=4){ int arrayList[9]; for(int j = 0; j < 9; j++){ arrayList[j] = temp->data[j]; } arrayList[i] = arrayList[i+3]; arrayList[i+3] = 0; InsertList(temp, arrayList, Gn(arrayList)+temp->step, 2); } if(nextAction%8%4>=2){ int arrayList[9]; for(int j = 0; j < 9; j++){ arrayList[j] = temp->data[j]; } arrayList[i] = arrayList[i-1]; arrayList[i-1] = 0; InsertList(temp, arrayList, Gn(arrayList)+temp->step, 3); } if(nextAction%8>=4){ int arrayList[9]; for(int j = 0; j < 9; j++){ arrayList[j] = temp->data[j]; } arrayList[i] = arrayList[i+1]; arrayList[i+1] = 0; InsertList(temp, arrayList, Gn(arrayList)+temp->step, 4); } } finish = clock(); std::stack<int *>s; int steps = temp->step; std::cout << "步骤为:" << std::endl; while(temp->last != NULL){ s.push(temp->data); temp = temp->last; } while(!s.empty()){ int *arr = s.top(); s.pop(); for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ std::cout << arr[i * 3 + j] << " "; } std::cout << std::endl; } std::cout << std::endl; } std::cout << steps << "步,搜索用时:" << (double)(finish - start) / CLOCKS_PER_SEC << "s" << std::endl; return 0; }
|