Capricorn's Trial E - 5 HDU - 1043 & F - 6 HDU - 2181 & G - 7 HDU - 1213 & H - 8 HDU - 1541

                                                                                 Eight

Problem 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 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

题意:给出一个字符矩阵,使其变成123456789,输出其变化过程。
思路:采用BFS,从最后的状态往前回溯,记录每种变化方式。

#include<queue>
#include<math.h>
#include<string>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
	string a;
	int s[3][3];
	int x, y;
};
map<int, int>s1;
map<int, string>s2;
int nextt[4][2] = { 0,1,0,-1,1,0,-1,0 };
void bfs()
{
	queue<node>Q;
	node p, q;
	int i, j, k;
	for (i = 0; i<3; i++)
		for (j = 0; j<3; j++)
			p.s[i][j] = i * 3 + j + 1;
	p.s[2][2] = 0;
	p.x = 2;
	p.y = 2;
	Q.push(p);
	s1[123456780] = 1;
	while (!Q.empty())
	{
		p = Q.front();
		Q.pop();
		for (i = 0; i<4; i++)
		{
			q.x = p.x + nextt[i][0];
			q.y = p.y + nextt[i][1];
			if (q.x<0 || q.x >= 3 || q.y<0 || q.y >= 3)
				continue;
			for (j = 0; j<3; j++)
				for (k = 0; k<3; k++)
					q.s[j][k] = p.s[j][k];
			swap(q.s[p.x][p.y], q.s[q.x][q.y]);
			int sum = 1;
			for (j = 0; j<3; j++)
				for (k = 0; k<3; k++)
					sum = sum * 10 + q.s[j][k];
			if (s1[sum] == 1)
				continue;
			s1[sum] = 1;
			if (i == 0)
				q.a = p.a + 'l';
			else if (i == 1)
				q.a = p.a + 'r';
			else if (i == 2)
				q.a = p.a + 'u';
			else if (i == 3)
				q.a = p.a + 'd';
			Q.push(q);
			s2[sum] = q.a;
		}
	}
}
int main()
{
	bfs();
	char s[30];
	while (gets_s(s))
	{
		int l = strlen(s);
		int i, sum = 1;
		for (i = 0; i < l; i++)
		{
			if (s[i] >= '1'&&s[i] <= '9')
				sum = sum * 10 + s[i] - '0';
			else if (s[i] == 'x')
				sum *= 10;
		}
		if (s1[sum] == 0)
			cout << "unsolvable\n";
		else
		{
			string st = s2[sum];
			for (i = st.length() - 1; i >= 0; i--)
				cout << st[i];
			cout << endl;
		}
	}
}

                                                                 哈密顿绕行世界问题

Problem Description

一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市。

Input

前20行的第i行有3个数,表示与第i个城市相邻的3个城市.第20行以后每行有1个数m,m<=20,m>=1.m=0退出.

Output

输出从第m个城市出发经过每个城市1次又回到m的所有路线,如有多条路线,按字典序输出,每行1条路线.每行首先输出是第几条路线.然后个一个: 后列出经过的城市.参看

Sample output

2 5 20

1 3 12

2 4 10

3 5 8

1 4 6

5 7 19

6 8 17

4 7 9

8 10 16

3 9 11

10 12 15

2 11 13

12 14 20

13 15 18

11 14 16

9 15 17

7 16 18

14 17 19

6 18 20

1 13 19

5

0

Sample Output

1: 5 1 2 3 4 8 7 17 18 14 15 16 9 10 11 12 13 20 19 6 5

2: 5 1 2 3 4 8 9 10 11 12 13 20 19 18 14 15 16 17 7 6 5

3: 5 1 2 3 10 9 16 17 18 14 15 11 12 13 20 19 6 7 8 4 5

4: 5 1 2 3 10 11 12 13 20 19 6 7 17 18 14 15 16 9 8 4 5

5: 5 1 2 12 11 10 3 4 8 9 16 15 14 13 20 19 18 17 7 6 5

6: 5 1 2 12 11 15 14 13 20 19 18 17 16 9 10 3 4 8 7 6 5

7: 5 1 2 12 11 15 16 9 10 3 4 8 7 17 18 14 13 20 19 6 5

8: 5 1 2 12 11 15 16 17 18 14 13 20 19 6 7 8 9 10 3 4 5

9: 5 1 2 12 13 20 19 6 7 8 9 16 17 18 14 15 11 10 3 4 5

10: 5 1 2 12 13 20 19 18 14 15 11 10 3 4 8 9 16 17 7 6 5

11: 5 1 20 13 12 2 3 4 8 7 17 16 9 10 11 15 14 18 19 6 5

12: 5 1 20 13 12 2 3 10 11 15 14 18 19 6 7 17 16 9 8 4 5

13: 5 1 20 13 14 15 11 12 2 3 10 9 16 17 18 19 6 7 8 4 5

14: 5 1 20 13 14 15 16 9 10 11 12 2 3 4 8 7 17 18 19 6 5

15: 5 1 20 13 14 15 16 17 18 19 6 7 8 9 10 11 12 2 3 4 5

16: 5 1 20 13 14 18 19 6 7 17 16 15 11 12 2 3 10 9 8 4 5

17: 5 1 20 19 6 7 8 9 10 11 15 16 17 18 14 13 12 2 3 4 5

18: 5 1 20 19 6 7 17 18 14 13 12 2 3 10 11 15 16 9 8 4 5

19: 5 1 20 19 18 14 13 12 2 3 4 8 9 10 11 15 16 17 7 6 5

20: 5 1 20 19 18 17 16 9 10 11 15 14 13 12 2 3 4 8 7 6 5

21: 5 4 3 2 1 20 13 12 11 10 9 8 7 17 16 15 14 18 19 6 5

22: 5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5

23: 5 4 3 2 12 11 10 9 8 7 6 19 18 17 16 15 14 13 20 1 5

24: 5 4 3 2 12 13 14 18 17 16 15 11 10 9 8 7 6 19 20 1 5

25: 5 4 3 10 9 8 7 6 19 20 13 14 18 17 16 15 11 12 2 1 5

26: 5 4 3 10 9 8 7 17 16 15 11 12 2 1 20 13 14 18 19 6 5

27: 5 4 3 10 11 12 2 1 20 13 14 15 16 9 8 7 17 18 19 6 5

28: 5 4 3 10 11 15 14 13 12 2 1 20 19 18 17 16 9 8 7 6 5

29: 5 4 3 10 11 15 14 18 17 16 9 8 7 6 19 20 13 12 2 1 5

30: 5 4 3 10 11 15 16 9 8 7 17 18 14 13 12 2 1 20 19 6 5

31: 5 4 8 7 6 19 18 17 16 9 10 3 2 12 11 15 14 13 20 1 5

32: 5 4 8 7 6 19 20 13 12 11 15 14 18 17 16 9 10 3 2 1 5

33: 5 4 8 7 17 16 9 10 3 2 1 20 13 12 11 15 14 18 19 6 5

34: 5 4 8 7 17 18 14 13 12 11 15 16 9 10 3 2 1 20 19 6 5

35: 5 4 8 9 10 3 2 1 20 19 18 14 13 12 11 15 16 17 7 6 5

36: 5 4 8 9 10 3 2 12 11 15 16 17 7 6 19 18 14 13 20 1 5

37: 5 4 8 9 16 15 11 10 3 2 12 13 14 18 17 7 6 19 20 1 5

38: 5 4 8 9 16 15 14 13 12 11 10 3 2 1 20 19 18 17 7 6 5

39: 5 4 8 9 16 15 14 18 17 7 6 19 20 13 12 11 10 3 2 1 5

40: 5 4 8 9 16 17 7 6 19 18 14 15 11 10 3 2 12 13 20 1 5

41: 5 6 7 8 4 3 2 12 13 14 15 11 10 9 16 17 18 19 20 1 5

42: 5 6 7 8 4 3 10 9 16 17 18 19 20 13 14 15 11 12 2 1 5

43: 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5

44: 5 6 7 8 9 16 17 18 19 20 1 2 12 13 14 15 11 10 3 4 5

45: 5 6 7 17 16 9 8 4 3 10 11 15 14 18 19 20 13 12 2 1 5

46: 5 6 7 17 16 15 11 10 9 8 4 3 2 12 13 14 18 19 20 1 5

47: 5 6 7 17 16 15 11 12 13 14 18 19 20 1 2 3 10 9 8 4 5

48: 5 6 7 17 16 15 14 18 19 20 13 12 11 10 9 8 4 3 2 1 5

49: 5 6 7 17 18 19 20 1 2 3 10 11 12 13 14 15 16 9 8 4 5

50: 5 6 7 17 18 19 20 13 14 15 16 9 8 4 3 10 11 12 2 1 5

51: 5 6 19 18 14 13 20 1 2 12 11 15 16 17 7 8 9 10 3 4 5

52: 5 6 19 18 14 15 11 10 9 16 17 7 8 4 3 2 12 13 20 1 5

53: 5 6 19 18 14 15 11 12 13 20 1 2 3 10 9 16 17 7 8 4 5

54: 5 6 19 18 14 15 16 17 7 8 9 10 11 12 13 20 1 2 3 4 5

55: 5 6 19 18 17 7 8 4 3 2 12 11 10 9 16 15 14 13 20 1 5

56: 5 6 19 18 17 7 8 9 16 15 14 13 20 1 2 12 11 10 3 4 5

57: 5 6 19 20 1 2 3 10 9 16 15 11 12 13 14 18 17 7 8 4 5

58: 5 6 19 20 1 2 12 13 14 18 17 7 8 9 16 15 11 10 3 4 5

59: 5 6 19 20 13 12 11 10 9 16 15 14 18 17 7 8 4 3 2 1 5

60: 5 6 19 20 13 14 18 17 7 8 4 3 10 9 16 15 11 12 2 1 5

题解:简单深搜,对于深搜没有什么诀窍。就是分析题意找出终止条件,然后根据终止条件选择需要使用的变量。
再根据题意进行模拟步骤。最后则是优化剪枝(如果需要的话)。

题中停止往下走的条件是 到达起点,且20个城市走一圈。这里有俩变量了 

1:当前所在城市                            x

2:已经走过的城市数目                 y

所以终止条件是        x=起点城市      y=20

执行搜索条件是 该城市没有访问过

于是得出以下程序。开始时并不把起点城市算入已访问,这样更简单一点。

#include<iostream>
using namespace std;
int a[21], b[21], c[21], visited[22], m, s = 0;
void dfs(int x, int y) //x:当前所在城市
{

	// y:当前已经走过的城市数目
	if (y == 20 && x == m)
	{
		s++;
		// 走了一圈,且最后一个为起点
		//ok
		cout << s << ":" << "  " << m;
		for (int i = 1; i <= 20; i++)
			for (int j = 1; j <= 20; j++)
			{
				if (visited[j] == i)
				{
					cout << " " << j;
				}

			}
		cout << endl;
	}
	if (y>20) //算上出发城市,最多走20次
	{
		return;
	}
	if (visited[a[x]] == 0)
	{
		visited[a[x]] = y + 1;
		dfs(a[x], y + 1);
		visited[a[x]] = 0;
	}
	if (visited[b[x]] == 0)
	{
		visited[b[x]] = y + 1;
		dfs(b[x], y + 1);
		visited[b[x]] = 0;
	}
	if (visited[c[x]] == 0)
	{
		visited[c[x]] = y + 1;
		dfs(c[x], y + 1);
		visited[c[x]] = 0;
	}
}

int main()
{
	int i, j;
	memset(visited, 0, sizeof(visited));
	for (i = 1; i <= 20; i++)
	{
		cin >> a[i] >> b[i] >> c[i];
	}
	while (cin>>m && m)
	{
		dfs(m, 0);
	}
}

                                                                  How Many Tables

Problem Description

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

Input

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

Output

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

Sample Input

2
5 3
1 2
2 3
4 5

5 1
2 5

Sample Output

2

4

题目大意:一个人办聚会,要来的人不是朋友的不能坐一桌,坐一桌的朋友可以是间接的朋友。问最少需要准备多少张桌子。
解题思路:并查集连接后,求树的个数,不太会,只能调用模板;

#include <iostream>
using namespace std;
int pre[1010];
int find(int x)
{
	int r = x;
	while (r != pre[r])
	{
		r = pre[r];
	}
	return r;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, m;
		cin >> n >> m;
		for (int i = 1; i <= n; i++)//初始化 
		{
			pre[i] = i;
		}
		for (int i = 0; i < m; i++)
		{
			int x, y;
			cin >> x >> y;
			int fx = find(x);
			int fy = find(y);
			if (fx != fy)
			{
				pre[fx] = fy;
			}
		}
		int cnt = 0;
		for (int i = 1; i <= n; i++)//求树的个数 
		{
			if (pre[i] == i)
			{
				cnt++;
			}
		}
		cout << cnt << endl;
	}
}

                                                                       Stars

Problem Description

 

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.

.

 

 

For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.

You are to write a program that will count the amounts of the stars of each level on a given map

Input

The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
 

Output

The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.

 

Sample Input

1 1

5 1

7 1

3 3 

5 5 

Sample Output

1

2

1

1

0

 

 

Sample Output

1

2

1

1

0

参考文章:https://blog.csdn.net/libin56842/article/details/46582685 
题目大意:

在坐标上有n个星星,如果某个星星坐标为(x, y), 它的左下位置为:(x0,y0),x0<=x 且y0<=y。如果左下位置有a个星星,就表示这个星星属于level x

按照y递增,如果y相同则x递增的顺序给出n个星星,求出所有level水平的数量。

分析与总结:

因为输入是按照按照y递增,如果y相同则x递增的顺序给出的, 所以,对于第i颗星星,它的level就是之前出现过的星星中,横坐标x小于等于i星横坐标的那些星星的总数量(前面的y一定比后面的y小)。

所以,需要找到一种数据结构来记录所有星星的x值,方便的求出所有值为0~x的星星总数量。

 

#include <iostream>
using namespace std;
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define N 32005
#define lowbit(x) (x&-x)
const int mod = 1e9 + 7;
int a[N];
int n, c[N], ans[N], x, y;
int sum(int x)
{
	int ret = 0;
	while (x>0)
	{
		ret += c[x];
		x -= lowbit(x);
	}
	return ret;
}

void add(int x, int d)
{
	while (x <= N)
	{
		c[x] += d;
		x += lowbit(x);
	}
}

int main()
{
	int i, j, k;
	while (cin>>n)
	{
		MEM(c, 0);
		MEM(ans, 0);
		for (i = 1; i <= n; i++)
		{
			cin>>x>>y;
			x++;
			ans[sum(x)]++;
			add(x, 1);
		}
		for (i = 0; i < n; i++)
			cout << ans[i] << endl;
	}

	return 0;
}

 

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值