暑假训练二阶段day3

Shuffle’m Up

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 containing C chips. Each stack may contain chips of several different colors.

The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:

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 S2 starting 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

题意:就是不断合并s1,s2;然后重新分开,得到新的s1,s2;直到得到指定字符串;永远得不到就输出-1;

#include <iostream>
#include <algorithm>
#include <map>
#include <string>

using namespace std;

int main(void) {
    int t;
    cin >> t;
    map<string, bool> vis;
    int times = 0;
    while (t--) {
        int ans = 0;
        vis.clear();
        int n;
        string s1, s2, s12;
        cin >> n >> s1 >> s2 >> s12;
        while (1) {
            ans++;
            string stmp = "";
            for (int j = 0, i = 0; j < n; j++, i++) {
                stmp += s2[i];
                stmp += s1[j];
            }
            if (vis[stmp] && stmp != s12) {
                ans = -1;
                break;
            }
            if (stmp == s12) break;
            vis[stmp] = true;
            s1 = stmp.substr(0, n);
            s2 = stmp.substr(n);
        }
        times++;
        cout << times << " " << ans << endl;
    }
    return 0;
}

Prime Path

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.

Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.

    1033
    1733
    3733
    3739
    3779
    8779
    8179

The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

Input
One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).
Output
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
Sample Input

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0

题意和题解

Function Run Fun

We all love recursion! Don't we?

Consider a three-parameter recursive function w(a, b, c):

if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1

if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)

if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)

otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)

This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.

Input
The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above technique, you are to calculate w(a, b, c) efficiently and print the result.
Output
Print the value for w(a,b,c) for each triple.
Sample Input

1 1 1
2 2 2
10 4 6
50 50 50
-1 7 18
-1 -1 -1

Sample Output

w(1, 1, 1) = 2
w(2, 2, 2) = 4
w(10, 4, 6) = 523
w(50, 50, 50) = 1048576
w(-1, 7, 18) = 1

题解:按公式记忆化搜索

#include<iostream>
#include<cstring>

const int N = 25;
using namespace std;
int dp[N][N][N];

int w(int a, int b, int c) {
    if (a <= 0 || b <= 0 || c <= 0)
        return 1;
    if (a > 20 || b > 20 || c > 20)
        return dp[20][20][20] = w(20, 20, 20);
    if (dp[a][b][c] != 0)
        return dp[a][b][c];
    if (a < b && b < c)
        return dp[a][b][c] = (w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c));
    return dp[a][b][c] = (w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1));
}

int main(void) {
    ios::sync_with_stdio(false);
    memset(dp, 0, sizeof(dp));
    int a, b, c;
    while (cin >> a >> b >> c) {
        if (a == -1 && b == -1 && c == -1) break;
        cout << "w(" << a << ", " << b << ", " << c << ") = " << w(a, b, c) << endl;
    }
    return 0;
}

蜘蛛牌

 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么这些牌也跟着一起移动,游戏的目的是将所有的牌按同一花色从小到大排好,为了简单起见,我们的游戏只有同一花色的10张牌,从A到10,且随机的在一行上展开,编号从1到10,把第i号上的牌移到第j号牌上,移动距离为abs(i-j),现在你要做的是求出完成游戏的最小移动距离。

Input
第一个输入数据是T,表示数据的组数。
每组数据有一行,10个输入数据,数据的范围是[1,10],分别表示A到10,我们保证每组数据都是合法的。
Output
对应每组数据输出最小移动距离。
Sample Input

1
1 2 3 4 5 6 7 8 9 10

Sample Output

9

题解:按照顺序回溯搜

#include <iostream>
#include <algorithm>
#include <cstring>
#define inf 0x3f3f3f3f

using namespace std;
int a[20];
int vis[20];
int ans = inf;

void dfs(int sum, int t) {
    if (sum > ans || t == 9) {
        if (ans > sum) ans = min(ans, sum);
        return;
    }
    for (int i = 1; i <= 9; i++)
    {
        if (!vis[i]) {
            vis[i] = 1;
            for (int j = i + 1; j <= 10; j++) {
                if (!vis[j]) {
                    dfs(sum + abs(a[i] - a[j]), t + 1);
                    break;
                }
            }
            vis[i] = 0;
        }
    }
    return ;
}

int main(void) {
    int t;
    cin >> t;
    while (t--) {
        ans = inf;
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= 10; i++) {
            int x;
            cin >> x;
            a[x] = i;
        }
        dfs(0, 0);
        cout << ans << endl;
    }
    return 0;
}

Nightmare Ⅱ

 Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.

Input
The input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.
Output
Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.
Sample Input

3
5 6
XXXXXX
XZ..ZX
XXXXXX
M.G...
......
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...

10 10
..........
..X.......
..M.X...X.
X.........
.X..X.X.X.
.........X
..XX....X.
X....G...X
...ZX.X...
...Z..X..X

Sample Output

1
1
-1

题意和题解

Color the ball

 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗? 

Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input

3
1 1
2 2
3 3
3
1 1
1 2
1 3
0

Sample Output

1 1 1
3 2 1

差分即可

#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>

using namespace std;

const int N = 1e6 + 10;


int main(void) {
    int n;
    while(cin >> n && n)
    {
        int a[N] = {0};
        for(int i = 1; i <= n; i++)
        {
            int l,r;
            cin >> l >> r;
            a[l]++,a[r+1]--;
        }
        for(int i = 1; i <= n; i++)
        {
            a[i+1]+=a[i];
            cout << a[i];
            if(i!=n)
                cout << " ";
        }
        cout << endl;
    }
    return 0;

Tempter of the Bone

 The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.

Output
For each test case, print in one line “YES” if the doggie can survive, or “NO” otherwise.
Sample Input

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

Sample Output

NO
YES

题意:在一个迷宫中有一只小狗和一根骨头,然后在T分钟的时候恰好迷宫的出口会打开,然后问小狗是否能在第T分钟的时候到达出口离开迷宫。

小狗可以向上下左右四个方向移动,然后小狗不能走回头路,即已经走过了的路就不能再走,并且小狗只能一直走,不能停下来。

题解:dfs+剪枝

#include<iostream>
#include<cstring>
#include<cmath>

using namespace std;
const int N = 10;
string s[N];
bool vis[N][N];
int n, m, t;
int sx, sy, ex, ey;
bool flag;
int d[][2] = {0, 1, 0, -1, 1, 0, -1, 0};

void dfs(int x, int y, int z) {
    if (flag)
        return;
    if (z > t)
        return;
    if (x == ex && y == ey && z == t) {
        flag = true;
        return;
    }
    if (x == ex && y == ey && z < t)
        return;

    int temp = t - abs(x - ex) - abs(y - ey) - z;
    if (temp < 0 || temp & 1)
        return;
    for (int i = 0; i < 4; i++) {
        int tx = x + d[i][0];
        int ty = y + d[i][1];
        if (tx >= 0 && tx < n && ty >= 0 && ty < m && s[tx][ty] != 'X' && !vis[tx][ty])
        {
            vis[tx][ty] = true;
            dfs(tx, ty, z + 1);
            vis[tx][ty] = false;
        }
    }
    return;
}

int main(void) {
    while (cin >> n >> m >> t && n != 0 && (m + n + t)) {
        int len = 0;
        flag = false;
        memset(vis, false, sizeof(vis));
        s->clear();
        for(int i = 0; i < n; i++)
        {
            cin >> s[i];
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (s[i][j] == 'X')
                    len++;
                if (s[i][j] == 'S') {
                    sx = i;
                    sy = j;
                    s[i][j] = '.';
                    vis[i][j] = true;
                }
                if (s[i][j] == 'D') {
                    ex = i;
                    ey = j;
                    s[i][j] = '.';
                }
            }
        }
        if (n * m - len >= t)
            dfs(sx, sy, 0);
        if (flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值