poj 1077 Eight(bfs,dbfs)

代码如下:

dbfs:

  1 #include <iostream>
  2 #include <map>
  3 #include <algorithm>
  4 #include <string>
  5 #include <queue>
  6 using namespace std;
  7 typedef long long LL;
  8
  9 int eight[3][3], xx, xy, dx[]={0,0,-1,1,0}, dy[]={0,1,0,0,-1};
 10 char dir[] = "orudl";
 11
 12 LL encode(){
 13     LL s=eight[2][2];
 14     for(int i=7; i>=0; --i){
 15         s <<=4;
 16         s |= eight[i/3][i%3];
 17     }
 18     return s;
 19 }
 20 void decode(LL s){
 21     for(int i=0; i<9; ++i){
 22         eight[i/3][i%3] = s%16;
 23         if(s%16 == 0)
 24             xx = i/3, xy = i%3;
 25         s >>=4;
 26     }
 27 }
 28 void print_state(LL s){
 29     printf("s:%d,xx,xy:", s);
 30     decode(s);
 31     printf("%d,%d\n",xx,xy);
 32     for(int i=0; i<3; ++i){
 33         for(int j=0; j<3; ++j)
 34             printf("%d ", eight[i][j]);
 35         printf("\n");
 36     }
 37
 38 }
 39 queue<LL> que[2];
 40 map<LL,pair<int,LL> > mp[2];// pair:d,ps
 41
 42 void print(LL s){
 43     int d = mp[0][s].first;
 44     LL ts=s;
 45     string ope;
 46     while(d!=-1){
 47         ope.push_back(dir[d]);
 48         ts = mp[0][ts].second;
 49         d = mp[0][ts].first;
 50     }
 51     reverse(ope.begin(), ope.end());
 52     cout<< ope;
 53     ope.clear(), d = mp[1][s].first, ts = s;
 54     while(d!=-1){
 55         ope.push_back(dir[d]);
 56         ts = mp[1][ts].second;
 57         d = mp[1][ts].first;
 58     }
 59     cout << ope << "\n";
 60 }
 61 void expand(int idx){
 62     static int found = 0;
 63     if(found){
 64         while(!que[idx].empty()) que[idx].pop();
 65         return;
 66     }
 67     int len = que[idx].size();
 68     while(len--){
 69         LL s = que[idx].front(), s2; que[idx].pop();
 70         for(int i=1; i<5; ++i){
 71             decode(s);
 72             int r,c;
 73             if(idx==0)
 74                 r = xx+dx[i], c = xy+dy[i];
 75             else
 76                 r = xx-dx[i], c = xy-dy[i];
 77             if(r<0 || c<0 || r==3 || c==3)
 78                 continue;
 79             swap(eight[xx][xy], eight[r][c]);
 80             s2 = encode();
 81             if(mp[idx].find(s2)!=mp[idx].end())
 82                 continue;
 83             mp[idx][s2] = make_pair(i,s);
 84             if(mp[idx^1].find(s2)!=mp[idx^1].end()){
 85                 print(s2);
 86                 found=1;
 87                 return ;
 88             }
 89             else que[idx].push(s2);
 90         }
 91     }
 92 }
 93 void dbfs(){
 94     mp[0].clear(), mp[1].clear();
 95     LL tar=8;
 96     for(int i=7; i>0; --i)
 97         tar<<=4, tar |= i;
 98     que[1].push(tar); mp[1][tar]=make_pair(-1,0);// 两者相等??
 99     tar = encode();
100     que[0].push(tar); mp[0][tar] = make_pair(-1,0);
101     while(!que[0].empty() && !que[1].empty()){
102         if(que[0].size()<que[1].size())
103             expand(0);
104         else expand(1);
105     }
106     while(!que[0].empty())
107         expand(0);
108     while(!que[1].empty())
109         expand(1);
110 }
111 int main(){
112     std::ios::sync_with_stdio(false);
113     char c;
114     for(int i=0; i<3; ++i){
115         for(int j=0; j<3; ++j){
116             cin>>c;
117             if(c==‘x‘)
118                 eight[i][j]=0, xx = i, xy = j;
119             else
120                 eight[i][j]= c-‘0‘;
121         }
122     }
123     int cnt=0;
124     for(int i=0; i<9; ++i){
125         for(int pi=0; pi<i; ++pi)
126             if(eight[pi/3][pi%3] && eight[pi/3][pi%3]<eight[i/3][i%3])
127                 cnt++;
128     }
129     if(cnt%2)
130         printf("unsolvable\n");
131     else
132         dbfs();
133     return 0;
134 }

bfs:

  1 #include <iostream>
  2 #include <map>
  3 #include <algorithm>
  4 #include <string>
  5 #include <queue>
  6 using namespace std;
  7 typedef long long LL;
  8
  9 int eight[3][3], xx, xy, dx[]={0,0,-1,1,0}, dy[]={0,1,0,0,-1};
 10 char dir[] = "orudl";
 11 LL tar;
 12
 13 LL encode(){
 14     LL s=eight[2][2];
 15     for(int i=7; i>=0; --i){
 16         s <<=4;
 17         s |= eight[i/3][i%3];
 18     }
 19     return s;
 20 }
 21 void decode(LL s){
 22     for(int i=0; i<9; ++i){
 23         eight[i/3][i%3] = s%16;
 24         if(s%16 == 0)
 25             xx = i/3, xy = i%3;
 26         s >>=4;
 27     }
 28 }
 29 void print_state(LL s){
 30     printf("s:%d,xx,xy:", s);
 31     decode(s);
 32     printf("%d,%d\n",xx,xy);
 33     for(int i=0; i<3; ++i){
 34         for(int j=0; j<3; ++j)
 35             printf("%d ", eight[i][j]);
 36         printf("\n");
 37     }
 38
 39 }
 40 queue<LL> que;
 41 map<LL,pair<int,LL> > mp;// pair:d,ps
 42
 43 void print(LL s){
 44     int d = mp[tar].first;
 45     LL ts=s;
 46     string ope;
 47     while(d!=-1){
 48         ope.push_back(dir[d]);
 49         ts = mp[ts].second;
 50         d = mp[ts].first;
 51     }
 52     reverse(ope.begin(), ope.end());
 53     cout<< ope<< "\n";
 54 }
 55 void bfs(){
 56     mp.clear();
 57     LL s = encode(), s2;
 58     que.push(s); mp[s] = make_pair(-1,0);
 59     while(!que.empty()){
 60         s = que.front(); que.pop();
 61         for(int i=1; i<5; ++i){
 62             decode(s);
 63             int r,c;
 64             r = xx+dx[i], c = xy+dy[i];
 65             if(r<0 || c<0 || r==3 || c==3)
 66                 continue;
 67             swap(eight[xx][xy], eight[r][c]);
 68             s2 = encode();
 69             if(mp.find(s2)!=mp.end())
 70                 continue;
 71             mp[s2] = make_pair(i,s);
 72             if(s2==tar){
 73                 print(s2);
 74                 return ;
 75             }
 76             else que.push(s2);
 77         }
 78     }
 79 }
 80 int main(){
 81     std::ios::sync_with_stdio(false);
 82     char c;
 83     for(int i=0; i<3; ++i){
 84         for(int j=0; j<3; ++j){
 85             cin>>c;
 86             if(c==‘x‘)
 87                 eight[i][j]=0, xx = i, xy = j;
 88             else
 89                 eight[i][j]= c-‘0‘;
 90         }
 91     }
 92     tar=8;
 93     for(int i=7; i>0; --i)
 94         tar<<=4, tar |= i;
 95     int cnt=0;
 96     for(int i=0; i<9; ++i){
 97         for(int pi=0; pi<i; ++pi)
 98             if(eight[pi/3][pi%3] && eight[pi/3][pi%3]<eight[i/3][i%3])
 99                 cnt++;
100     }
101     if(cnt%2)
102         printf("unsolvable\n");
103     else
104         bfs();
105     return 0;
106 }

时间: 2024-10-07 07:45:57

poj 1077 Eight(bfs,dbfs)的相关文章

POJ 1077 Eight(bfs+康托展开)

Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41040   Accepted: 16901   Special Judge Description 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

poj 3414 Pots(BFS+递归打印)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10255   Accepted: 4333   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

hdu5335(bfs,贪心)

In an n?mn?m maze, the right-bottom corner is the exit (position (n,m)(n,m) is the exit). In every position of this maze, there is either a 00 or a 11 written on it. An explorer gets lost in this grid. His position now is (1,1)(1,1), and he wants to

UVA 821 Page Hopping 网页跳跃(BFS,简单)

题意:给一个无权有向图,可认为边的长度为1,求两点间的平均长度(即所有点对的长度取平均),保留3位小数.保证任意点对都可达. 思路:简单题.直接穷举每个点,进行BFS求该点到其他点的距离.累加后除去边数即可. 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define pii pair<int,int> 4 #define INF 0x7f7f7f7f 5 using namespace std; 6 const int

POJ - 3414 Pots (BFS+路径记录)

题目链接:http://poj.org/problem?id=3414 题意:两个空杯子倒水,使得任意一个杯子中的水量达到题目要求,有6种操作:装满a,装满b,倒掉a,倒掉b,a->b,b->a. 题解:模拟一下操作,就好了. 1 #include <queue> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7

poj 1742 Coins (动态规划,背包问题)

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32977   Accepted: 11208 Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some

[ACM] POJ 2506 Tiling (递归,睑板)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7487   Accepted: 3661 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

poj 3414 pots (bfs 倒水问题)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int vis[120][120]; int vl,vr; int ok; int key; struct node { int l,r; int fa; int op; }; node str[100000]; int ans[100000]; void bfs() {

POJ 1101 The Game(BFS+判方向)

    The Game Description One morning, you wake up and think: "I am such a good programmer. Why not make some money?'' So you decide to write a computer game. The game takes place on a rectangular board consisting of w * h squares. Each square might o