日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

《挑战程序设计竞赛(疑惑)》19.2九宫格拼图

發(fā)布時(shí)間:2023/12/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《挑战程序设计竞赛(疑惑)》19.2九宫格拼图 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

不知道為什么用二維數(shù)組來實(shí)現(xiàn)不能,留給未來的我來解決吧。

#define N 3 #define M 4#include <iostream> #include<string> #include<queue> #include<map> using namespace std; struct Puzzle {int grids[N][N];//拼圖坐標(biāo)的二維數(shù)組描述string path = "";//路徑int space_x;int space_y;//目標(biāo)點(diǎn)的坐標(biāo)bool operator < (const Puzzle &p) const{for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){if (grids[i][j] == p.grids[i][j]) continue;return grids[i][j]< p.grids[i][j];}}return false;} }; //確立方向的變化 static const char dir[M] = {'L','R','U','D'}; static const int dx[M] = {0, 0, -1, 1}; static const int dy[M] = {-1, 1, 0, 0}; bool is_target(Puzzle tp)//判斷是否到達(dá)目標(biāo)狀態(tài) {for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){if (tp.grids[i][j] != (3 * i + j + 1)){return false;}}}return true; }string bfs(Puzzle tp)//用寬度搜索進(jìn)行狀態(tài)轉(zhuǎn)移 {Puzzle tp2;queue<Puzzle> qu;map<Puzzle, bool>Cut;tp.path = "";qu.push(tp);Cut[tp] = true;while(!qu.empty()){printf("1\n");tp2 = qu.front(); qu.pop();if (is_target(tp2)) {return tp2.path;}for (int i = 0; i < M; i++){int space_x = tp2.space_x+dx[i];int space_y = tp2.space_y+dy[i];if (space_x < 0 || space_y < 0 || space_x >= N || space_y >= N){continue;}swap(tp2.grids[tp2.space_x][tp2.space_y], tp2.grids[space_x][space_y]);tp2.space_x = space_x;tp2.space_y = space_y;if (!Cut[tp2]){Cut[tp2] = true;tp2.path += dir[i];qu.push(tp2);}}}return "unsolvable"; }int main() {Puzzle sp;//初始化拼圖坐標(biāo)for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){cin>>sp.grids[i][j];if (sp.grids[i][j] == 0){sp.grids[i][j] = N*3;sp.space_x = i;sp.space_y = j;}}}for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){cout << sp.grids[i][j]<<" ";}cout<<"\n";}cout << is_target(sp)<<"\n";string ans = bfs(sp);cout << ans;system("pause");return 0; }

總結(jié)

以上是生活随笔為你收集整理的《挑战程序设计竞赛(疑惑)》19.2九宫格拼图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。