ZOJ 1217 Eight(單向BFS+map)

Eight

題目傳送門~

Time Limit: 10000 msMemory Limit: 32768 KB

Scenario

The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as:

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15  x

where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:

 1  2  3  4     1  2  3  4     1  2  3  4     1  2  3  4
 5  6  7  8     5  6  7  8     5  6  7  8     5  6  7  8
 9  x 10 12     9 10  x 12     9 10 11 12     9 10 11 12
13 14 11 15    13 14 11 15    13 14  x 15    13 14 15  x
            r->            d->            r->

The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.


Input

You will receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x'. For example, this puzzle

 1  2  3
 x  4  6
 7  5  8

is described by this list:

1 2 3 x 4 6 7 5 8


Output

You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line. Do not print a blank line between cases.


Sample Input

 2  3  4  1  5  x  7  6  8


Sample Output

ullddrurdllurdruldr


這道題A的不容易呀!!!(看咱這內存耗得)


題後兩句話:

①啊啊啊啊啊這題真的不容易啊,思路就很難想,不容易A掉...

②這題本人犯迷糊了...(其實是沒理解清楚)

      

上面兩幅圖,左邊是本人用自己代碼輸入測試樣例跑出來的,然後右邊是系統給的output。

乍一看好像挺對,但仔細觀察後發現最後幾個操作符兩兩相反,於是我就懵*了嚶嚶嚶嚶...

然後反覆推敲,自己debug,搞各種測試發現好像完美無瑕...(其實是真的哈哈哈哈哈哈)

然後不甘心的情況下我就...選擇場外求助了

(特別鳴謝我的老師,講課好,還耐心的幫助了我~)

對!!!這個題的移動過程是不唯一的,我最後用我的運行結果手動模擬了一下,確定結果是一致的,然後提交上去就A掉了。

真是個心酸的故事...希望下次可以長點心hhhhhh

③迴歸正題,這題其實就是最強大腦裏的數字迷盤遊戲,我代碼思路的核心是深度優先搜索空格的移動位置來生成狀態空間轉換圖,然後直接判新的Input是否可以搜到,搜到就反向把路徑輸出來;找不到就直接say goodbye。(注意深度優先的時候反向來搜索,從goal開始更好寫算法,也更好理解)

④好的,接下來把圖模型整理上來就交作業遼~繼續加油吧!!!

 


這道題真的耗了我很大的心血,代碼奉上,請多指教~

#include<iostream>
#include<string>
#include<map>
#include<iterator>
#include<queue>
using namespace std;

map<string,string> mark;  //把狀態轉換圖記錄下來,前後轉換關係形成mark映射 
map<string,string>::iterator it;
string goal="12345678x"; 
queue<string> qstr;  //廣搜隊列 

void push_to_mark(string str1,string str2,int t1,int t2){  //映射函數 
	str2=str1; 
	str1[t2]='x';
	str1[t1]=str2[t2];  //建立新的狀態字符串str1 
	
	it=mark.find(str1);
	if(it==mark.end()){  //如果mark中沒有str1狀態,則將其加入mark 
		mark[str1]=str2;  //str1是由str2轉換來的 
		qstr.push(str1);  //將當前搜索到的狀態放入隊列 
	} 	
}

void Search(){
	//初始化 
	string s1=goal,s2;
	qstr.push(s1);
	mark[s1]=s1;  //方便最後的判斷,直接判映射值和goal是否相同即可 
	int t1,t2;
	
	while(!qstr.empty()){
		s1=qstr.front(); qstr.pop();//取出待搜索狀態 
		for(t1=0;t1<9;t1++) if(s1[t1]=='x') break;  //查到x位置立即彈出
		if(t1/3!=0)  { t2=t1-3; push_to_mark(s1,s2,t1,t2); }  //由於不是最上面的一列,所以可以上移一格
		if(t1/3!=2)  { t2=t1+3; push_to_mark(s1,s2,t1,t2); }  //由於不是最下面的一列,所以可以下移一格
		if(t1%3!=0)  { t2=t1-1; push_to_mark(s1,s2,t1,t2); }  //由於不是最左邊的一列,所以可以左移一格
		if(t1%3!=2)  { t2=t1+1; push_to_mark(s1,s2,t1,t2); }  //由於不是最右邊的一列,所以可以右移一格		 
	}
}

int main(){
	//初始化 
	int ans1,ans2;
	string str,markstr;
	char ch;
	mark.clear();
	
	Search();  //記錄狀態轉換圖 
	while(cin>>ch){
		str="";str+=ch;
		for(int i=1;i<9;i++) { cin>>ch; str+=ch;}
		it=mark.find(str);
		if(it==mark.end()) cout<<"unsolvable"<<endl;  //mark中找不到str則輸出unsolvable 
		else{
			while(str!=goal){
				it=mark.find(str);
				markstr=it->second;
				for(ans1=0;ans1<9;ans1++) if(str[ans1]=='x') break;
				for(ans2=0;ans2<9;ans2++) if(markstr[ans2]=='x') break;
				int ans=ans2-ans1;
				
				if(ans==-3)      cout<<"u";
				else if(ans==3)  cout<<"d";
				else if(ans==1)  cout<<"r";
				else if(ans==-1) cout<<"l";
		
				str=markstr;
			}
			cout<<endl;
		}		
	} 
	return 0;
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章