非常可乐
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 阅读全文 »
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 阅读全文 »
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Sample Output
66
88
66
Author
yifenfei
Source
题目类型:BFS+贪心
算法分析:首先贪心可以得到到’@’最短的距离是Yifenfei和Merceki分别到该位置时的距离和的最小值,此时可以调用两次BFS来将两人到所有’@’的距离求出来,然后更新最小值即可
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 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; char g[266][266]; int row, col, x2, y2, x3, y3, aa[266*266], bb[266*266], flag[266][266], tot; bool vis[266][266]; struct node { int a, b, cnt; node (int ax, int bx, int ccnt) : a (ax), b (bx), cnt (ccnt) {} node () {} }; void bfs (int x, int y, int f) { memset (vis, false, sizeof (vis)); vis[x][y] = true; queue <node> qu; qu.push (node (x, y, 0)); while (!qu.empty ()) { node tt = qu.front (); qu.pop (); if (g[tt.a][tt.b] == '@') { if (f == 1) aa[flag[tt.a][tt.b]] = tt.cnt; if (f == 2) bb[flag[tt.a][tt.b]] = tt.cnt; } for (int i = 0; i < 4; i++) { int tx = tt.a + dx[i], ty = tt.b + dy[i]; if (tx >= 0 && tx <= row - 1 && ty >= 0 && ty <= col - 1 && !vis[tx][ty] && g[tx][ty] != '#') { vis[tx][ty] = true; qu.push (node (tx, ty, tt.cnt + 1)); } } } } int main() { // CFF; while (scanf ("%d %d", &row, &col) != EOF) { for (int i = 0; i < row; i++) scanf (" %s", g[i]); tot = 0; for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) { if (g[i][j] == 'Y') x2 = i, y2 = j; if (g[i][j] == 'M') x3 = i, y3 = j; if (g[i][j] == '@') flag[i][j] = tot++; } fill (aa, aa + (266 * 266), INF); fill (bb, bb + (266 * 266), INF); bfs (x2, y2, 1); bfs (x3, y3, 2); int minval = INF; for (int i = 0; i < tot; i++) if (aa[i] < INF && bb[i] < INF) minval = min (minval, aa[i] + bb[i]); printf ("%d\n", minval * 11); } return 0; } |
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it. Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The
following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
C characters, and each of these characters is one of:
There will be exactly one J in each test case.
Output
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output
3
IMPOSSIBLE
题目类型:BFS
算法分析:同时扩展Joe可以走的位置和着火的区域(这里分别是将两个扩展的时间同步来实现的),注意图中可以存在多个Fire的地方!!!
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 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; char g[1666][1666]; int row, col, sx, sy, len; bool vis[1666][1666], vvis[1666][1666]; struct node { int a, b, cnt; node (int aa, int bb, int ccnt) : a (aa), b (bb), cnt (ccnt) {} node () {} }; node fire[1666*1666]; int bfs () { memset (vis, false, sizeof(vis)); memset (vvis, false, sizeof (vvis)); vis[sx+6][sy+6] = true; for (int i = 0; i < len; i++) vvis[fire[i].a+6][fire[i].b+6] = true; queue <node> qua, qub; qua.push (node (sx, sy, 0)); for (int i = 0; i < len; i++) qub.push (node (fire[i].a, fire[i].b, 0)); int pp = 0; while (1) { if (qua.empty ()) return -1; while (!qua.empty ()) { node tt = qua.front (); if (tt.cnt > pp) break; qua.pop (); if (tt.a == -1 || tt.b == -1 || tt.a == row || tt.b == col) return tt.cnt; if (vvis[tt.a+6][tt.b+6]) continue; for (int i = 0; i < 4; i++) { int tx = tt.a + dx[i], ty = tt.b + dy[i]; if (tx >= 0 && tx <= row - 1 && ty >= 0 && ty <= col - 1 && g[tx][ty] == '.' && !vis[tx+6][ty+6] && !vvis[tx+6][ty+6]) { vis[tx+6][ty+6] = true; qua.push (node (tx, ty, tt.cnt + 1)); } if (tx == -1 || tx == row || ty == -1 || ty == col) qua.push (node (tx, ty, tt.cnt + 1)); } } if (qua.empty ()) return -1; while (!qub.empty ()) { node ttt = qub.front (); if (ttt.cnt > pp) break; qub.pop (); for (int i = 0; i < 4; i++) { int tx = ttt.a + dx[i], ty = ttt.b + dy[i]; if (tx >= 0 && tx <= row - 1 && ty >= 0 && ty <= col - 1 && !vvis[tx+6][ty+6] && g[tx][ty] != '#') { vvis[tx+6][ty+6] = true; qub.push (node (tx, ty, ttt.cnt + 1)); } } } pp++; } return -1; } int main() { // CFF; int t; scanf ("%d", &t); while(t--) { scanf ("%d %d", &row, &col); for (int i = 0; i < row; i++) scanf (" %s", g[i]); len = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (g[i][j] == 'J') { sx = i, sy = j; } if (g[i][j] == 'F') { fire[len].a = i; fire[len].b = j; fire[len++].cnt = 0; } } } int tt = bfs (); if (tt == -1) puts ("IMPOSSIBLE"); else printf ("%d\n", tt); } return 0; } |
Problem 2150 Fire Game
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)
You can assume that the grass in the board would never burn out and the empty grid would never get fire.
Note that the two grids they choose can be the same.
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.
1 <= T <=100, 1 <= n <=10, 1 <= m <=10
For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2
题目类型:BFS(两个点)
算法分析:由于问题的规模比较小,所以可以枚举任意两个种草的地方并计算最小的相遇时间。如果枚举完发现还有种草的地方没有被访问到,则直接输出-1。如果发现在一次枚举过程中出现没有被访问到的节点,则不更新此时的最小值(此时包含了可能存在两个联通块的情况!!!)
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 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; char g[66][66]; int row, col, minval; bool vis[66][66]; struct node { int a, b, cnt; node (int aa, int bb, int ccnt) : a (aa), b (bb), cnt (ccnt) {} node () {} }; vector <node> aa; int dbfs (node &vala, node &valb) { memset (vis, false, sizeof (vis)); vis[vala.a][vala.b] = true, vis[valb.a][valb.b] = true; queue <node> qu; qu.push (node (vala.a, vala.b, 0)), qu.push (node (valb.a, valb.b, 0)); int res = INF; while (!qu.empty ()) { node tt = qu.front (); qu.pop (); res = tt.cnt; for (int i = 0; i < 4; i++) { int tx = tt.a + dx[i], ty = tt.b + dy[i]; if (tx >= 0 && tx <= row - 1 && ty >= 0 && ty <= col - 1 && !vis[tx][ty] && g[tx][ty] == '#') { vis[tx][ty] = true; qu.push (node (tx, ty, tt.cnt + 1)); } } } return res; } int main() { // CFF; int t, flag = 1; scanf ("%d", &t); while (t--) { printf ("Case %d: ", flag++); scanf ("%d %d", &row, &col); for (int i = 0; i < row; i++) scanf (" %s", g[i]); aa.clear (); for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) if (g[i][j] == '#') aa.push_back (node (i, j, 0)); minval = INF; for (int i = 0; i < aa.size (); i++) { for (int j = i; j < aa.size (); j++) { int temp = dbfs (aa[i], aa[j]); bool is_valid = true; for (int k = 0; k < row && is_valid; k++) { for (int q = 0; q < col && is_valid; q++) { if (g[k][q] == '#' && !vis[k][q]) { is_valid = false; break; } } } if (is_valid) minval = min (minval, temp); } } if (minval == INF) puts ("-1"); else printf ("%d\n", minval); } return 0; } |
Pots
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i): fill the poti (1 ≤ i ≤ 2) from the tap; DROP(i): empty the poti to the drain; POUR(i,j): pour from poti to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
Source
Northeastern Europe 2002, Western Subregion
题目类型:BFS+记录路径
算法分析:注意在记录路径的时候要分好类
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 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int A, B, C, vis[166][166]; struct node { int a, b, cnt; node (int aa, int bb, int ccnt) : a (aa), b (bb), cnt (ccnt) {} node () {} }; struct point { int a, b, flag; }; point par[166][166], out[666]; int pa, pb; int bfs () { memset (vis, false, sizeof (vis)); for (int i = 0; i < 166; i++) for (int j = 0; j < 166; j++) par[i][j].a = par[i][j].b = par[i][j].flag = -1; vis[0][0] = true; queue <node> qu; qu.push (node (0, 0, 0)); while (!qu.empty ()) { node tt = qu.front (); qu.pop (); if (tt.a == C || tt.b == C) { pa = tt.a, pb = tt.b; return tt.cnt; } int ta = tt.a, tb = B; if (!vis[ta][tb]) { vis[ta][tb] = true; qu.push (node (ta, tb, tt.cnt + 1)); par[ta][tb].a = tt.a; par[ta][tb].b = tt.b; par[ta][tb].flag = 1; } ta = A, tb = tt.b; if (!vis[ta][tb]) { vis[ta][tb] = true; qu.push (node (ta, tb, tt.cnt + 1)); par[ta][tb].a = tt.a; par[ta][tb].b = tt.b; par[ta][tb].flag = 2; } ta = 0, tb = tt.b; if (!vis[ta][tb]) { vis[ta][tb] = true; qu.push (node (ta, tb, tt.cnt + 1)); par[ta][tb].a = tt.a; par[ta][tb].b = tt.b; par[ta][tb].flag = 3; } ta = tt.a, tb = 0; if (!vis[ta][tb]) { vis[ta][tb] = true; qu.push (node (ta, tb, tt.cnt + 1)); par[ta][tb].a = tt.a; par[ta][tb].b = tt.b; par[ta][tb].flag = 4; } int tk = min (tt.a, B - tt.b); ta = tt.a - tk, tb = tt.b + tk; if (!vis[ta][tb]) { vis[ta][tb] = true; qu.push (node (ta, tb, tt.cnt + 1)); par[ta][tb].a = tt.a; par[ta][tb].b = tt.b; par[ta][tb].flag = 5; } tk = min (tt.b, A - tt.a); ta = tt.a + tk, tb = tt.b - tk; if (!vis[ta][tb]) { vis[ta][tb] = true; qu.push (node (ta, tb, tt.cnt + 1)); par[ta][tb].a = tt.a; par[ta][tb].b = tt.b; par[ta][tb].flag = 6; } } return -1; } int main() { // CFF; while (scanf ("%d %d %d", &A, &B, &C) != EOF) { int tt = bfs (); if (tt == -1) puts ("impossible"); else { printf ("%d\n", tt); int len = 0; do { out[len].a = par[pa][pb].a; out[len].b = par[pa][pb].b; out[len++].flag = par[pa][pb].flag; int tta = par[pa][pb].a, ttb = par[pa][pb].b; pa = tta, pb = ttb; } while (pa != -1 && pb != -1); for (int i = len - 1; i >= 0; i--) { if (out[i].flag == 1) puts ("FILL(2)"); if (out[i].flag == 2) puts ("FILL(1)"); if (out[i].flag == 3) puts ("DROP(1)"); if (out[i].flag == 4) puts ("DROP(2)"); if (out[i].flag == 5) puts ("POUR(1,2)"); if (out[i].flag == 6) puts ("POUR(2,1)"); } } } return 0; } |
Shuffle'm Up
The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containingC chips. Each stack may contain chips of several different colors.
The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.
After the shuffle operation, S12 is split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new S2. The shuffle operation may then be repeated to form a new S12.
For this problem, you will write a program to determine if a particular resultant stack S12 can be formed by shuffling two stacks some number of times.
Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of four lines of input. The first line of a dataset specifies an integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S1 and S2 zero or more times. The bottommost chip’s color is specified first.
Output
Output for each dataset consists of a single line that displays the dataset number (1 though N), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (−1) for the number of shuffle operations.
Sample Input
2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC
Sample Output
1 2
2 -1
Source
题目类型:BFS
算法分析:直接按照题目的规则进行BFS即可
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 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; string sa, sb, ss; int n; string TT (string &vala, string &valb) { string res; for (int i = 0; i < n; i++) { res += vala[i]; res += valb[i]; } return res; } struct node { string cur; int cnt; node (string ccur, int ccnt) : cur (ccur), cnt (ccnt) {} node () {} }; int bfs () { map<string, bool> vis; queue <node> qu; qu.push (node (TT(sb,sa), 1)); while (!qu.empty ()) { node tt = qu.front (); qu.pop (); if (tt.cur == ss) return tt.cnt; string ta, tb; for (int i = 0; i < n; i++) ta += tt.cur[i]; for (int i = n; i < 2 * n; i++) tb += tt.cur[i]; tt.cur = TT (tb, ta); if (!vis[tt.cur]) { vis[tt.cur] = true; qu.push (node (tt.cur, tt.cnt + 1)); } } return -1; } int main() { // CPPFF; int t, flag = 1; cin >> t; while(t--) { cin >> n; cin >> sa >> sb >> ss; cout << flag++ << " " << bfs () << endl; } return 0; } |