跳过正文
  1. Posts/

poj 3087 Shuffle'm Up (bfs)

·1 分钟

http://poj.org/problem?id=3087

用bfs写的,但是其实就是个模拟啊喂!

只有一种操作,何谈最短? 一直往下写就行了.

有一点疑惑,就是map的初始值

比如我定义的 map<string,int>d;它的初始的value是什么?随机值?0?还是什么,百度了下,没找到,求指教.

 1
 2    #include<iostream>
 3    #include<iomanip>
 4    #include<cstdio>
 5    #include<algorithm>
 6    #include<cmath>
 7    #include<cstring>
 8    #include<string>
 9    #include<map>
10    #include<set>
11    #include<queue>
12    #include<vector>
13    #include<stack>
14    #define y0 abc111qqz
15    #define y1 hust111qqz
16    #define yn hez111qqz
17    #define j1 cute111qqz
18    #define tm crazy111qqz
19    #define lr dying111qqz
20    using namespace std;
21    #define REP(i, n) for (int i=0;i<int(n);++i)
22    typedef long long LL;
23    typedef unsigned long long ULL;
24    map<string,int>d;
25    string st1,st2,tar;
26    int n;
27    bool flag;
28    string add(string a,string b)
29    {
30        string res="";
31        int len = a.length();
32        for ( int i = 0 ; i < len ; i++ )
33        {
34    	  res+=b[i];
35    	  res+=a[i];
36        }
37        return res;
38    }
39    void bfs()
40    {
41        queue<string>q;
42        string str=add(st1,st2);
43        q.push(str);
44        d[str]=1;
45        while (!q.empty())
46        {
47    	  string pst = q.front();
48    	  q.pop();
49    	  if (pst==tar)
50    	  {
51    		flag = true;
52    		return;
53    	  }
54    	  string s1=pst.substr(0,n);
55    	  string s2=pst.substr(n,n);
56    	  string tmps = add(s1,s2);
57    	  if (d[tmps]>0)
58    	  {
59    		flag = false;
60    		return;
61    	  }
62    	  d[tmps]=d[pst]+1;
63    	  q.push(tmps);
64        }
65    }
66    int main()
67    {
68        int T;
69        int cas = 0;
70        cin>>T;
71
72        while (T--)
73        {
74    	  d.clear();
75    	  cas++;
76    	  cin>>n;
77    	  cin>>st1>>st2>>tar;
78    	  bfs();
79    	  if (flag)
80    	  {
81    		cout<<cas<<" "<<d[tar]<<endl;
82    	  }
83    	  else
84    	  {
85    		cout<<cas<<" "<<-1<<endl;
86    	  }
87        }
88
89    	return 0;
90    }

相关文章

poj 3126 Prime Path (bfs)

·2 分钟
http://poj.org/problem?id=3126 题意是说,给定两个四位素数a b 问从a变换到b,最少需要变换几次. 变换的要求是,每次只能改变一个数字,而且中间过程得到的四位数也必须为素数. 因为提到最少变换几次,容易想到bfs,bfs第一次搜到的一定是最短步数.

poj 3278 catch that cow

·1 分钟
http://poj.org/problem?id=3278 bfs,用到了stl的queue 1 2 3 /* *********************************************** 4 Author :111qqz 5 Created Time :2016年02月19日 星期五 15时45分05秒 6 File Name :3278.cpp 7 ************************************************ */ 8 9 #include <algorithm> 10 #include <cstdio> 11 #include <iostream> 12 #include <cstring> 13 #include <string> 14 #include <cmath> 15 #include <map> 16 #include <stack> 17 #include <queue> 18 19 using namespace std; 20 typedef long long LL; 21 const int inf = 8E8; 22 const int N=2E5+7; 23 int d[N]; 24 int n,k; 25 void bfs() 26 { 27 queue<int> q; 28 memset(d,-1,sizeof(d)); 29 q.push(n); 30 d[n]=0; 31 while (!q.empty()) 32 { 33 int x = q.front(); 34 q.pop(); 35 if ( x==k ) 36 { 37 break; 38 } 39 int next[10]; 40 next[1]=x-1; 41 next[2]=x+1; 42 next[3]=2*x; 43 for ( int i = 1; i <= 3 ; i++ ) 44 { 45 if (next[i]>=0&&next[i]<=100000&&d[next[i]]==-1) 46 { 47 d[next[i]]=d[x]+1; 48 q.push(next[i]); 49 } 50 } 51 } 52 53 54 } 55 int main() 56 { 57 while (scanf("%d %d",&n,&k)!=EOF) 58 { 59 bfs(); 60 cout<<d[k]<<endl; 61 } 62 return 0; 63 }

poj 3279 Fliptile (搜索..暴力?)

·2 分钟
http://poj.org/problem?id=3279 反转类问题. 有N*M个方格,每个上面有数字0或者1 操作一个方格,这个方格即其相邻的四个方格(有公共边)会改变状态(由0变1或者由1变0)